コード例 #1
0
ファイル: Program.cs プロジェクト: bradygrapentine/cohort-21
        public void Play()
        {
            // 1.  Create a new deck
            //     PEDAC ^^^^ - Properties: A list of 52 cards
            //     Algorithm for making a list of 52 cards

            //         Make a blank list of cards
            var deck = new List <Card>();
            // var deck = new Deck();

            //         Suits is a list of "Club", "Diamond", "Heart", or "Spade"
            //         Faces is a list of 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Ace

            var suits = new List <string>()
            {
                "Clubs", "Diamonds", "Hearts", "Spades"
            };
            var ranks = new List <string>()
            {
                "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"
            };

            // Go through each suit, Clubs, Diamonds, Hearts, and Spaces, one at a time
            foreach (var suit in suits)
            {
                //     For each suit do the following
                //        Go through all the ranks, Ace, 2, 3, 4, etc.
                foreach (var rank in ranks)
                {
                    //  With the current suit and the current face, make a new card
                    var card = new Card {
                        Suit = suit, Rank = rank
                    };

                    // Add that card to the list of cards
                    deck.Add(card);
                }
            }

            // 2.  Ask the deck to make a new shuffled 52 cards
            // numberOfCards = length of our deck
            var numberOfCards = deck.Count;

            // for rightIndex from numberOfCards - 1 down to 1 do:
            for (var rightIndex = numberOfCards - 1; rightIndex >= 1; rightIndex--)
            {
                //   leftIndex = random integer that is greater than or equal to 0 and LESS than rightIndex. See the section "How do we get a random integer")
                var randomNumberGenerator = new Random();
                var leftIndex             = randomNumberGenerator.Next(rightIndex);

                //   Now swap the values at rightIndex and leftIndex by doing this:
                //     leftCard = the value from deck[leftIndex]
                var leftCard = deck[leftIndex];
                //     rightCard = the value from deck[rightIndex]
                var rightCard = deck[rightIndex];
                //     deck[rightIndex] = leftCard
                deck[rightIndex] = leftCard;
                //     deck[leftIndex] = rightCard
                deck[leftIndex] = rightCard;
            }

            // 3.  Create a player hand
            var playerHand = new Hand();

            // 4.  Create a dealer hand
            var dealerHand = new Hand();

            // 5.  Ask the deck for a card
            // var newCard = deck.Deal();
            var newCard = deck[0];

            deck.Remove(newCard);

            // 5b and place it in the player hand
            playerHand.Receive(newCard);

            // 6.  Ask the deck for a card
            newCard = deck[0];
            deck.Remove(newCard);

            // 6b. and place it in the player hand
            playerHand.Receive(newCard);


            // 7.  Ask the deck for a card and place it in the dealer hand
            newCard = deck[0];
            deck.Remove(newCard);

            dealerHand.Receive(newCard);

            // 8.  Ask the deck for a card and place it in the dealer hand
            newCard = deck[0];
            deck.Remove(newCard);

            dealerHand.Receive(newCard);


            // 10. If they have BUSTED, then goto step 15
            var keepAsking = true;

            while (keepAsking && !playerHand.Busted())
            {
                // 9.  Show the player the cards in their hand and the TotalValue of their Hand
                Console.WriteLine("Your cards are:");
                foreach (var card in playerHand.IndividualCards)
                {
                    Console.WriteLine(card.Description());
                }
                Console.Write("Your total hand value is: ");
                Console.WriteLine(playerHand.TotalValue());

                // 11. Ask the player if they want to HIT or STAND
                Console.Write("Do you want to [H]it or [S]tand? ");
                var choice = Console.ReadLine().ToLower();

                // 12. If HIT
                if (choice == "h")
                {
                    //     - Ask the deck for a card and place it in the player hand, repeat step 10
                    var hitCard = deck[0];
                    deck.Remove(hitCard);

                    playerHand.Receive(hitCard);
                }
                else
                {
                    // 13. If STAND continue on
                    keepAsking = false;
                }
            }

            Console.WriteLine("Your cards are:");
            foreach (var card in playerHand.IndividualCards)
            {
                Console.WriteLine(card.Description());
            }
            Console.Write("Your total hand value is: ");
            Console.WriteLine(playerHand.TotalValue());

            // 14. If the dealer has busted then goto step 17
            // 15. If the dealer has less than 17
            //     - Add a card to the dealer hand and go back to 14
            while (dealerHand.TotalValue() < 17 && !playerHand.Busted())
            {
                var newDealerCard = deck[0];
                deck.Remove(newDealerCard);

                dealerHand.Receive(newDealerCard);
            }

            // 16. Show the dealer's hand TotalValue
            Console.WriteLine("The dealer's cards are:");
            foreach (var card in dealerHand.IndividualCards)
            {
                Console.WriteLine(card.Description());
            }
            Console.Write("The dealer's total hand value is: ");
            Console.WriteLine(dealerHand.TotalValue());

            if (playerHand.Busted())
            {
                // 17. If the player busted show "DEALER WINS"
                Console.WriteLine("Dealer wins!");
            }
            else if (dealerHand.Busted())
            {
                // 18. If the dealer busted show "PLAYER WINS"
                Console.WriteLine("Player wins!");
            }
            else if (dealerHand.TotalValue() > playerHand.TotalValue())
            {
                // 19. If the dealer's hand is more than the player's hand then show "DEALER WINS", else show "PLAYER WINS"
                Console.WriteLine("Dealer Wins");
            }
            else if (playerHand.TotalValue() > dealerHand.TotalValue())
            {
                Console.WriteLine("Player Wins!");
            }
            else
            {
                // 20. If the value of the hands are even, show "DEALER WINS"
                Console.WriteLine("Ties to to the dealer");
            }
        }