Question: Hello. For some reason, my program is not stopping at 21 cards and allowing the player or dealer to bust. I have attached a picture
Hello. For some reason, my program is not stopping at 21 cards and allowing the player or dealer to bust. I have attached a picture of the results I have been getting back, as well as my 4 classes to run the program.
import java.util.Scanner;
public class BlackJackGame {
public static void main (String args [])
{
boolean Run=true;
Scanner input = new Scanner(System.in);
while(Run) //loop until user exits
{
Deck theDeck = new Deck (1, true);
Player me = new Player("Suz");
Player dealer = new Player("Dealer");
me.addCard(theDeck.dealNextCard());
dealer.addCard(theDeck.dealNextCard());
me.addCard(theDeck.dealNextCard());
dealer.addCard(theDeck.dealNextCard());
System.out.println("Enter you bid money Player 1");
int money1 = input.nextInt();
int money2= money1;
System.out.println("Cards are dealt: ");
me.printHand(true);
dealer.printHand(false);
boolean meDone = false;
boolean dealerDone = false;
String ans;
while (!meDone || !dealerDone) {
if (!meDone) {
System.out.print("Hit [H] or Stay[S])? ");
ans=input.next();
//if player hits, then add next card to deck and store if player is busted
if (ans.compareToIgnoreCase("H") == 0) {
meDone = !me.addCard(theDeck.dealNextCard());
me.printHand(true);
//if busted, done
} else {
meDone = true;
}
}
//dealer
if (!dealerDone) {
System.out.println("The Dealer hits. ");
dealerDone = !dealer.addCard(theDeck.dealNextCard());
dealer.printHand(false);
} else {
System.out.println("The Dealer stays. ");
dealerDone = true;
}
System.out.println(dealer.getHandSum());
System.out.println();
}
//print final hands
me.printHand(true);
dealer.printHand(true);
int mySum = me.getHandSum();
int dealerSum = dealer.getHandSum();
if ((mySum > dealerSum && mySum 21) {
System.out.println("You win."+(money1+money2));
} else {
System.out.println("Dealer wins."+(money1+money2));
}
System.out.print("Want To Play Again (Yes or No): "); //printing
String choice=input.next(); //taking input
if(choice.equalsIgnoreCase("Yes")) //is yes run =true or run =false
Run=true;
else
Run=false;
}
input.close();
}
}
/**
* This class displays the Deck using switch statement
* uses methods Card(int), getNumber(): void, and toString()
* @author suzettepalmer
* @version 8.1
* @since 10/23/2017
*/
public class Card {
private int myNumber;
/**
* Number of card: Ace = 1, Jack = 11, Queen = 12, King = 13
* @param aNumber = Number of the Card
* @return Number of Card
*/
public Card(int aNumber)
{
if (aNumber >=1 && aNumber
{
this.myNumber = aNumber;
} else {
System.err.println(aNumber + "is not valid.");
System.exit(1); //tells program error and exits program
}
}
public int getNumber()
{
return myNumber;
}
/**
* Switch statement for deck
* @return numStr
*/
public String toString() //returns name of card
{
String numStr = "Error";
switch(this.myNumber)
{
case 1:
numStr = "Ace";
break;
case 2:
numStr = "Two";
break;
case 3:
numStr = "Three";
break;
case 4:
numStr = "Four";
break;
case 5:
numStr = "Five";
break;
case 6:
numStr = "Six";
break;
case 7:
numStr = "Seven";
break;
case 8:
numStr = "Eight";
break;
case 9:
numStr = "Nine";
break;
case 10:
numStr = "Ten";
break;
case 11:
numStr = "Jack";
break;
case 12:
numStr = "Queen";
break;
case 13:
numStr = "King";
break;
}
return numStr;
}
}
import java.util.Random;
/**
* This class displays the Deck with random shuffling and decreases number of cards in deck
* uses methods Deck(int, boolean), dealNextCard(), shuffle():void and printDeck(int)
* @author suzettepalmer
* @version 8.1
* @since 10/23/2017
*/
public class Deck {
/**
* array of cards in deck, where top card is the first index
*/
private Card [] myCards;
/**
* Number of cards that are in deck
*/
private int numCards;
public Deck(int numCards, boolean shuffle)
{
Random rng = new Random();
Card temp; //temporary value used to perform swap to prevent data from being lost
int j;
for (int i = 0; i
j = rng.nextInt(this.numCards);
temp = this.myCards[i];
this.myCards[i] = this.myCards[j];
this.myCards[j] = temp;
}
this.numCards = 52;
this.myCards = new Card [this.numCards];
int c = 0; // initiate card index
for (int n = 1; n
{
this.myCards[c] = new Card(n); //add new card to deck and increment
c++;
}
}
/**
* @param shuffle: shuffle before play
* @return void
*/
public void shuffle()
{
/**
* Deal top card
* @return dealt card
*/
}
public Card dealNextCard() {
Card top = this.myCards [0];
for (int c = 1; c
{
this.myCards[c-1] = this.myCards[c];
}
this.myCards[this.numCards-1] = null; //when last card is used.
this.numCards--; //decrement the number of cards in deck
return top;
}
/**
* Print top of cards on deck
* @param numToPrint (# of cards from the top of deck to print)
*/
public void printDeck(int numToPrint)
{
for (int c =0; c { //printf gives string and formatting guidelines; allows formating string, %3d print integer with width of 3 and padded to L with spaces, %s print string print new line System.out.printf("% 3d/%d%s ", c+1, this.numCards, this.myCards[c].toString()); System.out.printf(" "); } } /** * initiate hand and set max value of cards player/dealer can have * uses methods Player(String), emptyHand(), addCard(Card), getHandSum(), printHand(boolean) * @author suzettepalmer * @version 8.1 * @since 10/23/2017 */ public class Player { private String name; private Card[] hand = new Card [10]; //allocating memory block for cards drawn private int numCards; public Player (String aName) { this.name = aName; //set player's hand to zero this.emptyHand(); } /** * Sets parameters for game initiation * @param player's hand is 0. * @return null */ public void emptyHand() { for (int c = 0; c { this.hand[c] = null; } this.numCards = 0; //defaults to 0 if not specified } /** * return handSum within bounds for each draw. If over 21, bust. * @param aCard * @return handSum */ public boolean addCard(Card aCard) { if (this.numCards == 10) { System.err.printf("%s's hand already has 10 cards; " + "cannot add another ", this.name); System.exit(1); } this.hand[this.numCards] = aCard; this.numCards++; return (this.getHandSum() } public int getHandSum() { int handSum = 0; int cardNum; int numAces = 0; for (int c = 0; c cardNum = this.hand[c].getNumber(); if (cardNum == 1) { numAces++; handSum += 11; } else if (cardNum > 10) { handSum += 10; } else { handSum += cardNum; } } while (handSum >21 && numAces > 0) { handSum -= 10; numAces--; } return handSum; } /** * Print First Card * @param showFirstCard */ public void printHand(boolean showFirstCard) { System.out.printf("%s's cards : ", this.name); for (int c = 0; c System.out.printf(" %s ", this.hand[c].toString()); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
