Exemplo n.º 1
0
 static void ShowResults(BJHand player, BJHand dealer)
 {
     Console.WriteLine("Player's hand: \n" + player.ToString());
     Console.WriteLine("Dealer's hand: \n" + dealer.ToString());
     Console.WriteLine("Player's score: " + player.Score.ToString());
     Console.WriteLine("Dealer's score: " + dealer.Score.ToString());
 }
        public static void HandClassTesting()
        {
            Console.WriteLine("TESTING THE HAND CLASS.");
            Deck d = new Deck();
            Hand h = new Hand(d, 2);

            Console.WriteLine("Created a hand of 2 cards, lets see them. \n");
            Console.WriteLine(h.ToString());
            Console.WriteLine("Let's get rid of one card from the last one drawn. \n");
            h.Discard(1);
            Console.WriteLine("Let's see the final one. \n");
            Console.WriteLine(h.GetCard(0).ToString());
            Console.WriteLine("Let's add two. \n");
            h.AddCard(new Card(2, 2));
            h.AddCard(new Card(4, 2));
            Console.WriteLine("Let's see them again. \n");
            Console.WriteLine(h.ToString());
            Console.WriteLine("Let's get the second card by index and then get the index of the card. \n");
            Card c1 = h.GetCard(1);

            Console.WriteLine("Index of second card, should be 1 and is: " + h.IndexOf(c1));
            Console.WriteLine("Now let's get the index of it by it's value and suit. \n");
            Console.WriteLine("Index should be 1 and is: " + h.IndexOf(c1.Value, c1.Suit));
            Console.WriteLine("Does the hand contain the card we've been checking? " + (h.HasCard(c1) ? "Yes" : "No"));
            Console.WriteLine("Can we find it by it's value and suit? " + (h.HasCard(c1.Value, c1.Suit) ? "Yes" : "No"));
            Console.WriteLine("TESTING COMPLETE.");

            /*
             *  ^^^^^
             * HAND CLASS TESTING
             *
             * BLACKJACK HAND CLASS TESTING
             * v v v v v
             */
            Console.WriteLine("TESTING BLACKJACK HAND.");
            Deck d2 = new Deck();

            d2.Shuffle();
            BJHand bjHand = new BJHand(d2, 3);

            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine("///////");
                Console.WriteLine("Showing results of the hand: " + bjHand.ToString());
                Console.WriteLine("Showing score of the hand: " + bjHand.Score + ", is it a bust? " + bjHand.IsBusted);
                Console.WriteLine("Emptying hand and filling it.");
                Console.WriteLine("///////");
                // Emptying hand.
                bjHand.Discard(0);
                bjHand.Discard(0);
                bjHand.Discard(0);
                bjHand.AddCard(d2.Draw());
                bjHand.AddCard(d2.Draw());
                bjHand.AddCard(d2.Draw());
            }
            Console.WriteLine("TESTING COMPLETE.");
        }
        static void Main(string[] args)
        {
            // Intro
            Console.WriteLine("BLACKJACK");
            Console.WriteLine();

            int playerWins = 0;
            int dealerWins = 0;

            // Game Loop (Round)
            do
            {
                // Reshuffle deck every round
                Deck d = new Deck();
                d.Shuffle();

                BJHand player = new BJHand(d, 2);
                BJHand dealer = new BJHand(d, 2);

                // Turn "Loop"
                // Player (then dealer), player continues until Bust or Stand
                bool stand = false;
                bool bust  = false;

                while (!stand && !bust)
                {
                    Console.WriteLine("Player");
                    Console.WriteLine(player.ToString());

                    stand = !willHit();
                    Console.WriteLine();

                    if (!stand)
                    {
                        player.AddCard(d.Deal());
                    }

                    bust = player.Score > 21;
                }

                // Dealer
                bool bustD  = false;
                bool standD = false;

                if (!bust)
                {
                    while (!standD && !bustD)
                    {
                        //Console.WriteLine("Dealer");
                        //Console.WriteLine(dealer.ToString());

                        if (dealer.Score <= 16)
                        {
                            dealer.AddCard(d.Deal());
                        }
                        else
                        {
                            standD = true;
                        }

                        bustD = dealer.Score > 21;
                    }
                }

                // Player Score and Hand
                Console.WriteLine("Player Score: " + player.Score.ToString() + (bust ? " (Bust)" : ""));
                Console.WriteLine(player.ToString());

                // Dealer Score and Hand
                Console.WriteLine("Dealer Score: " + dealer.Score.ToString() + (bustD ? " (Bust)" : ""));
                Console.WriteLine(dealer.ToString());

                // Announce winner
                if (bust || (!bustD && dealer.Score > player.Score))
                {
                    Console.WriteLine("The Dealer has won!");
                    dealerWins++;
                }
                else if (bustD || player.Score > dealer.Score)
                {
                    Console.WriteLine("The Player has won!");
                    playerWins++;
                }
                else
                {
                    Console.WriteLine("It's a draw!");
                }

                // Show running round score
                Console.WriteLine("Player/Dealer Wins: " + playerWins.ToString() + "/" + dealerWins.ToString());

                // Another game?
            } while (playAgain());

            // Exit
            Console.Write("Press any key to exit > ");
            Console.ReadKey();
        }
        private static void TestScoreIsBusted()
        {
            Random ranGen = new Random();

            // Non-Face Cards, Non-Aces
            Console.WriteLine("Testing non-face cards, non-Aces scoring...");
            Console.WriteLine();

            BJHand bjHand = new BJHand();

            bjHand.AddCard(new Card(2, ranGen.Next(1, 5)));
            bjHand.AddCard(new Card(8, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 10");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = False");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();

            bjHand.AddCard(new Card(9, ranGen.Next(1, 5)));
            bjHand.AddCard(new Card(5, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 24");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = True");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();

            ContinuePrompt();

            // Face Cards
            Console.WriteLine("Testing face card scoring ...");
            Console.WriteLine();

            bjHand = new BJHand();
            bjHand.AddCard(new Card(11, ranGen.Next(1, 5)));
            bjHand.AddCard(new Card(12, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 20");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = False");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();

            bjHand.AddCard(new Card(13, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 30");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = True");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();

            ContinuePrompt();

            Console.WriteLine("Testing ace scoring ...");
            Console.WriteLine();

            bjHand = new BJHand();
            bjHand.AddCard(new Card(1, ranGen.Next(1, 5)));
            bjHand.AddCard(new Card(10, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 21");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = False");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();

            bjHand.AddCard(new Card(1, ranGen.Next(1, 5)));

            Console.WriteLine(bjHand.ToString());

            Console.WriteLine("Expect: Score = 12");
            Console.WriteLine("Result: Score = " + bjHand.Score.ToString());
            Console.WriteLine();

            Console.WriteLine("Expect: IsBusted = False");
            Console.WriteLine("Result: IsBusted = " + bjHand.IsBusted.ToString());
            Console.WriteLine();
        }