Пример #1
0
        static void shuffleAndDeal()
        {
            //Exercise 1: Shuffle Cards
            //Write a program to create a standard 52 card deck and then deal a random hand to a player.
            //Once a card is dealt then it can't be used again.

            //create a new deck (instance of class Deck)
            Deck myDeck = new Deck();
            Console.WriteLine("Original order of myDeck:");
            Console.WriteLine(myDeck.ToString());
            Console.WriteLine();    //Whitespace

            //shuffle it
            myDeck.shuffle();
            Console.WriteLine("Order of myDeck after shuffle:");
            Console.WriteLine(myDeck.ToString());
            Console.WriteLine();    //Whitespace

            //create a player hand, dealing off the deck
            List<Card> hand = myDeck.dealHand(5);   //five is a traditional hand size for Poker

            Console.WriteLine("myDeck after dealing:");
            Console.WriteLine(myDeck.ToString());
            Console.WriteLine();    //Whitespace

            Console.WriteLine("And the hand dealt contains:");
            foreach (Card c in hand)
            {
                Console.WriteLine(c.ToString());
            }
        }
Пример #2
0
        static void ShuffleAndDeal4Hands()
        {
            //Objectionable Objects, Exercise 1: Card Shuffle duex
            //Rewrite your code for the card shuffle to deal a random hand to 4 players.
            //Use objects to represent each player's hand.

            //create a new deck (instance of class Deck), and shuffle it
            Deck myDeck = new Deck();
            myDeck.shuffle();

            //create a list of hands and populate with new empty hands to represent the four players' hands
            List<HandOfCards> playerHands = new List<HandOfCards>();
            for (int i = 0; i < 4; i++)
            {
                playerHands.Add(new HandOfCards(5));    //e.g.poker hands have five cards , so make limit 5
            }
            //Deal cards to each of the hands in the list of hands
            bool allHandsFull = false;
            while (!allHandsFull)       //careful!  if the hands had no hand limit, this would be an infinite loop!
            {
                foreach (HandOfCards h in playerHands)
                {
                    h.beDealt(myDeck.dealOne());
                    allHandsFull = allHandsFull && h.isFull();
                }
            }

            //display hands one by one, then show the deck state
            Console.WriteLine("Hands after dealing:");
            foreach (HandOfCards h in playerHands)
            {
                Console.WriteLine("hand:");
                Console.WriteLine(h.ToString());
            }
            Console.WriteLine();    //Whitespace
            Console.WriteLine("myDeck after dealing:");
            Console.WriteLine(myDeck.ToString());
            Console.WriteLine();    //Whitespace
        }