Exemplo n.º 1
0
 private void standButton_Click(object sender, EventArgs e)
 {
     //if score is < 17
     if (dealerHand.Score < 17)
     {
         //Draw until score is <= 17
         while (dealerHand.Score <= 17)
         {
             dealerHand.AddCard(d.Deal());
             ComputerTurn(dealerHand);
         }
     }
     else if (dealerHand.IsBusted)
     {
         //If dealer score IS 21, winning condition
         hitButton.Enabled       = false;
         standButton.Enabled     = false;
         playAgainButton.Enabled = true;
         playerWinLabel.Visible  = true;
         if (MessageBox.Show("Press OK to Play Again", "Dealer had 21!", MessageBoxButtons.OKCancel) == DialogResult.OK)
         {
             Setup();
         }
     }
     else
     {
         //Else just Draw ONE CARD
         dealerHand.AddCard(d.Deal());
         ComputerTurn(dealerHand);
     }
 }
Exemplo n.º 2
0
        private void Reset()
        {
            hitButton.Enabled       = true;
            standButton.Enabled     = true;
            playerWinLabel.Visible  = false;
            dealerWinLabel.Visible  = false;
            playAgainButton.Enabled = false;

            HideAllCards();

            cards.Shuffle();

            Card c1 = cards.Deal();
            Card c2 = cards.Deal();
            Card c3 = cards.Deal();
            Card c4 = cards.Deal();

            playerHand.AddCard(c1);
            Show(card6, c1);

            dealerHand.AddCard(c2);
            Show(card1, c2);

            playerHand.AddCard(c3);
            Show(card7, c3);

            dealerHand.AddCard(c4);
            Show(card2, c4);
        }
Exemplo n.º 3
0
 public void TestingHasAce()
 {
     BJDef.AddCard(aC);
     Assert.IsTrue(BJDef.HasAce);
     BJDef.Discard(BJDef.IndexOf(aC));
     Assert.IsFalse(BJDef.HasAce);
     BJDef.AddCard(qH);
     Assert.IsFalse(BJDef.HasAce);
 }
        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.");
        }
Exemplo n.º 5
0
        public void TestIsBusted()
        {
            Card c = new Card(13, 4);

            def.AddCard(c);
            def.AddCard(c);

            Assert.True(def.IsBusted);
        }
Exemplo n.º 6
0
        static void BJHandTestIsBusted()
        {
            BJHand h1 = new BJHand();

            h1.AddCard(new Card(11, 12));
            h1.AddCard(new Card(11, 12));
            h1.AddCard(new Card(11, 12));

            Console.WriteLine("Testing Is Busted");

            Console.WriteLine("Two aces in hand. Expecting true. " + h1.IsBusted());
        }
Exemplo n.º 7
0
        static void BJHandTestHasAce()
        {
            BJHand h1 = new BJHand();

            Console.WriteLine("Testing Has Ace");

            h1.AddCard(new Card(3, 4));
            h1.AddCard(new Card(11, 12));
            Console.WriteLine("No aces in hand. Expecting false. " + h1.HasAce());

            h1.AddCard(new Card(1, 1));
            Console.WriteLine("One ace in the hand. Expecting true. " + h1.HasAce());
        }
Exemplo n.º 8
0
        static void BJHandTestScore()
        {
            BJHand h1 = new BJHand();

            h1.AddCard(new Card(11, 12));
            h1.AddCard(new Card(11, 12));
            Console.WriteLine("Testing Score");
            Console.WriteLine("Two Jacks in Hand. Expecting 20. " + h1.Score);

            h1.AddCard(new Card(1, 2));
            h1.AddCard(new Card(1, 2));
            Console.WriteLine("Testing Score");
            Console.WriteLine("Two Aces in Hand. Expecting 22. " + h1.Score);
        }
Exemplo n.º 9
0
 public void TestAddCard()
 {
     hand1.AddCard(d.DealCard());
     hand2.AddCard(d.DealCard());
     hand3.AddCard(d.DealCard());
     hand4.AddCard(d.DealCard());
     Assert.True(hand1.NumCards == 1);
     Assert.True(hand2.NumCards == 6);
     Assert.True(hand3.NumCards == 1);
     Assert.True(hand4.NumCards == 6);
 }
Exemplo n.º 10
0
        private void hitButton_Click(object sender, EventArgs e)
        {
            //Add's card to players hand
            playerHand.AddCard(d.Deal());
            PlayerTurn(playerHand);

            //if over 21, Change board and displays message
            if (playerHand.IsBusted)
            {
                hitButton.Enabled       = false;
                standButton.Enabled     = false;
                playAgainButton.Enabled = true;
                dealerWinLabel.Visible  = true;
                if (MessageBox.Show("Press OK to Play Again", "You lost!", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Setup();
                }
            }
            else
            {
                MessageBox.Show("Next Move");
            }
        }
        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();
        }
Exemplo n.º 13
0
        static void BlackJackGameTest()
        {
            // Make the deck and shuffle.
            Deck d = new Deck();

            d.Shuffle();
            // Initiate the dealer and player.
            BJHand dealer = new BJHand(d, 2);
            BJHand player = new BJHand(d, 2);
            // While playing start hitting or just stand.
            bool playing = true;

            while (playing)
            {
                if (player.Score == 21)
                {
                    Console.WriteLine("The player just scored a blackjack."); return;
                }
                else if (dealer.Score == 21)
                {
                    Console.WriteLine("The dealer just scored a blackjack."); return;
                }
                Console.WriteLine("Enter HIT to draw a card or STAND to finish up.");
                ShowResults(player, dealer);
                string sss = Console.ReadLine();
                if (sss == "HIT")
                {
                    player.AddCard(d.Draw());
                    dealer.AddCard(d.Draw());
                    if (player.Score > 20 || dealer.Score > 20)
                    {
                        playing = false;
                        ShowResults(player, dealer);
                    }
                }
                else if (sss == "STAND")
                {
                    playing = false;
                    ShowResults(player, dealer);
                }
                else
                {
                    Console.WriteLine("That wasn't a readable answer, try again.");
                }
            }
            // Game over.
            Console.WriteLine("Calculating results.");
            // If the dealer has a score less than the player and neither are busted.
            while (dealer.Score <= player.Score && !(dealer.IsBusted || player.IsBusted))
            {
                dealer.AddCard(d.Draw());
                ShowResults(player, dealer);
            }
            // Show the final results.
            if ((player.IsBusted && dealer.IsBusted) || (player.Score == dealer.Score))
            {
                Console.WriteLine("It's a tie.");
            }
            else
            {
                if (player.IsBusted)
                {
                    Console.WriteLine("The player has busted and the dealer won.");
                }
                else if (dealer.IsBusted)
                {
                    Console.WriteLine("The dealer has busted and the player won.");
                }
                else if (player.Score > dealer.Score)
                {
                    Console.WriteLine("The player has won.");
                }
                else
                {
                    Console.WriteLine("The dealer has won.");
                }
            }
        }