public void Test_BlackjackHand_Score_Example5()
        {
            BlackjackHand hand1 = new BlackjackHand();

            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Ace));
            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Jack));
            Assert.AreEqual(21, hand1.Score);
        }
        public void Test_BlackjackHand_Score_Example6()
        {
            BlackjackHand hand1 = new BlackjackHand();

            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Ace));
            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Four));
            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Seven));
            Assert.AreEqual(12, hand1.Score);
        }
        public void Test_BlackjackHand_Score_Example9()
        {
            BlackjackHand hand1 = new BlackjackHand();

            hand1.AddCard(new Card(Suit.Diamonds, FaceValue.Ace));
            hand1.AddCard(new Card(Suit.Spades, FaceValue.Ace));
            hand1.AddCard(new Card(Suit.Hearts, FaceValue.Ace));
            Assert.AreEqual(13, hand1.Score);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The entire code lives inside the main method
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            /* Declare variables for and create a deck of cards and blackjack hands
             * for the dealer and the player */
            Deck          deck = new Deck();
            BlackjackHand blackjackHandDealer = new BlackjackHand("Dealer");
            BlackjackHand blackjackHandPlayer = new BlackjackHand("Player");

            // The welcome message
            Console.WriteLine("Welcome to the game! The program will play a single hand of Blackjack with you.");
            Console.WriteLine();

            // Shuffle the deck of cards
            deck.Shuffle();

            // Deal 2 cards to the player and dealer
            blackjackHandDealer.AddCard(deck.TakeTopCard());
            blackjackHandDealer.AddCard(deck.TakeTopCard());
            blackjackHandPlayer.AddCard(deck.TakeTopCard());
            blackjackHandPlayer.AddCard(deck.TakeTopCard());

            // Make all the player’s cards face up
            blackjackHandPlayer.ShowAllCards();

            // Make the dealer’s first card face up (the second card is the dealer’s “hole” card)
            blackjackHandDealer.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand
            blackjackHandPlayer.Print();
            blackjackHandDealer.Print();
            Console.WriteLine();

            // Let the player hit if they want to
            blackjackHandPlayer.HitOrNot(deck);
            Console.WriteLine();

            // Make all the dealer’s cards face up; there's a method for this in the BlackjackHand class
            blackjackHandDealer.ShowAllCards();

            // Print both the player’s hand and the dealer’s hand
            blackjackHandPlayer.Print();
            blackjackHandDealer.Print();
            Console.WriteLine();

            // Print the scores for both hands
            int playerScore = blackjackHandPlayer.Score;
            int dealerScore = blackjackHandDealer.Score;

            Console.WriteLine("The player's score is " + playerScore);
            Console.WriteLine("The dealer's score is " + dealerScore);
            Console.WriteLine();
        }
        public void Test_BlackjackHand_Score_Example1()
        {
            BlackjackHand hand1 = new BlackjackHand();

            hand1.AddCard(new Card(Suit.Clubs, FaceValue.Two));
            Assert.AreEqual(2, hand1.Score);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Hit
        /// </summary>
        /// <param name="HandNum">Player Hand num</param>
        public static void Hit(int HandNum)
        {
            BlackjackHand playerHandNum = PlayerHands[HandNum];

            /*
             * // if the score is atlest 21
             * if (playerHandNum.Score > 21 || playerHandNum.Score == 21) {
             *  // Is standing automaticaly True
             *  playerHandNum.IsStanding = true;
             * } else {
             *  playerHandNum.IsStanding = false;
             * }
             */

            // draw one Card
            playerHandNum.AddCard(_deck.DealOneCard());


            if (playerHandNum.Score > 21 || playerHandNum.Score == 21)
            {
                // Is standing automaticaly True
                playerHandNum.IsStanding = true;
            }
            else
            {
                playerHandNum.IsStanding = false;
            }
        }
        /// <summary>
        /// Let you play a hand of Blackjack
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            //create deck and two hands
            Deck deck = new Deck();
            BlackjackHand dealerHand = new BlackjackHand("Dealer");
            BlackjackHand playerHand = new BlackjackHand("Player");

            //print welcome message
            Console.WriteLine("Welcome friend!");
            Console.WriteLine("You are now going to play a single hand of Blackjack\n");

            //shuffle the deck
            deck.Shuffle();

            //deal two cards for player
            playerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());

            //deal two cards for dealer
            dealerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());

            //make all player's cards face up
            playerHand.ShowAllCards();

            //make first dealer's card face up
            dealerHand.ShowFirstCard();

            //print both hands
            playerHand.Print();
            dealerHand.Print();

            //ask player if he wants to "hit"
            playerHand.HitOrNot(deck);

            //make all dealer's card face up
            dealerHand.ShowAllCards();

            //print both hands
            playerHand.Print();
            dealerHand.Print();

            //print scores
            Console.WriteLine("Player score: {0}", playerHand.Score);
            Console.WriteLine("Dealer score: {0}", dealerHand.Score);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Let you play a hand of Blackjack
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            //create deck and two hands
            Deck          deck       = new Deck();
            BlackjackHand dealerHand = new BlackjackHand("Dealer");
            BlackjackHand playerHand = new BlackjackHand("Player");

            //print welcome message
            Console.WriteLine("Welcome friend!");
            Console.WriteLine("You are now going to play a single hand of Blackjack\n");

            //shuffle the deck
            deck.Shuffle();

            //deal two cards for player
            playerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());

            //deal two cards for dealer
            dealerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());

            //make all player's cards face up
            playerHand.ShowAllCards();

            //make first dealer's card face up
            dealerHand.ShowFirstCard();

            //print both hands
            playerHand.Print();
            dealerHand.Print();

            //ask player if he wants to "hit"
            playerHand.HitOrNot(deck);

            //make all dealer's card face up
            dealerHand.ShowAllCards();

            //print both hands
            playerHand.Print();
            dealerHand.Print();

            //print scores
            Console.WriteLine("Player score: {0}", playerHand.Score);
            Console.WriteLine("Dealer score: {0}", dealerHand.Score);
        }
