Question
Create a program to play the game High Low: Shuffle the deck & display the first card. The user should guess whether the next card
Create a program to play the game High Low:
Shuffle the deck & display the first card.
The user should guess whether the next card drawn will be higher or lower than the first card.
The next card is then drawn from the deck & displayed.
If the user predicts higher or lower correctly, then the game keeps going and they predict whether the next card will be higher or lower.
As soon as the user makes an incorrect prediction the game ends and the number of correct predictions is printed out.
Use this code as a starting point and modify:
import java.util.Scanner;
public class CardTest {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Suit[] allSuits = Suit.values();
Rank[] allRanks = Rank.values();
Deck deck = new Deck();
deck.shuffle();
//deck.printDeck();
// initial player hand
Card player1 = deck.draw();
Card player2 = deck.draw();
System.out.println("Player's first card: " + player1);
System.out.println("Player's second card: " + player2);
int pScore = player1.getValueBJ() + player2.getValueBJ();
//System.out.println("Player's initial score " + pScore);
boolean pAce = false;
if ((player1.getValueBJ() ==1)|| (player2.getValueBJ()==1)) {
pAce = true;
}
// initial Computer hand
Card comp1 = deck.draw();
Card comp2 = deck.draw();
System.out.println(" Computer's first card: " + comp1);
System.out.println("Computer's second card: " + comp2);
int cScore = comp1.getValueBJ() + comp2.getValueBJ();
//System.out.println("Computer's initial score " + cScore);
boolean cAce = false;
if ((comp1.getValueBJ() ==1)|| (comp2.getValueBJ()==1)) {
cAce = true;
}
// let user draw more cards
System.out.println("Enter 'yes' to draw again");
String input = scnr.next();
while (input.equals("yes")) {
Card pCard = deck.draw();
System.out.println(" Player's next card: " + pCard);
pScore = pScore + pCard.getValueBJ();
//System.out.println("Player's new score " + pScore);
System.out.println("Enter 'yes' to draw again");
input = scnr.next();
}
// update scores for aces
if (pAce && (pScore <= 11)) {
pScore += 10;
}
if (cAce && (cScore <= 11)) {
cScore += 10;
}
// let computer draw more cards
while (cScore < 15) {
Card cCard = deck.draw();
System.out.println(" Computer's next card: " + cCard);
cScore = cScore + cCard.getValueBJ();
//System.out.println("Computer's new score " + cScore);
}
// Decide who wins-----------------
System.out.println("Player's final score " + pScore);
System.out.println("Computer's final score " + cScore);
if ((cScore > 21) && (pScore > 21)) {
System.out.println("Both went bust");
}
else if (cScore > 21) {
System.out.println("Computer busted, player wins");
}
else if (pScore > 21) {
System.out.println("Player busted, computer wins");
}
else if (pScore > cScore) {
System.out.println("Player Wins");
}
else if (cScore > pScore) {
System.out.println("Computer Wins");
}
else {
System.out.println("Tie game");
}
}
}
Seperate Card File (does not need modifications:)
public class Card { // attributes private Rank rank; private Suit suit; // constructors public Card(){ rank = Rank.QUEEN; suit = Suit.HEARTS; } public Card(Rank rank, Suit suit) { this.rank = rank; this.suit = suit; } // accessors public Rank getRank() { return rank; } public Suit getSuit() { return suit; } // toString public String toString() { return rank + " of " + suit; } // get value public int getValue() { return rank.ordinal() + 1; } // get value for War public int getValueWar() { if (rank.ordinal()==0) { return 14; } return rank.ordinal() + 1; } // get value for BlackJack public int getValueBJ() { int val = rank.ordinal() + 1; if (val > 10) { // facecards should all have value 10 val = 10; } return val; } }
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