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

You are to create a blackjack hand class. This class will contain a \"hand\" (up

ID: 3830014 • Letter: Y

Question

You are to create a blackjack hand class. This class will contain a "hand" (up to 11 cards). UML as follows: Blackjack Hand - Cards [] hand - numCards int +BlackjackHand() +getScore() int +addCard(Card c) void +resetHand() void -toString () String Modify your Blackjack Hand so that you can keep track of player's name, money on hand, money bet, and adjust after a hand depending on if the player won, lost, or tied. Create a game with up to 5 players and a dealer For each round, do the following Reset all hands Place bets (up to amount of money on hand) Deal two Cards to everyone Go from player to player, asking if they wish to hit or stay. Any total 22 or more they automatically lose and are done Once all players are through, deal cards to the dealer as follows o16 or less the dealer must take a Card, 17 or more the dealer must stay Reconcile all bets If a player's pot is empty, they are out of the game. Please don't use the complex coding. Use a simple one so that I can understand. Also, please provide as much details as possible. If possible also upload the image of output.

Explanation / Answer

package myProject;

import java.util.*;

public class Blackjack
{
   static Scanner sc = new Scanner(System.in);
   // Definition class Card
   public static class MyCard
   {
      
       // Declaration of instance variables
       public final int suit;
       public final int number;
       public final String type;
       public final int points;
      
       // Define Card constructor
       public MyCard(int s, int n)
       {
           this.suit = s;
           this.number = n;
               switch (this.number)
               {
               case 1:
                   this.type = "ace";
                   // aces being 1 or 11 will be handled by the getScore method
                   this.points = 11;
                   break;
               case 11:
                   this.type = "jack";
                   this.points = 10;
                   break;
               case 12:
                   this.type = "queen";
                   this.points = 10;
                   break;
               case 13:
                   this.type = "king";
                   this.points = 10;
                   break;
               default:
                   this.type = Integer.toString(number);
                   this.points = number;
                   break;
           }//End of switch
       }//End of constructor
   }//End of class

   // Definition of class player
   public static class MyPlayer
   {
       // Declaration of instance variables
       public String name;
       public int cash = 200;
       public int bet = 0;
       public boolean stay = false;
       public boolean bust = false;
       public boolean bankrupt = false;
       public ArrayList<MyCard> hand = new ArrayList<MyCard>();
      
       //Method getScore() returns the score
       public int getScore()
       {
           int score = 0;
           int aces = 0;
          
           // This loop counts the number of aces in one's hand
           for (MyCard card : this.hand)
           {
               //Adds the score with the points
               score += card.points;
               //Checks if the card type is ace then increase the aces by one
               if (card.type.equals("ace"))
                   aces++;
           }//End of for loop
          
           // This loop subtracts 10 points from the player's score
           // for each ace in their hand while their score is above 21
           while (aces > 0)
           {
               //Checks the score if it is less than or equal to 21 then break
               if (score <= 21)
                   break;
               //if not
               else
               {
                   //Subtract 10 from the score and decrease one from aces.
                   score -= 10;
                   aces--;
               }//End of else
           }//End of while
           //Returns the score
           return score;
       }//End of method
      
       // Define method to return a list of cards in one's hand
       public String getHand()
       {
           String cardInHand = "";
           for (MyCard card : this.hand)
           {
               if (!this.hand.get(this.hand.size() - 1).equals(card))
                   cardInHand += card.type + ", ";
               else
                   cardInHand += card.type;
           }//End of for
           return cardInHand;
       }//End of method
   }//End of class

   /* Define Blackjack method to deal a card to a player.
   * Generates a random suit and number.
   * If that combination of suit and number is still in the deck,
   * the card is dealt to the player and removed from the deck.
   * Otherwise a new combination is generated.*/
  
   public static void deal(MyPlayer player, ArrayList<MyCard> deck)
   {
       //Random class object created
       Random generator = new Random();
       //Initializes the success to false
       boolean success = false;
       //Loops while not success
       while (!success)
       {
           //Generates a random number between 1 and 13
           int randomNumber = generator.nextInt(13) + 1;
           //Generates a random suit between 1 and 4
           int randomSuit = generator.nextInt(4) + 1;
           for (MyCard card : deck)
           {
               if (card.suit == randomSuit && card.number == randomNumber)
               {
                   success = true;
                   MyCard randomCard = new MyCard(randomSuit,randomNumber);
                   player.hand.add(randomCard);
                   deck.remove(randomCard);
               }//End of if
           }//End of for loop
       }//End of while loop
   }//End of method
  
   //Sets the deck to the list
   public static ArrayList<MyCard> setDeck()
   {
       //Creates array list for cards
       ArrayList<MyCard> deck = new ArrayList<MyCard>();
       for (int s = 1; s < 5; s++)
       {
           for (int n = 1; n < 14; n++)
               deck.add(new Blackjack.MyCard(s,n));
       }//End of for loop
       return deck;
   }//End of method

