Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Blackjack is a card game. The player with the highest score at the end of the ga

ID: 3666875 • Letter: B

Question

Blackjack is a card game. The player with the highest score at the end of the game, as long as that score does not exceed 21 points. Write a program that gives the user a chance to play Blackjack. The human user is one player and the dealer is the computer (well, your program). The simplified game goes along the following steps: A ¢ Two cards are given and displayed to the user. (Your program needs to display the two cards. It also needs to store the value of these as score for the user.) Two cards are given to the dealer, but these are secret and not displayed to the user. (Your program still needs to store these values as the code for the dealer.) The user is prompted whether they would like an additional card. They can request as many additional cards as they would like, until they choose to "stop". If the value of the cards exceeds 21 at any point during this process, the user has "busted" and lost, and the game ends. If the user asks to get another card, the card should be displayed on the screen. (This should be done inside a loop and that continues as long as 1) user's score is not greater than 21,2) user opts to get another card, 3-4) additional conditions from the next bullet.) The computer dealer decides whether to request as many additional cards as it wants. Your ''silly" dealer always asks for two additional cards. If the computer dealer's cards exceed 21 points during this process, the dealer has busted and lost, and the user wins. The card should be displayed on the screen. If the dealer asks for another card, the card should be displayed on the screen. (This should be done in the same loop as the previous bullet. It adds two more conditions to the loop continuation condition 3) the dealer opts to get another card, 4) the dealer's score is not greater than 21). At the end of the game, if either the user or the dealer ''busted", then the other is the winner if nobody ''busted", then if the user's score is higher than the dealer's score, the user wins, otherwise, the dealer wins. Call your file: BlackjackGame. java.

Explanation / Answer

To implement BlackJack card game we have used the following algorithm

Create and shuffle a deck of cards

Create two BlackjackHands, userHand and dealerHand

Deal two cards into each hand

         if dealer has blackjack

             User loses and the game ends now

        If user has blackjack

             User wins and the game ends now

         Repeat:

             Ask whether user wants to hit or stand

             if user stands:

                 break out of loop

             if user hits:

                 Give user a card

                 if userHand.getBlackjackValue() > 21:

                     User loses and the game ends now

         while dealerHand.getBlackJackValue() <= 16 :

             Give dealer a card

             if dealerHand.getBlackjackValue() > 21:

                 User wins and game ends now

         if dealerHand.getBlackjackValue() >= userHand.getBlackjackValue()

             User loses

         else

             User wins

Program Code:

