Subject: Beginning Visual C# Exercises - Chapter 12 Answers
Posted By: seblake Post Date: 9/24/2004 4:16:43 PM
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;
        }

Go to topic 19747

Return to index page 764
Return to index page 763
Return to index page 762
Return to index page 761
Return to index page 760
Return to index page 759
Return to index page 758
Return to index page 757
Return to index page 756
Return to index page 755