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

BLACKJACK JAVA DATA STRUCTURES In this exercise your goal is to improve the blac

ID: 3815335 • Letter: B

Question

BLACKJACK JAVA DATA STRUCTURES

In this exercise your goal is to improve the blackjack game you created.

Here are a few possible extensions you can implement:

1) Insurance - A side bet if the dealer shows an ace
2) Splitting - If two cards are the same, split them into two hands and double the bet.
3) Double down - Double the bet and get one more card

How do I implement one of these into this code?

BLACKJACK.JAVA=============================================

public class Blackjack extends ConsoleProgram
{

private static final int HEARTS = 0;
private static final int DIAMONDS = 1;
private static final int SPADES = 2;
private static final int CLUBS = 3;
  
private static final int JACK = 11;
private static final int QUEEN = 12;
private static final int KING = 13;
private static final int ACE = 14;
  
// The starting bankroll for the player.
private static final int STARTING_BANKROLL = 100;
  
/**
* Ask the player for a move, hit or stand.
*
* @return A lowercase string of "hit" or "stand"
* to indicate the player's move.
*/
private String getPlayerMove()
{
while(true)
{
String move = readLine("Enter move (hit/stand): ");
move = move.toLowerCase();
  
if(move.equals("hit") || move.equals("stand"))
{
return move;
}
System.out.println("Please try again.");
}
}
  
/**
* Play the dealer's turn.
*
* The dealer must hit if the value of the hand is less
* than 17.
*
* @param dealer The hand for the dealer.
* @param deck The deck.
*/
private void dealerTurn(Hand dealer, Deck deck)
{
while(true)
{
System.out.println("Dealer's hand");
System.out.println(dealer);
  
int value = dealer.getValue();
System.out.println("Dealer's hand has value " + value);
  
readLine("Enter to continue...");
  
if(value < 17)
{
System.out.println("Dealer hits");
Card c = deck.deal();
dealer.addCard(c);
  
System.out.println("Dealer card was " + c);
  
if(dealer.busted())
{
System.out.println("Dealer busted!");
break;
}
}
else
{
System.out.println("Dealer stands.");
break;
}
}
}
  
/**
* Play a player turn by asking the player to hit
* or stand.
*
* Return whether or not the player busted.
*/
private boolean playerTurn(Hand player, Deck deck)
{
while(true)
{
String move = getPlayerMove();
  
if(move.equals("hit"))
{
Card c = deck.deal();
System.out.println("Your card was: " + c);
player.addCard(c);
System.out.println("Player's hand");
System.out.println(player);
  
if(player.busted())
{
return true;
}
}
else
{
// If we didn't hit, the player chose to
// stand, which means the turn is over.
return false;
}
  
}
}
  
/**
* Determine if the player wins.
*
* If the player busted, they lose. If the player did
* not bust but the dealer busted, the player wins.
*
* Then check the values of the hands.
*
* @param player The player hand.
* @param dealer The dealer hand.
* @return
*/
private boolean playerWins(Hand player, Hand dealer)
{
if(player.busted())
{
return false;
}
  
if(dealer.busted())
{
return true;
}
  
return player.getValue() > dealer.getValue();
}
  
/**
* Check if there was a push, which means the player and
* dealer tied.
*
* @param player The player hand.
* @param dealer The dealer hand.
* @return
*/
private boolean push(Hand player, Hand dealer)
{
return player.getValue() == dealer.getValue();
}
  
/**
* Find the winner between the player hand and dealer
* hand. Return how much was won or lost.
*/
private double findWinner(Hand dealer, Hand player, int bet)
{
if(playerWins(player, dealer))
{
System.out.println("Player wins!");
  
if(player.hasBlackjack())
{
return 1.5 * bet;
}
  
return bet;
}
else if(push(player, dealer))
{
System.out.println("You push");
return 0;
}
else
{
System.out.println("Dealer wins");
return -bet;
}
}
  
/**
* This plays a round of blackjack which includes:
* - Creating a deck
* - Creating the hands
* - Dealing the round
* - Playing the player turn
* - Playing the dealer turn
* - Finding the winner
*
* @param bankroll
* @return The new bankroll for the player.
*/
private double playRound(double bankroll)
{
int bet = readInt("What is your bet? ");

Deck deck = new Deck();
deck.shuffle();
  
Hand player = new Hand();
Hand dealer = new Hand();
  
player.addCard(deck.deal());
dealer.addCard(deck.deal());
player.addCard(deck.deal());
dealer.addCard(deck.deal());
  
System.out.println("Player's Hand");
System.out.println(player);
  
  
System.out.println("Dealer's hand");
//System.out.println(dealer);
dealer.printDealerHand();
  
boolean playerBusted = playerTurn(player, deck);
  
if(playerBusted)
{
System.out.println("You busted :(");
}

readLine("Enter for dealer turn...");
dealerTurn(dealer, deck);
  
double bankrollChange = findWinner(dealer, player, bet);
  
bankroll += bankrollChange;
  
System.out.println("New bankroll: " + bankroll);
  
return bankroll;
}
  
/**
* Play the blackjack game. Initialize the bankroll and keep
* playing roudns as long as the user wants to.
*/
public void run()
{
double bankroll = STARTING_BANKROLL;
System.out.println("Starting bankroll: " + bankroll);

while(true)
{
bankroll = playRound(bankroll);
  
String playAgain = readLine("Would you like to play again? (Y/N)");
if(playAgain.equalsIgnoreCase("N"))
{
break;
}
}
  
System.out.println("Thanks for playing!");
}
  
}

