Example #1
0
        // Returns a List<Hand> of the specified dimension.
        // Each hand populated randomly, without duplication (i.e. no card appears twice)
        public List<Hand> Deal(int handCount, int handLength)
        {
            // What error checking is missing here?

            List<Hand> dealtHands = new List<Hand>();

            // Used to insure each card appears only once in the deal
            bool[,] cardUsed = new bool[RankCount, SuitCount];
            Random rand = new Random();

            // Take time to make sure you understand the logic.
            // This is a very common algorithm pattern.
            for (int h = 0; h < handCount; h++)
            {
                Hand currentHand = new Hand();
                int cardCount = 0;

                while (cardCount < handLength)
                {
                    int nextCardRank = rand.Next(RankCount);
                    int nextCardSuit = rand.Next(SuitCount);

                    if (cardUsed[nextCardRank, nextCardSuit] == false)
                    {
                        currentHand.AddCard(cards[nextCardRank, nextCardSuit]);
                        cardUsed[nextCardRank, nextCardSuit] = true;
                        cardCount++;
                    }
                }
                currentHand.ComputeHCP();
                dealtHands.Add(currentHand);
            } // all hands dealt
            return dealtHands;
        }
        public void PrintHand(ListBox displayBox, Hand handToPrint)
        {
            String spadeString = "S:\t";
            String heartString = "H:\t";
            String diamondString = "D:\t";
            String clubString = "C:\t";

            //Code that I added
            //Sort by rank before suit
            handToPrint.sortCards();
            //End

            // Remember the ShortRank data member in the Card class?
            // Here's where we use it...
            foreach(Card c in handToPrint.CardsInHand)
            {
                switch(c.Suit)
                {
                    case SuitValue.Spades:
                        spadeString += c.ShortRank + " ";
                        break;
                    case SuitValue.Hearts:
                        heartString += c.ShortRank + " ";
                        break;
                    case SuitValue.Diamonds:
                        diamondString += c.ShortRank + " ";
                        break;
                    case SuitValue.Clubs:
                        clubString += c.ShortRank + " ";
                        break;
                }
            }

            displayBox.Items.Add(spadeString);
            displayBox.Items.Add(heartString);
            displayBox.Items.Add(diamondString);
            displayBox.Items.Add(clubString);

            int hcp = handToPrint.TotalHCP;
            displayBox.Items.Add("HCP: " + hcp);
        }
        public void PrintHand(ListBox displayBox, Hand handToPrint)
        {
            String spadeString = "S:\t";
            String heartString = "H:\t";
            String diamondString = "D:\t";
            String clubString = "C:\t";

            // Sort the hand - I have put this here instead of the hand constructor, incase something
            // else is relying on th hand not being sorteds
            handToPrint.Sort();

            // Remember the ShortRank data member in the Card class?
            // Here's where we use it...
            foreach(Card c in handToPrint.CardsInHand)
            {
                switch(c.Suit)
                {
                    case SuitValue.Spades:
                        spadeString += c.ShortRank + " ";
                        break;
                    case SuitValue.Hearts:
                        heartString += c.ShortRank + " ";
                        break;
                    case SuitValue.Diamonds:
                        diamondString += c.ShortRank + " ";
                        break;
                    case SuitValue.Clubs:
                        clubString += c.ShortRank + " ";
                        break;
                }
            }

            displayBox.Items.Add(spadeString);
            displayBox.Items.Add(heartString);
            displayBox.Items.Add(diamondString);
            displayBox.Items.Add(clubString);

            int hcp = handToPrint.TotalHCP;
            displayBox.Items.Add("HCP: " + hcp);
        }
        public void PrintHand(ListBox displayBox, Hand handToPrint)
        {
            //sorts the hands in order from highest to lowest suit and rank
            handToPrint.CardsInHand.Sort();

            String spadeString = "S:\t";
            String heartString = "H:\t";
            String diamondString = "D:\t";
            String clubString = "C:\t";

            // Remember the ShortRank data member in the Card class?
            // Here's where we use it...
            foreach (Card c in handToPrint.CardsInHand)
            {
                switch (c.Suit)
                {
                    case SuitValue.Spades:
                        spadeString += c.ShortRank + " ";
                        break;
                    case SuitValue.Hearts:
                        heartString += c.ShortRank + " ";
                        break;
                    case SuitValue.Diamonds:
                        diamondString += c.ShortRank + " ";
                        break;
                    case SuitValue.Clubs:
                        clubString += c.ShortRank + " ";
                        break;
                }
            }

            displayBox.Items.Add(spadeString);
            displayBox.Items.Add(heartString);
            displayBox.Items.Add(diamondString);
            displayBox.Items.Add(clubString);

            int hcp = handToPrint.TotalHCP;
            displayBox.Items.Add("HCP: " + hcp);
        }