/*

       This program lets the user play Blackjack. The computer

       acts as the dealer. The user has a stake of $100, and

       makes a bet on each game. The user can leave at any time,

       or will be kicked out when he loses all the money.

      

    */

   

    public class Blackjack {

   

       public static void main(String[] args) {

      

          int money;          // Amount of money the user has.

          int bet;            // Amount user bets on a game.

          boolean userWins;   // Did the user win the game?

         

          TextIO.putln("Welcome to the game of blackjack.");

          TextIO.putln();

         

          money = 100; // User starts with $100.

      

          while (true) {

              TextIO.putln("You have " + money + " dollars.");

              do {

                 TextIO.putln("How many dollars do you want to bet? (Enter 0 to end.)");

                 TextIO.put("? ");

                 bet = TextIO.getlnInt();

                 if (bet < 0 || bet > money)

                     TextIO.putln("Your answer must be between 0 and " + money + '.');

              } while (bet < 0 || bet > money);

              if (bet == 0)

                 break;

              userWins = playBlackjack();

              if (userWins)

                 money = money + bet;

              else

                 money = money - bet;

              TextIO.putln();

              if (money == 0) {

                 TextIO.putln("Looks like you've are out of money!");

                 break;

              }

          }

         

          TextIO.putln();

          TextIO.putln("You leave with $" + money + '.');

      

       } // end main()

      

      

       static boolean playBlackjack() {

             // Let the user play one game of Blackjack.

             // Return true if the user wins, false if the user loses.

   

          Deck deck;                  // A deck of cards. A new deck for each game.

          BlackjackHand dealerHand;   // The dealer's hand.

          BlackjackHand userHand;     // The user's hand.

         

          deck = new Deck();

          dealerHand = new BlackjackHand();

          userHand = new BlackjackHand();

   

          /* Shuffle the deck, then deal two cards to each player. */

         

          deck.shuffle();

          dealerHand.addCard( deck.dealCard() );

          dealerHand.addCard( deck.dealCard() );

          userHand.addCard( deck.dealCard() );

          userHand.addCard( deck.dealCard() );

         

          TextIO.putln();

          TextIO.putln();

         

          /* Check if one of the players has Blackjack (two cards totaling to 21).

             The player with Blackjack wins the game. Dealer wins ties.

          */

         

          if (dealerHand.getBlackjackValue() == 21) {

               TextIO.putln("Dealer has the " + dealerHand.getCard(0)

                                       + " and the " + dealerHand.getCard(1) + ".");

               TextIO.putln("User has the " + userHand.getCard(0)

                                         + " and the " + userHand.getCard(1) + ".");

               TextIO.putln();

               TextIO.putln("Dealer has Blackjack. Dealer wins.");

               return false;

          }

         

          if (userHand.getBlackjackValue() == 21) {

               TextIO.putln("Dealer has the " + dealerHand.getCard(0)

                                       + " and the " + dealerHand.getCard(1) + ".");

               TextIO.putln("User has the " + userHand.getCard(0)

                                         + " and the " + userHand.getCard(1) + ".");

               TextIO.putln();

               TextIO.putln("You have Blackjack. You win.");

               return true;

          }

         

          /* If neither player has Blackjack, play the game. First the user

              gets a chance to draw cards (i.e., to "Hit"). The while loop ends

              when the user chooses to "Stand". If the user goes over 21,

              the user loses immediately.

          */

         

          while (true) {

             

               /* Display user's cards, and let user decide to Hit or Stand. */

   

               TextIO.putln();

               TextIO.putln();

               TextIO.putln("Your cards are:");

               for ( int i = 0; i < userHand.getCardCount(); i++ )

                  TextIO.putln("    " + userHand.getCard(i));

               TextIO.putln("Your total is " + userHand.getBlackjackValue());

               TextIO.putln();

               TextIO.putln("Dealer is showing the " + dealerHand.getCard(0));

               TextIO.putln();

               TextIO.put("Hit (H) or Stand (S)? ");

               char userAction; // User's response, 'H' or 'S'.

               do {

                  userAction = Character.toUpperCase( TextIO.getlnChar() );

                  if (userAction != 'H' && userAction != 'S')

                     TextIO.put("Please respond H or S: ");

               } while (userAction != 'H' && userAction != 'S');

   

               /* If the user Hits, the user gets a card. If the user Stands,

                  the loop ends (and it's the dealer's turn to draw cards).

               */

   

               if ( userAction == 'S' ) {

                       // Loop ends; user is done taking cards.

                   break;

               }

               else { // userAction is 'H'. Give the user a card.

                       // If the user goes over 21, the user loses.

                   Card newCard = deck.dealCard();

                   userHand.addCard(newCard);

                   TextIO.putln();

                   TextIO.putln("User hits.");

                   TextIO.putln("Your card is the " + newCard);

                   TextIO.putln("Your total is now " + userHand.getBlackjackValue());

                   if (userHand.getBlackjackValue() > 21) {

                       TextIO.putln();

                       TextIO.putln("You busted by going over 21. You lose.");

                       TextIO.putln("Dealer's other card was the "

                                                          + dealerHand.getCard(1));

                       return false;

                   }

               }

              

          } // end while loop

         

          /* If we get to this point, the user has Stood with 21 or less. Now, it's

             the dealer's chance to draw. Dealer draws cards until the dealer's

             total is > 16. If dealer goes over 21, the dealer loses.

          */

   

          TextIO.putln();

          TextIO.putln("User stands.");

          TextIO.putln("Dealer's cards are");

          TextIO.putln("    " + dealerHand.getCard(0));

          TextIO.putln("    " + dealerHand.getCard(1));

          while (dealerHand.getBlackjackValue() <= 16) {

             Card newCard = deck.dealCard();

             TextIO.putln("Dealer hits and gets the " + newCard);

             dealerHand.addCard(newCard);

             if (dealerHand.getBlackjackValue() > 21) {

                TextIO.putln();

                TextIO.putln("Dealer busted by going over 21. You win.");

                return true;

             }

          }

          TextIO.putln("Dealer's total is " + dealerHand.getBlackjackValue());

         

          /* If we get to this point, both players have 21 or less. We

             can determine the winner by comparing the values of their hands. */

         

          TextIO.putln();

          if (dealerHand.getBlackjackValue() == userHand.getBlackjackValue()) {

             TextIO.putln("Dealer wins on a tie. You lose.");

             return false;

          }

          else if (dealerHand.getBlackjackValue() > userHand.getBlackjackValue()) {

             TextIO.putln("Dealer wins, " + dealerHand.getBlackjackValue()

                              + " points to " + userHand.getBlackjackValue() + ".");

             return false;

          }

          else {

             TextIO.putln("You win, " + userHand.getBlackjackValue()

                              + " points to " + dealerHand.getBlackjackValue() + ".");

             return true;

          }

   

       } // end playBlackjack()

   

   

    } // end class Blackjack