CARD.JAVA=============================================

public class Card
{
   // These constants represent the possible suits and
   // can be used to index into the suits array to get
   // their string representation.
private static final int HEARTS = 0;
private static final int DIAMONDS = 1;
private static final int SPADES = 2;
private static final int CLUBS = 3;
  
// These constants represent the ranks of the non-number
// cards, or cards above 10. To maintain the ordering after
// 2-10, the integer values are 11, 12, 13, and 14 and
// also allow us to index into the ranks array to get their
// String representation.
private static final int JACK = 11;
private static final int QUEEN = 12;
private static final int KING = 13;
private static final int ACE = 14;

// Instance variables
  
// This represents the rank of the card, the value from 2 to Ace.
private int rank;
  
// This represents the suit of the card, one of hearts, diamonds, spades or clubs.
private int suit;
  
// This represents the value of the card, which is 10 for face cards or 11 for an ace.
private int value;
  
// This String array allows us to easily get the String value of a Card from its rank.
// There are two Xs in the front to provide padding so numbers have their String representation
// at the corresponding index. For example, the String for 2 is at index 2.
private String[] ranks = {"X", "X", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  
// The String array allow us to easily get the String value of the Card for its suit. This
// is the same order as the suits above so we can index into this array.
private String[] suits = {"H", "D", "S", "C"};
  
/**
* This is the constructor to create a new Card. To create a new card
* we pass in its rank and its suit.
*
* @param r   The rank of the card, as an int.
* @param s   The suit of the card, as an int.
*/
public Card(int r, int s)
{
rank = r;
suit = s;
}
  
// Getter Methods
  
/**
* This returns the rank of the card as an integer.
*
* @return rank of card as an int.
*/
public int getRank()
{
return rank;
}
  
/**
* This returns the suit of the card as an integer.
*
* @return suit of card as an int.
*/
public int getSuit()
{
return suit;
}
  
/**
* This returns the value of the card as an integer.
*
* For facecards the value is 10, which is different
* than their rank underlying value. For aces the default
* value is 11.
*
* @return The value of the card as an int.
*/
public int getValue()
{
int value = rank;
if(rank > 10)
{
value = 10;
}
  
if(rank == ACE)
{
value = 11;
}
  
return value;
}
  
/**
* This utility method converts from a rank integer to a String.
*
* @param r   The rank.
* @return   String version of rank.
*/
public String rankToString(int r)
{
return ranks[r];
}
  
/**
* This utility method converts from a suit integer to a String.
*
* @param s   The suit.
* @return   String version of suit.
*/
public String suitToString(int s)
{
return suits[s];
}
  
/**
* Return the String version of the suit.
*
* @return String version of suit.
*/
public String getSuitAsString()
{
return suitToString(suit);
}
  
/**
* Return the String version of the rank.
*
* @return String version of the rank.
*/
public String getRankAsString()
{
return rankToString(rank);
}
  
/**
* This returns the String representation of a card which
* will be two characters. For example, the two of hearts would
* return 2H. Face cards have a short string so the ace of
* spades would return AS.
*
* @return String representation of Card.
*/
public String toString()
{
// Get a string for rank
String rankString = ranks[rank];
  
// Get a string for the suit
String suitString = suits[suit];
  
// combine those
return rankString + suitString;
}

  

DECK.JAVA=============================================

import java.util.*;

public class Deck
{
private static final int HEARTS = 0;
private static final int DIAMONDS = 1;
private static final int SPADES = 2;
private static final int CLUBS = 3;

private static final int JACK = 11;
private static final int QUEEN = 12;
private static final int KING = 13;
private static final int ACE = 14;
  
// Instance variables
  
// This stores the deck which is a list of the Card objects.
private ArrayList deck;
  
/**
* This creates a Deck. A Deck starts as a list of 52 cards.
* We loop through each suit and rank and construct a card
* and add it to the deck.
*/
public Deck()
{
deck = new ArrayList();
  
for(int rank = 2; rank <= ACE; rank++)
{
for(int suit = HEARTS; suit <= CLUBS; suit++)
{
Card card = new Card(rank, suit);
deck.add(card);
}
}
}
  
// Getter method
  
/**
* This getter method returns the ArrayList of cards.
* @return ArrayList of the Cards.
*/
public ArrayList getCards()
{
return deck;
}
  
/**
* This deals the first Card from the deck by removing it.
* @return The first Card in the deck.
*/
public Card deal()
{
return deck.remove(0);
}
  
/**
* This prints out the current state of the deck.
*/
public void print()
{
for(Card card: deck)
{
System.out.println(card);
}
}
  
/**
* This shuffles the deck by making 52 swaps of
* card positions.
*/
public void shuffle()
{
for(int i = 0; i < deck.size(); i++)
{
int randomIndex = Randomizer.nextInt(52);
Card x = deck.get(i);
Card y = deck.get(randomIndex);
  
deck.set(i, y);
deck.set(randomIndex, x);
}
}

HAND.JAVA=============================================

import java.util.*;

public class Hand
{

private static final int HEARTS = 0;
private static final int DIAMONDS = 1;
private static final int SPADES = 2;
private static final int CLUBS = 3;

private static final int JACK = 11;
private static final int QUEEN = 12;
private static final int KING = 13;
private static final int ACE = 14;

// This represents the list of cards in the hand.
private ArrayList cards;
  
/**
* This constructor sets up our hand by initializing our
* ArrayList.
*/
public Hand()
{
cards = new ArrayList();
}
  
/**
* This adds a card to our hand.
*
* @param c The card to add to the hand.
*/
public void addCard(Card c)
{
cards.add(c);
}
  
/**
* This returns the value of the hand as an integer.
*
* The value of the hand is the sum of the values
* of the individual cards. There is also an adjustment
* made for the value of an ace, which can be 11 or 1
* depending on the situation.
*
* @return The value of the hand.
*/
public int getValue()
{
int sum = 0;
int aceCount = 0;
  
for(Card c: cards)
{
sum += c.getValue();
  
if(c.getRank() == ACE)
{
aceCount++;
}
}
  
while(sum > 21 && aceCount > 0)
{
sum -= 10;
aceCount--;
}
  
return sum;
}
  
/**
* Return if this hand has a blackjack.
*
* @return If the hand is a blackjack.
*/
public boolean hasBlackjack()
{
return getValue() == 21 && cards.size() == 2;
}
  
/**
* Return if the hand busted, which means has a value
* greater than 21.
*
* @return If the hand busted.
*/
public boolean busted()
{
return getValue() > 21;
}
  
/**
* Return if the hand is a five-card-charlie, which means
* contained five cards.
*
* @return If the hand was a five-card charlie.
*/
public boolean fiveCardCharlie()
{
return cards.size() == 5;
}
  
/**
* Print out a dealer hand, and show an X instead of
* the first card.
*/
public void printDealerHand()
{
for(int i = 0; i < cards.size(); i++)
{
Card c = cards.get(i);
  
if(i == 0)
{
System.out.print("X ");
}
else
{
System.out.print(c + " ");
}
}
System.out.println();
}
  
  
/**
* Return a String representation of the hand, including showing
* the value.
*/
public String toString()
{
String result = "";
  
for(Card c: cards)
{
result += c + " ";
}
  
result += "(" + getValue() + ")";
  
return result;
}

}

RANDOMIZER.JAVA=============================================

import java.util.*;

public class Randomizer{

   public static Random theInstance = null;
  
   public Randomizer(){
      
   }
  
   public static Random getInstance(){
       if(theInstance == null){
           theInstance = new Random();
       }
       return theInstance;
   }
  
   /**
   * Return a random boolean value.
   * @return True or false value simulating a coin flip.
   */
   public static boolean nextBoolean(){
       return Randomizer.getInstance().nextBoolean();
   }

   /**
   * This method simulates a weighted coin flip which will return
   * true with the probability passed as a parameter.
   *
   * @param   probability   The probability that the method returns true, a value between 0 to 1 inclusive.
   * @return True or false value simulating a weighted coin flip.
   */
   public static boolean nextBoolean(double probability){
       return Randomizer.nextDouble() < probability;
   }
  
   /**
   * This method returns a random integer.
   * @return A random integer.
   */
   public static int nextInt(){
       return Randomizer.getInstance().nextInt();
   }

   /**
   * This method returns a random integer between 0 and n, exclusive.
   * @param n   The maximum value for the range.
   * @return A random integer between 0 and n, exclusive.
   */
   public static int nextInt(int n){
       return Randomizer.getInstance().nextInt(n);
   }

   /**
   * Return a number between min and max, inclusive.
   * @param min   The minimum integer value of the range, inclusive.
   * @param max   The maximum integer value in the range, inclusive.
   * @return A random integer between min and max.
   */
   public static int nextInt(int min, int max){
       return min + Randomizer.nextInt(max - min + 1);
   }

   /**
   * Return a random double between 0 and 1.
   * @return A random double between 0 and 1.
   */
   public static double nextDouble(){
       return Randomizer.getInstance().nextDouble();
   }

   /**
   * Return a random double between min and max.
   * @param min The minimum double value in the range.
   * @param max The maximum double value in the rang.
   * @return A random double between min and max.
   */
   public static double nextDouble(double min, double max){
       return min + (max - min) * Randomizer.nextDouble();
   }

  
}

(Thank you for your help!!!!!!)

Explanation / Answer

//added this to check if user has said yes for double deal

//this is a global variable
private boolean doubledeal=false;

private boolean playerTurn(Hand player, Deck deck)
   {
       while(true)
       {
           String move = getPlayerMove();

           if(move.equals("hit"))
           {
               Card c = deck.deal();
               System.out.println("Your card was: " + c);
               player.addCard(c);
               System.out.println("Player's hand");
               System.out.println(player);

               if(player.busted())
               {
                   return true;
               }
               else
               {
                   return Doubledown();

// calls doubledown
               }
           }
           else
           {
           // If we didn't hit, the player chose to
           // stand, which means the turn is over.
           return false;
           }
       }
              

       }

//to set doubledeal to true

//it also return true if player is busted or false if not in order to couple with playerTurn method
           public boolean DoubleDown()

           {

               String move=readLine("do you want to take one more card and double the bet(Y/N)");
               if(move.equals("Y"))
               {
                   Card c = deck.deal();
                   System.out.println("Your card was: " + c);
                   player.addCard(c);
                   System.out.println("Player's hand");
                   System.out.println(player);

                   if(player.busted())
                   {
                       return true;
                   }
                   else
                   {
  
                       doubledeal=true;
                       return false;
                   }
               }
           else
           {
               // player chooses No so no chance of getting busted
               return false;
           }


       }

// added a check to see if doubledeal ==true if yes double the bet and set it to false;

private double playRound(double bankroll)
   {
       int bet = readInt("What is your bet? ");
       Deck deck = new Deck();
       deck.shuffle();

       Hand player = new Hand();
       Hand dealer = new Hand();

       player.addCard(deck.deal());
       dealer.addCard(deck.deal());
       player.addCard(deck.deal());
       dealer.addCard(deck.deal());

       System.out.println("Player's Hand");
       System.out.println(player);


       System.out.println("Dealer's hand");
       //System.out.println(dealer);
       dealer.printDealerHand();

       boolean playerBusted = playerTurn(player, deck);

       if(playerBusted)
       {
           System.out.println("You busted :(");
       }
       //added this
       if(doubledeal==true)
       {
           bet=2*bet;
           //make it false again
           doubledeal=false;
       }
       readLine("Enter for dealer turn...");
       dealerTurn(dealer, deck);

       double bankrollChange = findWinner(dealer, player, bet);

       bankroll += bankrollChange;

       System.out.println("New bankroll: " + bankroll);

       return bankroll;
   }