Exemplo n.º 9
0
        /// <summary>
        /// A single hand of Blackjack
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            //Declare variables for and create a deck of cards and blackjack hands for the dealer and the player

            Deck          deck       = new Deck();
            BlackjackHand dealerHand = new BlackjackHand("Dealer");
            BlackjackHand playerHand = new BlackjackHand("Player");

            //Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack

            Console.WriteLine("You will play a single hand of Blackjack.");

            // Shuffle the deck of cards

            deck.Shuffle();

            //Deal 2 cards to the player and dealer
            dealerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());

            //Show all the player’s cards

            playerHand.ShowAllCards();

            //Show the dealer’s first card

            dealerHand.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand

            playerHand.Print();
            dealerHand.Print();

            //Let the player hit if they want to

            playerHand.HitOrNot(deck);

            //Show all the dealer’s cards

            dealerHand.ShowAllCards();

            //Print both the player’s hand and the dealer’s hand

            playerHand.Print();
            dealerHand.Print();

            //Print the scores for both hands

            Console.WriteLine("Player's score: " + playerHand.Score + ".");
            Console.WriteLine("Dealer's score: " + dealerHand.Score + ".");
        }
Exemplo n.º 10
0
        /// <summary> 
        /// A single hand of Blackjack
        /// </summary> 
        /// <param name="args">command-line args</param> 
        static void Main(string[] args)
        {
            //Declare variables for and create a deck of cards and blackjack hands for the dealer and the player

            Deck deck = new Deck();
            BlackjackHand dealerHand = new BlackjackHand("Dealer");
            BlackjackHand playerHand = new BlackjackHand("Player");

            //Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack

            Console.WriteLine("You will play a single hand of Blackjack.");

            // Shuffle the deck of cards

            deck.Shuffle();

            //Deal 2 cards to the player and dealer
            dealerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());

            //Show all the player’s cards

            playerHand.ShowAllCards();

            //Show the dealer’s first card

            dealerHand.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand

            playerHand.Print();
            dealerHand.Print();

            //Let the player hit if they want to

            playerHand.HitOrNot(deck);

            //Show all the dealer’s cards

            dealerHand.ShowAllCards();

            //Print both the player’s hand and the dealer’s hand

            playerHand.Print();
            dealerHand.Print();

            //Print the scores for both hands

            Console.WriteLine("Player's score: " + playerHand.Score + ".");
            Console.WriteLine("Dealer's score: " + dealerHand.Score + ".");
        }