   //Method to set a player
   public static ArrayList<MyPlayer> setPlayers()
   {
         
       //Creates an array list for players
       ArrayList<MyPlayer> players = new ArrayList<MyPlayer>();
       String playerNum = null;
       //For maximum 7 players
       Object[] playerSelector = {"1","2","3","4","5","6","7"};
       //Accepts a valid player number
       do
       {
           System.out.println("How many players? (1 - 7)");
           for(int x = 0; x < playerSelector.length; x++)
               System.out.print(playerSelector[x]+ " ");
           playerNum = sc.next();
                  
           if (playerNum == null)
               Blackjack.quitConfirm();
       }while (playerNum == null || (playerNum != null && (!playerNum.matches("[1-7]"))));
       //Converts the inputed player number to integer
       int intPlayers = Integer.parseInt(playerNum);
       //Loops till the entered player number
       for (int i = 0; i < intPlayers; i++)
       {
           //Adds the player to the players list
           players.add(new MyPlayer());
           //Accepts name of the player
           System.out.println("Player " + (i+1) + ", Enter your name:");
           players.get(i).name = sc.next();          
       }//End of for loop
       //Returns the players
       return players;
   }//End of method

   //Accepts the confirmation from the players and returns it
   public static char quitConfirm()
   {
       //Accepts the confirmation for quit
       System.out.println("Are you sure you want to quit? (Y/N)");
       char quitConfirm = sc.next().charAt(0);
       if (quitConfirm == 'Y' || quitConfirm == 'y')
       {
           System.out.println("Please play again!");
           System.exit(0);
           return quitConfirm;
       }//End of if
       else
           return quitConfirm;
   }//End of method

   //Method for playing
   public static void playRound(ArrayList<MyPlayer> players, ArrayList<MyCard> deck)
   {
       // Deal two cards to each player to start the game, and accepts their bet amount
       for (MyPlayer curPlayer : players)
       {
           //Checks if the current player is not having sufficient money
           if (!curPlayer.bankrupt)
           {
               Blackjack.deal(curPlayer,deck);
               Blackjack.deal(curPlayer,deck);
               int bet = 0;
               boolean validBet = false;
               do
               {
                   //Accepts the bet amount
                   System.out.println(curPlayer.name + ", you have $" + curPlayer.cash + ". " + "Please enter an positive integer amount of money for your bet:");
                   String betStr = sc.next();
                   try
                   {
                       //Converts the bet amount to integer
                       bet = Integer.parseInt(betStr);
                       //Checks if the current bet amount is greater than the cash in hand then display error message
                       if (bet > curPlayer.cash)
                       {
                           System.out.println("Your bet was $" + bet + " but you only have $" + curPlayer.cash + ".");
                       }//End of if
                       //Otherwise
                       else
                       {
                           curPlayer.bet = bet;
                           validBet = true;
                       }//End of else
                   }//End of try block
                   catch (NumberFormatException e)
                   {
                       if (betStr == null)
                           Blackjack.quitConfirm();
                       else
                       {
                           System.out.println("Please enter a valid bet!");
                       }//End of else
                   }//End of catch
               }while (!validBet);
           }//End of if
       }//End of for

       // while the user not quit
       boolean roundEnd = false;
       while (!roundEnd)
       {
           // Count the number of finished players
           int endPlayers = 0;
           // Loop through each player
           for (int i = 0; i < players.size(); i++)
           {
               MyPlayer curPlayer = players.get(i);
               // If the current player hasn't bust or stayed and isn't bankrupt
               if (!curPlayer.bust && !curPlayer.stay && !curPlayer.bankrupt)
               {
                   // Alert whose turn it is
                   System.out.println(curPlayer.name + ", it's your turn!");
                   Object[] options = {"Hit","Stay"};
                   int decision = -1;
                   char quitConfirm = 'n';
                   do
                   {
                       System.out.println("The cards in your hand are: " + curPlayer.getHand() + ". " +
                               "Your score is " + curPlayer.getScore());
                       System.out.println("Enter your choice: 0 for " + options[0] + "1 for " + options[1]);
                       decision = sc.nextInt();
                       switch (decision)
                       {
                           case 0:
                               Blackjack.deal(curPlayer,deck);
                               break;
                           case 1:
                               curPlayer.stay = true;
                               break;
                           default:
                               quitConfirm = Blackjack.quitConfirm();
                       }//End of switch
                   }while (quitConfirm == 'n' && decision == -1);
                   if (curPlayer.getScore() > 21)
                   {
                       curPlayer.bust = true;
                       System.out.println("You busted! Your final score is " + curPlayer.getScore() + ".");
                   }//End of if
               }//End of if
               else
                   endPlayers++;
           }//End of for
           if (endPlayers == players.size())
               roundEnd = true;
       }//End of while
   }//End of method
  
