Example #1
0
        public void addCard(Deck deck)
        {
            Random rng    = new Random();
            int    num    = rng.Next(deck.getDeckSize());
            Card   select = deck.getCard(num);

            playerHand.Add(new Card(select.getName(), select.getSuit(), select.getValue()));
            deck.removeCard(select);
        }
Example #2
0
/********************************************************************************************************************************************************
******** Function: playHand
******** Description: plays a hand of blackjack, allowing user to choose when to hit, dealer hits until they reach 17
******** Parameters: deck object and an integer for the amount the user is betting
******** Pre-Conditions: user has starting capital and other game variables initialized, deck object has been created
******** Post-Conditions: flow of gameplay is printed to screen
********************************************************************************************************************************************************/

        public void playHand(Deck deck, int bet)
        {
            //if we get to the halfway point of the deck then it is shuffled and the index is reset to avoid indexing out of range
            if (deckIdx > 25)
            {
                deck.shuffleDeck();
                deckIdx = 0;
            }

            bool playerBust = false;
            bool addCard    = true;
            char input;

            //the 4 cards the begin a blackjack hand are drawn and assigned
            Card dealerDraw    = deck.drawCard(deckIdx++);
            Card playerDraw    = deck.drawCard(deckIdx++);
            Card dealerDrawTwo = deck.drawCard(deckIdx++);
            Card playerDrawTwo = deck.drawCard(deckIdx++);

            //next draw variable to be used for continued draws from the deck
            Card nextDraw;

            //sets the scores of the dealer and the player
            playerScore = playerDraw.getValue() + playerDrawTwo.getValue();
            dealerScore = dealerDraw.getValue() + dealerDrawTwo.getValue();

            //displays one of the dealer cards to console
            Console.WriteLine("The dealer has drawn a " + dealerDrawTwo.getName());
            Console.WriteLine("Their first card is hidden");
            Console.WriteLine();

            //displays the two cards player has received on their draws as well as their current score
            Console.WriteLine("Your hand: ");
            Console.WriteLine(playerDraw.getName());
            Console.WriteLine(playerDrawTwo.getName());
            Console.WriteLine();

            Console.WriteLine("Your current score is " + playerScore);
            do
            {
                //asks the player if the would like to add another card to try and increase their score
                Console.WriteLine("Would you like to add another card? [y/n]");
                input = Console.ReadKey().KeyChar;
                Console.WriteLine();
                Console.WriteLine();

                //if player wants another card it is drawn from the deck and both scores are now displayed
                if (Char.ToLower(input) == 'y')
                {
                    nextDraw     = deck.drawCard(deckIdx++);
                    playerScore += nextDraw.getValue();
                    Console.WriteLine("You have drawn a " + nextDraw.getName());
                    Console.WriteLine("Player Score: " + playerScore);
                    Console.WriteLine("Dealer Score: " + dealerScore);
                    Console.WriteLine();

                    //if the players hand now exceeds 21 they have lost this hand to the dealer
                    if (playerScore > 21)
                    {
                        Console.WriteLine("You have busted, the dealer wins");
                        playerEarnings -= bet;
                        Console.WriteLine("Total bankroll: $" + playerEarnings);
                        Console.WriteLine();
                        playerBust = true;
                        addCard    = false;
                    }
                }

                //breaks out of loop if player does not want any more cards
                else if (Char.ToLower(input) == 'n')
                {
                    addCard = false;
                }
                else
                {
                    Console.WriteLine("Invalid response, please enter 'y' to add another card or 'n' to stay");
                }
            } while (input != Char.ToLower('y') && input != Char.ToLower('n') || addCard == true);

            //only continues to dealer if the player has not busted, meaning exceeded 21
            if (playerBust == false)
            {
                //the dealer will not aquire more cards if they have at least a 17
                if (dealerScore >= 17)
                {
                    Console.WriteLine();
                    evalWin(playerScore, dealerScore, bet); //evaluates both scores
                }
                else
                {
                    //continues to draw cards and display current scores while the dealer has a score below 17
                    while (dealerScore < 17)
                    {
                        nextDraw     = deck.drawCard(deckIdx++);
                        dealerScore += nextDraw.getValue();
                        Console.WriteLine("The dealer has drawn a " + nextDraw.getName());
                        Console.WriteLine("Player Score: " + playerScore);
                        Console.WriteLine("Dealer Score: " + dealerScore);
                        Console.WriteLine();

                        //if dealer accidently exceeds 21 then the player wins the hand
                        if (dealerScore > 21)
                        {
                            Console.WriteLine("The dealer has busted, you have won this hand!");
                            playerEarnings += bet;
                            Console.WriteLine("Total bankroll: $" + playerEarnings);
                            Console.WriteLine();
                        }
                    }
                    //evaluates both scores after dealer has aquired more cards but only if they have not exceeded 21
                    if (dealerScore <= 21)
                    {
                        evalWin(playerScore, dealerScore, bet);
                    }
                }
            }
        }