Exemplo n.º 11
0
        /// <summary>
        /// Double
        /// </summary>
        /// <param name="HandNum">Player Hand num</param>
        public static void Double(int HandNum)
        {
            BlackjackHand playerHandNum = PlayerHands[HandNum];

            //automatic standing
            playerHandNum.IsStanding = true;

            // add one card to hand
            playerHandNum.AddCard(_deck.DealOneCard());

            // substract fund
            PlayerFunds -= playerHandNum.Bet;

            // double the bet
            playerHandNum.Bet += playerHandNum.Bet;
        }
        public void Test_BlackjackHand_Score_Example3()
        {
            BlackjackHand hand1 = new BlackjackHand();

            hand1.AddCard(new Card(Suit.Clubs, FaceValue.Jack));

            BlackjackHand hand2 = new BlackjackHand();

            hand2.AddCard(new Card(Suit.Hearts, FaceValue.Queen));

            BlackjackHand hand3 = new BlackjackHand();

            hand3.AddCard(new Card(Suit.Spades, FaceValue.King));

            Assert.AreEqual(10, hand1.Score);
            Assert.AreEqual(10, hand2.Score);
            Assert.AreEqual(10, hand3.Score);
        }
Exemplo n.º 13
0
        /// <summary>
        /// make a new round
        /// </summary>
        /// <param name="initialBet"> The bet </param>
        public static void NewRound(int initialBet)
        {
            // make a full deck and shuffle it
            _deck = new CardPile(true);
            _deck.ShufflePile();

            // reduce the player fund
            PlayerFunds -= initialBet;


            DealerHand = new BlackjackHand();

            // make a new player hand
            PlayerHands = new List <BlackjackHand>();
            PlayerHands.Add(new BlackjackHand(initialBet));


            // deals  2 card to the player and the dealer
            for (int i = 0; i < 2; i++)
            {
                PlayerHands[0].AddCard(_deck.DealOneCard());
                DealerHand.AddCard(_deck.DealOneCard());
            }
        }