   //Method to update cash
   public static void setCash(ArrayList<MyPlayer> players, MyPlayer dealer)
   {
       //Loops till end of all the players
       for (MyPlayer curPlayer : players)
       {
           //Checks for not sufficient cash
           if (!curPlayer.bankrupt)
           {
               //Checks if the score is between 5 and 21
               if (curPlayer.getScore() <= 21 && curPlayer.hand.size() >= 5)
                   //Player cash is added with the bet amount
                   curPlayer.cash += curPlayer.bet;
               else if (curPlayer.getScore() > 21 || (curPlayer.getScore() <= dealer.getScore() && dealer.getScore() <= 21))
                   //Player bet amount is deducted from the players cash
                   curPlayer.cash -= curPlayer.bet;
               else if (curPlayer.getScore() == 21 && curPlayer.hand.size() == 2)
                   curPlayer.cash += 2 * curPlayer.bet;
               else
                   curPlayer.cash += curPlayer.bet;
               //If the current player cash is zero then bankrupt
               if (curPlayer.cash == 0)
               {
                   curPlayer.bankrupt = true;
                   System.out.println(curPlayer.name + ", you lost all of your money! Thank you for playing with us.");
               }//End of if
           }//End of if
       }//End of for loop
   }//End of method

   //To reset
   public static void reset(ArrayList<MyPlayer> players, ArrayList<MyCard> deck)
   {
       //Loops for all the players
       for (MyPlayer player : players)
       {
           player.hand = new ArrayList<MyCard>();
           player.bust = false;
           player.stay = false;
           player.bet = 0;
       }//End of for loop
       deck = Blackjack.setDeck();
   }//End of method

   //Method to display score
   public static void displayScores(ArrayList<MyPlayer> players)
   {
       // Display final scores
       int[] finalScores = new int[players.size()];
       String scores = "--- Final Scores --- ";
       //Creates a HTML table for all the players score
       for (int i = 0; i < players.size(); i++)
       {
           finalScores[i] = players.get(i).getScore();
           scores += " " + players.get(i).name + ": $" + players.get(i).cash + " ";
       }//End of for
       scores += " ";
       System.out.println(scores);
   }//End of method

   public static void main(String[] args)
   {
       // Create list of players
       ArrayList<MyPlayer> players = Blackjack.setPlayers();
       // Generate deck
       ArrayList<MyCard> deck = Blackjack.setDeck();
       char repeat = 'y';//JOptionPane.YES_OPTION;
       while (repeat == 'y')
       {
           // Generate dealer
           MyPlayer dealer = new MyPlayer();
           dealer.name = "Dealer";
           Blackjack.deal(dealer,deck);
           Blackjack.deal(dealer,deck);
           while (dealer.getScore() < 17)
               Blackjack.deal(dealer,deck);

           // Play one round
           Blackjack.playRound(players,deck);

           // Show scores
           String roundScores = "--- Round Scores --- Dealer: " + dealer.getScore() + " ";
           int bankruptPlayers = 0;
           for (MyPlayer curPlayer : players)
               roundScores += " " + curPlayer.name + ": " + curPlayer.getScore() + " ";
           roundScores += " ";
           System.out.println(roundScores);
           Blackjack.setCash(players, dealer);
           for (MyPlayer curPlayer : players)
               if (curPlayer.bankrupt)
                   bankruptPlayers++;
           if (bankruptPlayers < players.size())
           {
               System.out.println("Would you like to play another round?");
               repeat = sc.next().charAt(0);
           }//End of if
           else
               repeat = 'n';
           if (repeat == 'N' || repeat == 'n')
           {
               break;
           }
           else
           {
               Blackjack.reset(players,deck);
           }
       }//End of while
       Blackjack.displayScores(players);
   }//End of method
}//End of class

Output:

How many players? (1 - 7)
1 2 3 4 5 6 7 3
Player 1, Enter your name:
pyari
Player 2, Enter your name:
mohan
Player 3, Enter your name:
sahu
pyari, you have $200.
Please enter an positive integer amount of money for your bet:
150
mohan, you have $200.
Please enter an positive integer amount of money for your bet:
100
sahu, you have $200.
Please enter an positive integer amount of money for your bet:
80
pyari, it's your turn!
The cards in your hand are: 6, 5.
Your score is 11
Enter your choice: 0 for Hit1 for Stay
0
mohan, it's your turn!
The cards in your hand are: king, ace.
Your score is 21
Enter your choice: 0 for Hit1 for Stay
0
sahu, it's your turn!
The cards in your hand are: ace, jack.
Your score is 21
Enter your choice: 0 for Hit1 for Stay
0
pyari, it's your turn!
The cards in your hand are: 6, 5, 8.
Your score is 19
Enter your choice: 0 for Hit1 for Stay
1
mohan, it's your turn!
The cards in your hand are: king, ace, ace.
Your score is 12
Enter your choice: 0 for Hit1 for Stay
0
sahu, it's your turn!
The cards in your hand are: ace, jack, 10.
Your score is 21
Enter your choice: 0 for Hit1 for Stay
0
You busted! Your final score is 25.
mohan, it's your turn!
The cards in your hand are: king, ace, ace, 5.
Your score is 17
Enter your choice: 0 for Hit1 for Stay
0
You busted! Your final score is 26.
--- Round Scores ---
Dealer:   20

pyari:   19

mohan:   26

sahu:   25


Would you like to play another round?
n
--- Final Scores ---

pyari:   $50

mohan:   $100

sahu:   $120

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote