Question: I need help with putting this classes together into a blackjackdemo class and I need help with the output too. (The deck class) import java.util.*;
I need help with putting this classes together into a blackjackdemo class and I need help with the output too.
(The deck class)
import java.util.*; public class Deck {
public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList
//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); } } }
//This getter method returns the ArrayList of cards. public ArrayList getCards() { return deck; }
//This deals the first card from the deck by removing it. public Card deal() { return deck.remove(0); }
//This shuffles the deck by making 52 swaps of cards positions. public void shuffle() { for(int i = 0; i < deck.size(); i++) { int randomIndex = Randomizer.nextInt(52); Card one = deck.get(i); Card two = deck.get(randomIndex); deck.set(i,two); deck.set(randomIndex,one); } } }
(the Card class)
public class Card { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; //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; //The reason for the two X's in the front provide padding so numbers //have their String representation for their corresponding index. private String[]ranks = {"X","X","2","3","4","5","6","7","8","8","9","10","Jack","Queen", "King","Ace"}; //The String array makes it easier to get the String value of the Card for its suit. private String[]suits = {"HEARTS","DIAMONDS","SPADES","CLUBS"};
//This is the constructor to create a new card. //To create a new card we pass in its rank and its suit. public Card(int r, int s) { rank = r; suit = s; }
public void setRank(int rank) { this.rank = rank; }
public void setValue(int value) { this.value = value; } //This getter method returns the rank of the cards an an integer. public int getRank() { return rank; }
//This getter method returns the suit of the cards an integer. public int getSuit() { return suit; }
//This getter method returns the value of the card as an integer. //For facecards the value is 10, which is different than their rank //underlying value. //Four aces the default value is 11. 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. public String rankToString(int r) { return ranks[r]; }
//This utility method converts from a suit integer to a String. public String suitToString(int s) { return suits[s]; }
//Returns the String version of the suit. public String getSuitAsString() { return suitToString(suit); }
//Returns the String version of the rank. public String getRankAsString() { return rankToString(rank); }
//This toString method returns the String representation of a card //which will be two characters. //For instance, the two of hearts would return 2HEARTS. //Face cards have a short string so the ace of spades would return AceSpades. public String toString() { String rankString = ranks[rank]; String suitString = suits[suit]; return rankString + suitString; } }
(the Hand class)
import java.util.*; public class Hand { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList
//This constructor sets up our hand by initializing our ArrayList. public Hand() { cards = new ArrayList
//This adds a card to our 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. 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. public boolean hasBlackJack() { return getValue() == 21 && cards.size() == 2; }
//Return if the hand is busted, which means it has value //greater than 21. public boolean busted() { return getValue() > 21; }
}
(the randomizer class)
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; }
//Returns a random boolean value. public static boolean nextBoolean() { return Randomizer.getInstance().nextBoolean(); }
//This method simulated a wieghted coin flip which will return //true with the probability passed as a parameter. public static boolean nextBoolean(double probability) { return Randomizer.nextDouble() < probability; }
//This method returns a random integer. public static int nextInt() { return Randomizer.getInstance().nextInt(); }
//This method returns a random integer between 0 and n, exclusive. public static int nextInt(int n) { return Randomizer.getInstance().nextInt(n); }
//Returns a number between min and mix, inclusive. public static int nextInt(int min, int max) { return min + Randomizer.nextInt(max - min + 1); }
//Returns a random double between 0 and 1. public static double nextDouble() { return Randomizer.getInstance().nextDouble(); }
//Returns a random double between min and max. public static double nextDouble(double min, double max) { return min + (max - min) * Randomizer.nextDouble(); } }
(BlackJackDemo class in progress)
import java.util.Scanner; public class BlackJackDemo { public static void main(String[] args) { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; Scanner keyboard = new Scanner(System.in); String input;
private String getPlayerMove() { while(true) { System.out.print("Do you want to stand or hit?: ");
if(input.equals("hit") || input.equals("stand")) { input = keyboard.nextLine(); return input; } System.out.println("Please try again."); } }
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);
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; } } }
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 false; } } }
private boolean playerWins(Hand player, Hand dealer) { if(player.busted()) { return false; } if(dealer.busted()) { return true; } return player.getValue() > dealer.getValue(); } } }
(Sample output)
*********************************** * Welcome to the Black Jack Table * *********************************** Your hand is: Four of Hearts Five of Hearts <9 points> Do you want to stand or hit ? hit Your hand is: Four of Hearts Five of Hearts Six of Hearts <15 points> Do you want to stand or hit ? hit Your hand is: Four of Hearts Five of Hearts Six of Hearts Eight of Clubs <23 points> Bust! The dealer wins! ----------------------------- Statistics: number of games played: 1 wins: 0.0% ----------------------------- Do you want to play again? (yes/no) yes 5 *********************************** * Welcome to the Black Jack Table * *********************************** Your hand is: Four of Hearts Ten of Spades <14 points> Do you want to stand or hit ? hit Your hand is: Four of Hearts Ten of Spades Seven of Clubs <21 points> Do you want to stand or hit ? stand The dealer's hand is: Five of Spades Six of Diamonds <11 points> The dealer draws a card... The dealer's hand is now: Five of Spades Six of Diamonds Eight of Hearts <19 points> The dealer draws a card... The dealer's hand is now: Five of Spades Six of Diamonds Eight of Hearts Ten of Hearts <29 points> Bust! Hooray, you win ! ----------------------------- Statistics: number of games played: 2 wins: 50.0% ----------------------------- Do you want to play again? (yes/no) yes 6 *********************************** * Welcome to the Black Jack Table * *********************************** Your hand is: Queen of Hearts Nine of Diamonds <19 points> Do you want to stand or hit ? hit Your hand is: Queen of Hearts Nine of Diamonds Ace of Diamonds <20 points> Do you want to stand or hit ? stand The dealer's hand is: Queen of Diamonds Ace of Hearts <21 points> The dealer wins with a Blackjack! ----------------------------- Statistics: number of games played: 3 wins: 33.33% ----------------------------- Do you want to play again? (yes/no) no Bye Bye
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