Exemplo n.º 14
0
        static bool PlayBlackjack()
        {
            var deck = new Deck();

            deck.Shuffle();

            var dealerHand = new BlackjackHand();
            var userHand   = new BlackjackHand();

            dealerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());
            Console.WriteLine("\nDealer's face card is " + dealerHand.GetCard(0));

            userHand.AddCard(deck.DealCard());
            userHand.AddCard(deck.DealCard());
            Console.Write("\nUser has" + " " + userHand.GetBlackjackValue() + "\n");
            userHand.DisplayCards();

            if (userHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("\nCongratulations you win!");
                userHand.Clear();
                return(true);
            }
            else if (dealerHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("\n You Lose");
                userHand.Clear();
                return(false);
            }

            while (true)
            {
                if (userHand.GetBlackjackValue() < 21)
                {
                    int choice = GetChoice();
                    switch (choice)
                    {
                    case 1:
                    {
                        userHand.AddCard(deck.DealCard());
                        userHand.DisplayCards();
                        break;
                    }

                    case 2:
                    {
                        Console.Write("\n" + userHand.GetBlackjackValue());
                        break;
                    }

                    default:
                    {
                        Console.WriteLine("Please enter a valid choice");
                        break;
                    }
                    }
                }
                else
                {
                    Console.WriteLine("You lose!");
                    Console.WriteLine(dealerHand.GetBlackjackValue());
                    return(false);
                }
                // Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                while (dealerHand.GetBlackjackValue() <= 16)
                {
                    dealerHand.AddCard(deck.DealCard());
                    // Console.Write("\nDealer has" + " " + dealerHand.GetCardCount() + " ");

                    if (dealerHand.GetBlackjackValue() > 21)
                    {
                        Console.WriteLine("\nDealer busts... User wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(true);
                    }
                    else if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue())
                    {
                        Console.WriteLine("\nDealer Wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(false);
                    }
                    else if (userHand.GetBlackjackValue() >= dealerHand.GetBlackjackValue())
                    {
                        Console.WriteLine("\nUser wins!");
                        Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21");
                        return(true);
                    }
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Blackjack game in console
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //Declare varables for and create a deck of cards and blackjack hands for the dealer and the player
            Deck          deck0      = new Deck();
            BlackjackHand playerHand = new BlackjackHand("Player");
            BlackjackHand dealerHand = new BlackjackHand("Dealer");

            //Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack
            Console.WriteLine("Welcome to Blackjack game (Single hand version)\n");

            //Shuffle the deck of cards
            deck0.Shuffle();

            //Deal 2 cards to the player and dealer
            playerHand.AddCard(deck0.TakeTopCard());
            dealerHand.AddCard(deck0.TakeTopCard());
            playerHand.AddCard(deck0.TakeTopCard());
            dealerHand.AddCard(deck0.TakeTopCard());

            // Make all the player’s cards face up (you need to see what you have!); there's a method for this in the BlackjackHand class
            playerHand.ShowAllCards();

            //Make the dealer’s first card face up (the second card is the dealer’s “hole” card); there's a method for this in the BlackjackHand class
            dealerHand.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand
            playerHand.Print();
            dealerHand.Print();

            //Let the player hit if they want to
            playerHand.HitOrNot(deck0);

            //(ignore this) experimental code - looping invalid input (I only realised there's a HitOrNot meathod for this after wasting the time)
            ConsoleKeyInfo key;
            int            i = 1;

            while (i == 1)
            {
                i--;
                if (key.Key == ConsoleKey.Enter)
                {
                    Console.Write("\nDo you want to hit? (y/n):");
                    key = Console.ReadKey();
                    i++;
                }
                else
                {
                    if (key.Key == ConsoleKey.Y)
                    {
                        Console.WriteLine("\nyes");
                    }
                    else if (key.Key == ConsoleKey.N)
                    {
                        Console.WriteLine("\nno");
                    }
                    else
                    {
                        Console.Write("\nDo you want to hit? (y/n):");
                        key = Console.ReadKey();
                        i++;
                    }
                }
            }

            //Make all the dealer’s cards face up; there's a method for this in the BlackjackHand class
            dealerHand.ShowAllCards();

            //Print both the player’s hand and the dealer’s hand
            playerHand.Print();
            dealerHand.Print();

            //Print the scores for both hands
            Console.WriteLine("Your score is:" + playerHand.Score);
            Console.WriteLine("The dealer's score is:" + dealerHand.Score);

            //(Ignore this) Calculate who won
            if (playerHand.Score == 21)
            {
                Console.WriteLine("Blackjack! You won!");
            }
            else if (playerHand.Score > 21 || playerHand.Score < dealerHand.Score)
            {
                Console.WriteLine("You lost!");
            }
            else if (playerHand.Score == dealerHand.Score)
            {
                Console.WriteLine("Push!");
            }
            else
            {
                Console.WriteLine("You won!");
            }
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            //Declare variables for and create a deck of cards and blackjack hands for the dealer and the player
            Deck          deck       = new Deck();
            BlackjackHand playerHand = new BlackjackHand("Player");
            BlackjackHand dealerHand = new BlackjackHand("Dealer");

            //Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack
            Console.WriteLine("Hi Player, let's play one hand of BlackJack.");

            //Shuffle the deck of cards
            deck.Shuffle();

            //Deal 2 cards to the player and dealer
            playerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());

            //Make all the player’s cards face up (you need to see what you have!); there's a method for this in the BlackjackHand class
            playerHand.ShowAllCards();

            //Make the dealer’s first card face up (the second card is the dealer’s “hole” card); there's a method for this in the BlackjackHand class
            dealerHand.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand
            Console.Write("Player: ");
            playerHand.Print();
            Console.Write("Dealer: ");
            dealerHand.Print();

            //Let the player hit if they want to
            playerHand.HitOrNot(deck);

            //Make all the dealer’s cards face up; there's a method for this in the BlackjackHand class
            dealerHand.ShowAllCards();

            //Print both the player’s hand and the dealer’s hand
            Console.Write("Player: ");
            playerHand.Print();
            Console.Write("Dealer: ");
            dealerHand.Print();

            //Print the scores for both hands
            Console.WriteLine("Player score: " + playerHand.Score);
            Console.WriteLine("Dealer score: " + dealerHand.Score);
            Console.WriteLine();

            //Print win message
            if (playerHand.Score <= 21 &&
                playerHand.Score > dealerHand.Score)
            {
                Console.WriteLine("Yahooooo!!! Player won!");
            }
            else if (dealerHand.Score <= 21 &&
                     playerHand.Score < dealerHand.Score)
            {
                Console.WriteLine("Dealer won the game :(");
            }
            else
            {
                Console.WriteLine("Draw.");
            }

            //Print blank line
            Console.WriteLine();
        }
Exemplo n.º 17
0
        static bool PlayBlackjack()
        {
            Deck deck = new Deck();

            deck.Shuffle();

            //userHand and dealerHand
            BlackjackHand userHand   = new BlackjackHand();
            BlackjackHand dealerHand = new BlackjackHand();

            userHand.AddCard(deck.DealCard());
            userHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());

            if (dealerHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("Dealer's cards are: ");
                dealerHand.Display();
                Console.WriteLine("Your cards are: ");
                userHand.Display();
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");
                return(false);
            }
            if (userHand.GetBlackjackValue() == 21)
            {
                Console.WriteLine("Dealer's cards are: ");
                dealerHand.Display();
                Console.WriteLine(" \nYour cards are: ");
                userHand.Display();
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win.");
                return(true);
            }

            string hitOrStand;

            while (true)
            {
                Console.WriteLine(" \nYour cards are: ");
                userHand.Display();
                Console.WriteLine($"Your total is {userHand.GetBlackjackValue()}");
                Console.WriteLine("\nDealer is showing: ");
                dealerHand.Display(dealerHand.GetCard(0));
                Console.WriteLine();

                hitOrStand = GetHitOrStand();
                if (hitOrStand == "S" || hitOrStand == "s")
                {
                    Console.WriteLine("\nUser stands.");
                    break;
                }
                else
                {
                    userHand.AddCard(deck.DealCard());
                    if (userHand.GetBlackjackValue() > 21)
                    {
                        Console.WriteLine("User lose! Your cards are: ");
                        userHand.Display();
                        Console.WriteLine("Dealer's cards are: ");
                        dealerHand.Display();
                        Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");

                        return(false);
                    }
                }
            }
            Console.WriteLine("Dealer's cards are: ");
            dealerHand.Display();
            Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()}.");

            while (dealerHand.GetBlackjackValue() <= 16)
            {
                Console.WriteLine("Dealer hits and gets a card: ");
                dealerHand.AddCard(deck.DealCard());
                dealerHand.Display(dealerHand.GetCard(dealerHand.GetCardCount() - 1));
                if (dealerHand.GetBlackjackValue() > 21)
                {
                    Console.WriteLine("Dealer busted by going over 21. You win.");
                    return(true);
                }
            }
            if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue())
            {
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose.");
                return(false);
            }
            else
            {
                Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win.");
                return(true);
            }
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            //Declare variables for and create a deck of cards and blackjack hands for the dealer and the player
            // create a new deck
            Console.WriteLine("----- A New deck Shuffled-----");
            Deck deck = new Deck();

            //only for check:
            //deck.Print();
            Console.WriteLine();

            //blackjack hands for the dealer
            BlackjackHand dealerHand = new BlackjackHand("dealer");

            //blackjack hands for the player
            BlackjackHand playerHand = new BlackjackHand("player");

            // Print a “welcome” message to the user telling them that the program will play a single hand of Blackjack
            Console.WriteLine("Welcome. This program will play a single hand of Blackjack");

            Console.WriteLine();

            // suffle the deck
            deck.Shuffle();

            //Take top card from deck
            Card drawCard = deck.TakeTopCard();

            //drawCard.FlipOver();
            //Console.WriteLine(drawCard.Rank + " of " + drawCard.Suit);
            // Deal 2 cards to the player and dealer
            // add card to dealer hand
            dealerHand.AddCard(drawCard);
            //Take top card from deck & add to player hand
            drawCard = deck.TakeTopCard();
            playerHand.AddCard(drawCard);
            //Take top card from deck & add to dealer hand
            drawCard = deck.TakeTopCard();
            dealerHand.AddCard(drawCard);
            //Take top card from deck & add to player hand
            drawCard = deck.TakeTopCard();
            playerHand.AddCard(drawCard);

            //Make all the player’s cards face up
            playerHand.ShowAllCards();

            //Make the dealer’s first card face up
            dealerHand.ShowFirstCard();

            //Print both the player’s hand and the dealer’s hand
            playerHand.Print();
            dealerHand.Print();

            //Ask player if they want to stay or hit
            playerHand.HitOrNot(deck);
            //Print player’s hand
            playerHand.Print();

            //Make the dealer’s all cards face up and print
            dealerHand.ShowAllCards();
            dealerHand.Print();

            //print scores
            Console.WriteLine("---- Score ----");
            Console.WriteLine("Player: " + playerHand.Score + " Dealer: " + dealerHand.Score);

            Console.WriteLine();
        }
        public void BlackjackTest(int _)
        {
            // Arrange new deck
            var cards = new List <BlackjackCard>(52);

            for (int suit = 0; suit < 4; suit++)
            {
                for (int value = 1; value <= 13; value++)
                {
                    cards.Add(new BlackjackCard((CardSuit)suit, value));
                }
            }
            var deck = new Deck <BlackjackCard>(cards);

            // Assert new deck
            Assert.AreEqual(52, deck.TotalCards, "Incorrect total number of cards in the initial deck.");
            Assert.AreEqual(52, deck.RemainingCards, "Incorrect remaining number of cards in the initial deck.");
            Assert.IsTrue(cards.SequenceEqual(deck.Cards));

            // Act 1
            deck.Shuffle();
            List <BlackjackCard> handOfCards = deck.DealHand(2);
            var hand = new BlackjackHand(handOfCards);

            // Assert new hand
            Assert.AreEqual(52, deck.TotalCards, "Incorrect total number of cards in the deck.");
            Assert.AreEqual(50, deck.RemainingCards, "Incorrect remaining number of cards in the deck.");
            Assert.IsTrue(handOfCards.SequenceEqual(hand.Cards));

            int expectedScore = handOfCards[0].MaxValue + handOfCards[1].MaxValue;

            if (handOfCards[0].IsAce && handOfCards[1].IsAce)
            {
                expectedScore = 12;
            }
            Console.WriteLine($"Initial Score: {hand.Score()}. Cards in hand: {hand}");

            Assert.AreEqual(expectedScore, hand.Score(), "Incorrect score calculated.");
            Assert.AreEqual(expectedScore > 21, hand.IsBusted, "Check IsBusted failed.");
            Assert.AreEqual(expectedScore == 21, hand.Is21, "Check Is21 failed.");
            Assert.AreEqual(expectedScore == 21, hand.IsBlackjack, "Check IsBlackjack failed.");

            // Act 2
            int count = 1;

            while (!hand.IsBusted && !hand.Is21)
            {
                BlackjackCard newCard = deck.DealCard();
                hand.AddCard(newCard);
                handOfCards.Add(newCard);

                // Assert updated hand
                Assert.AreEqual(52, deck.TotalCards, "Incorrect total number of cards in the deck.");
                Assert.AreEqual(50 - count, deck.RemainingCards, "Incorrect remaining number of cards in the deck.");
                Assert.IsTrue(handOfCards.SequenceEqual(hand.Cards));

                int  expectedMinScore = 0;
                int  expectedMaxScore = 0;
                bool aceFound         = false;
                foreach (BlackjackCard card in handOfCards)
                {
                    if (!aceFound && card.IsAce)
                    {
                        expectedMaxScore += card.MaxValue;
                        aceFound          = true;
                    }
                    else
                    {
                        expectedMaxScore += card.Value;
                    }
                    expectedMinScore += card.Value;
                }
                expectedScore = expectedMaxScore > 21 ? expectedMinScore : expectedMaxScore;
                Console.WriteLine($"Round {count} Score: {hand.Score()}. Cards in hand: {hand}");

                Assert.AreEqual(expectedScore, hand.Score(), "Incorrect score calculated.");
                Assert.AreEqual(expectedScore > 21, hand.IsBusted, "Check IsBusted failed.");
                Assert.AreEqual(expectedScore == 21, hand.Is21, "Check Is21 failed.");
                Assert.IsFalse(hand.IsBlackjack, "Check IsBlackjack failed.");

                count++;
            }

            // Act 3
            deck.ResetDeck();

            // Assert after deck reset
            Assert.AreEqual(52, deck.TotalCards, "Incorrect total number of cards after deck reset.");
            Assert.AreEqual(52, deck.RemainingCards, "Incorrect remaining number of cards after deck reset.");
        }
