Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2004, 04:01 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 12

1. // Here's the general purpose event handler, but there is a problem....
        public void GenericEvent(object sender, EventArgs e)
        {
            if (e is MessageArrivedEventArgs)
                Console.WriteLine("Just received a MessageArrived event from {0}: {1}",
                                 sender.ToString, (e as MessageArrivedEventArgs).Message);

            else if (e is ElapsedEventArgs)
                Console.WriteLine("Just received an Elapsed event from {0}: {1}",
                              sender.ToString, (e as ElapsedEventArgs).SignalTime);
        }

        // The GenericEvent signature won't delegate an ElapsedEvents argument so force it
        public void ProcessElapsedEvent(timer sender, ElapsedEventArgs e)
        {
            GenericEvent(sender, e);
        }

2. ... Wrote two new methods and replaced old HasWon() method
        // A method to determine whether rank values are in sequence
        private bool isStraight(int[] theHand, int theSpan)
        {
            int maxRank = 0;
            int minRank = 13;
            for (int r = 0; r < theSpan; r++)
            {
                if (theHand[r] > maxRank)
                    maxRank = theHand[r];

                if (theHand[r] < minRank)
                    minRank = theHand[r];
            }

            return (maxRank == (minRank + theSpan -1));
        }

        // A method to determine whether a rank card is within straight flush
        private bool isClash(int[] arraySuit, int matchSuit, int[] arrayRank, int matchRank)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    if ((arraySuit[i] == matchSuit) && (arrayRank[j] == matchRank))
                        return true;
                }
            }

            return false;
        }

        public bool HasWon()
        {
            // Declare and initialize variables
            int cardCount = 0;
            int rankValue = -1;
            bool haveRank4 = false;
            bool haveRank3 = false;
            bool straightFlush4 = false;
            bool straightFlush3 = false;
            int[] sevenCards = new int[7];
            int[] suitRank3 = new int[3];

            // Determine what card ranks are in hand
            for (Rank currentRank = 0; currentRank < (Rank)13; currentRank++)
            {
                foreach (Card theCard in hand)
                {
                    if (theCard.rank == currentRank)
                        // Load the suit for each card that matches currentRank
                        sevenCards[cardCount++] = (int)theCard.suit;
                }

                // Analyze hand rank status
                if (cardCount == 4)
                {
                    // All four suits are present in hand for currentRank
                    haveRank4 = true;
                    // Remember for which rank we have all the 4 suits
                    rankValue = (int)currentRank;
                }

                else if (haveRank4 && (cardCount == 2))
                    // Can not accomplish rummy win
                    return false;

                else if (haveRank3 && (cardCount > 1))
                    // Can not accomplish rummy win
                    return false;

                else if (cardCount == 3)
                {
                    haveRank3 = true;
                    // Remember for which rank we have 3 of the 4 suits
                    rankValue = (int)currentRank;

                    for (int q = 0; q < cardCount; q++)
                    {
                        suitRank3[q] = sevenCards[q];
                    }
                }

                // Test if we have both 3 and 4 of a kind status
                if (haveRank3 && haveRank4)
                    // This is a winning rummy hand
                    return true;

                // Reset the variables for next loop
                sevenCards.Initialize();
                cardCount = 0;
            }

            // Determine how many of each suit are in hand
            for (Suit currentSuit = 0; currentSuit < (Suit)4; currentSuit++)
            {
                foreach (Card theCard in hand)
                {
                    if (theCard.suit == currentSuit)
                        // Remember the rank of each card in currentSuit
                        sevenCards[cardCount++] = (int)theCard.rank;
                }

                // Analyze hand rank status
                if (cardCount == 7)
                {
                    // All seven cards in hand are of same suit (flush)
                    if (isStraight(sevenCards, cardCount))
                        return true;

                    else
                    {
                        // Use array class method for rank order sequence
                        Array.Sort(sevenCards);

                        // Make arrays to two possible rank combinations of 3 and 4 sequence
                        int[] testFour1 = new int[4];
                        int[] testFour2 = new int[4];
                        int[] testThree1 = new int[3];
                        int[] testThree2 = new int[3];

                        // Assign rank values from hand
                        for (int h = 0; h < cardCount; h++)
                        {
                            if (h < 4)
                                testFour1[h] = sevenCards[h];

                            if (h > 2)
                                testFour2[h -3] = sevenCards[h];

                            if (h > 3)
                                testThree1[h -4] = sevenCards[h];

                            if (h < 3)
                                testThree2[h] = sevenCards[h];
                        }

                        if (isStraight(testFour1, 4) && isStraight(testThree1, 3))
                            // Test 1: first four in rank sequence; last three in another rank sequence
                            return true;

                        else if (isStraight(testFour2, 4) && isStraight(testThree2, 3))
                            // Test 2: first three in rank sequence; last four in another rank sequence
                            return true;

                        else
                            // Test failed; do not have 3 & 4 rank sequences
                            return false;
                    }
                }
                else if (cardCount == 6)
                {
                    // Can not accomplish rummy win
                    return false;
                }
                else if ((cardCount == 5) && (haveRank3 == true))
                {
                    // Make array to test 4 sequence
                    int[] testFour = new int[4];
                    int k = 0;

                    // Select the 4 cards not involved in the rank match
                    for (int h = 0; h < cardCount; h++)
                    {
                        if ((sevenCards[h] != rankValue) && (k < cardCount))
                            testFour[k++] = sevenCards[h];
                    }

                    if ((k == (cardCount)) && (sevenCards[(cardCount -1)] != rankValue))
                        // Rummy hand not possible
                        return false;

                    else
                        // Have three cards of same rank, test if 4 card flush is straight
                        return isStraight(testFour, 4);
                }
                else if ((cardCount == 4) && (isStraight(sevenCards, cardCount)))
                {
                    if ((haveRank4 == true) || (straightFlush3 == true))
                        // Have 4 card straight flush which includes one card from rank match
                        return true;

                    else if (haveRank3 == true)
                        // Determine whether rank card from straight flush in in rank match
                        return !isClash(sevenCards, rankValue, suitRank3, (int)currentSuit);

                    else
                        // Remember we have a four card straight flush
                        straightFlush4 = true;
                }
                else if ((cardCount == 4) && (haveRank4 == true))
                {
                    // Make array to test 3 sequence
                    int[] testThree = new int[3];
                    int k = 0;

                    // Select the 3 cards not involved in the rank match
                    for (int h = 0; h < cardCount; h++)
                    {
                        if ((sevenCards[h] != rankValue) && (k < cardCount))
                            testThree[k++] = sevenCards[h];
                    }

                    if ((k == (cardCount)) && (sevenCards[(cardCount -1)] != rankValue))
                        // Rummy hand not possible
                        return false;

                    else
                        // Have three cards of same rank, test if 4 card flush is straight
                        return isStraight(testThree, 3);
                }
                else if ((cardCount == 3) && (isStraight(sevenCards, cardCount)))
                {
                    if ((haveRank4 == true) || (straightFlush4 == true))
                        return true;

                    else
                        straightFlush3 = true;
                }

                sevenCards.Initialize();
                cardCount = 0;
            }

            return false;
        }
 
Old August 21st, 2004, 02:55 PM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll appreciate what you're doing. It's help a lot!
Thanks,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 18 seblake C# 1 October 9th, 2004 11:33 PM
Beginning Visual C# Exercises - Chapter 15 seblake C# 1 September 17th, 2004 08:41 PM
Beginning Visual C# Exercises - Chapter 03 seblake C# 1 July 19th, 2004 09:16 AM
Beginning Visual C# Exercises - Chapter 06 seblake C# 1 July 19th, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.