Question
I need help with my output, I need to make it look like the sample output, thank you. I've also, included the other classes in
I need help with my output, I need to make it look like the sample output, thank you. I've also, included the other classes in case you may need to refer to them.
----(Black jack demo class)----
import java.util.Scanner; public class BlackJackDemo { 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;
public static void main(String[] args) { BlackJackDemo dj = new BlackJackDemo(); //BlackJack mechanism starts here Scanner keyboard = new Scanner(System.in); String s = "yes"; int i = 0; int j = 0;
while(s.equalsIgnoreCase("yes")) { System.out.println("***********************************"); System.out.println("* Welcome to the Black Jack Table *"); System.out.println("***********************************"); i++; Hand dealer = new Hand(); Hand player = new Hand(); Deck deck = new Deck(); dj.playerTurn(player,deck); if(player.busted()) { System.out.println("The dealer wins!"); } else { dj.dealerTurn(dealer,deck); if(dealer.busted()) { System.out.println("Hooray you win!"); j++; } else if(dj.playerWins(player,dealer)) { System.out.println("Hooray you win!"); j++; } else System.out.println("The dealer wins with a Blackjack!"); } System.out.println("-----------------------------"); System.out.println("Statistics:"); //Printing number of games played. System.out.println("Number of games played: " +i); //Calculating win percentage. System.out.println("Wins: " + j/(double)i*100 + "%"); System.out.println("-----------------------------"); //Continue to the process if yes was interested //and stop the process if no was pressed. System.out.print("Do you want to play again?(yes/no) "); s = keyboard.next(); } System.out.println("Bye Bye"); }
String getPlayerMove() { Scanner kb = new Scanner(System.in); String input; while(true) { System.out.print("Do you want to stand or hit ? "); input = kb.nextLine(); 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) { System.out.println("The dealer's hand is: "); while(true) { int value = dealer.getValue(); if(value < 17) { Card c = deck.deal(); dealer.addCard(c); System.out.println(c); if(dealer.busted()) { System.out.println("<" + value + " points>"); System.out.println("Bust!"); break; } } else { System.out.println("<" + value + " points>"); break; } } }
private boolean playerTurn(Hand player,Deck deck) { System.out.println("Your hand is:"); Card d = deck.deal(); player.addCard(d); System.out.println(d); System.out.println("<" + player.getValue() + " points>"); while(true) { String move = getPlayerMove(); if(move.equals("hit")) { System.out.println("Your hand is:"); Card c = deck.deal(); player.addCard(c); System.out.println(c); System.out.println("<" + player.getValue() + " points>"); if(player.busted()) { return true; } } else { return false; } } }
private boolean playerWins(Hand player,Hand dealer) { if(player.busted()) { return false; } if(dealer.busted()) { return player.getValue() >= dealer.getValue(); } return false; } }
----(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
*********************************** * 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
*********************************** * 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
----(my output)------
*********************************** * Welcome to the Black Jack Table * ***********************************
Your hand is: Two of Hearts <2 points>
Do you want to stand or hit ? hit
Your hand is: Eight of Spades <11 points>
Do you want to stand or hit ? stand
The dealer's hand is: Three of Diamonds Seven of Clubs Seven of Diamonds <17 points> The dealer wins with a Blackjack!
----------------------------- Statistics: Number of games played: 1 Wins: 0.0% -----------------------------
Do you want to play again?(yes/no) no
Bye Bye
----(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); } } shuffle();//Shuffling the cards for the first time. }
//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); } } }
----(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; }
}
----(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","Two of ","Three of ","Four of ","Five of ","Six of ","Seven of ","Eight of ","Eight of ","Nine of ","Ten of ","Jack of ","Queen of ", "King of ","Ace of "}; //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; } }
---(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(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started