Exemplo n.º 20
0
        /// <summary>
        /// Console application that plays Blackjack
        /// </summary>
        /// <param name="args">command-line args</param>

        static void Main(string[] args)
        {
            // Create a deck of cards
            Deck deck = new Deck();

            // Create blackjack hands for the dealer and the player
            BlackjackHand playerHand = new BlackjackHand("Player");
            BlackjackHand dealerHand = new BlackjackHand("Dealer");

            // Print a “welcome” message to the user
            Console.WriteLine("Welcome player. The program will play a single hand of Blackjack.");
            Console.WriteLine();

            // Shuffle the deck of cards
            deck.Shuffle();

            // Deal 2 cards to the player and dealer
            // Otherwise
            // Card card; // Create a card
            // card = deck.TakeTopCard();
            // playerHand.AddCard(card);
            // card = deck.TakeTopCard();
            // playerHand.AddCard(card);
            // card = deck.TakeTopCard();
            // dealerHand.AddCard(card);
            // card = deck.TakeTopCard();
            // dealerHand.AddCard(card);
            playerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());
            playerHand.AddCard(deck.TakeTopCard());
            dealerHand.AddCard(deck.TakeTopCard());

            // Make all the player’s cards face up
            playerHand.ShowAllCards();

            //Make the dealer’s first card face up
            dealerHand.ShowFirstCard();

            // Print both the player’s hand and the dealer’s hand
            playerHand.Print();
            dealerHand.Print();

            // Let the player hit if they want to
            // Otherwhise
            // Console.Write("Hit or stand? (h or s): ");
            // char answer = Console.ReadLine()[0];
            // Console.WriteLine();
            //
            // if (answer == 'h')
            // {
            //     card = deck.TakeTopCard();
            //     playerHand.AddCard(card);
            //     playerHand.ShowAllCards();
            // }
            playerHand.HitOrNot(deck);

            // Make all the dealer’s cards face up
            dealerHand.ShowAllCards();

            // Print both the player’s hand and the dealer’s hand
            playerHand.Print();
            dealerHand.Print();

            // Print the scores for both hands
            // Otherwhise
            // int playerScore = playerHand.Score;
            // Console.WriteLine("The player score is: " + playerScore);
            // int dealerScore = dealerHand.Score;
            // Console.WriteLine("The dealer score is: " + dealerScore);
            Console.WriteLine("The player score is: " + playerHand.Score);
            Console.WriteLine("The dealer score is: " + dealerHand.Score);
            Console.WriteLine();

            Console.ReadKey();
        }