Create a program to play the game High Low: Shuffle the deck & display the first
ID: 3707943 • Letter: C
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 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;
}
}
Explanation / Answer
public class HighLow {
public static void main(String[] args) {
System.out.println("This program lets you play the simple card game,");
System.out.println("HighLow. A card is dealt from a deck of cards.");
System.out.println("You have to predict whether the next card will be");
System.out.println("higher or lower. Your score in the game is the");
System.out.println("number of correct predictions you make before");
System.out.println("you guess wrong.");
System.out.println();
int gamesPlayed = 0; // Number of games user has played.
int sumOfScores = 0; // The sum of all the scores from
// all the games played.
double averageScore; // Average score, computed by dividing
// sumOfScores by gamesPlayed.
boolean playAgain; // Record user's response when user is
// asked whether he wants to play
// another game.
do {
int scoreThisGame; // Score for one game.
scoreThisGame = play(); // Play the game and get the score.
sumOfScores += scoreThisGame;
gamesPlayed++;
TextIO.put("Play again? ");
playAgain = TextIO.getlnBoolean();
} while (playAgain);
averageScore = ((double)sumOfScores) / gamesPlayed;
System.out.println();
System.out.println("You played " + gamesPlayed + " games.");
System.out.printf("Your average score was %1.3f. ", averageScore);
} // end main()
private static int play() {
Deck deck = new Deck(); // Get a new deck of cards, and
// store a reference to it in
// the variable, deck.
Card currentCard; // The current card, which the user sees.
Card nextCard; // The next card in the deck. The user tries
// to predict whether this is higher or lower
// than the current card.
int correctGuesses ; // The number of correct predictions the
// user has made. At the end of the game,
// this will be the user's score.
char guess; // The user's guess. 'H' if the user predicts that
// the next card will be higher, 'L' if the user
// predicts that it will be lower.
deck.shuffle(); // Shuffle the deck into a random order before
// starting the game.
correctGuesses = 0;
currentCard = deck.dealCard();
TextIO.putln("The first card is the " + currentCard);
while (true) { // Loop ends when user's prediction is wrong.
/* Get the user's prediction, 'H' or 'L' (or 'h' or 'l'). */
TextIO.put("Will the next card be higher (H) or lower (L)? ");
do {
guess = TextIO.getlnChar();
guess = Character.toUpperCase(guess);
if (guess != 'H' && guess != 'L')
TextIO.put("Please respond with H or L: ");
} while (guess != 'H' && guess != 'L');
/* Get the next card and show it to the user. */
nextCard = deck.dealCard();
TextIO.putln("The next card is " + nextCard);
/* Check the user's prediction. */
if (nextCard.getValue() == currentCard.getValue()) {
TextIO.putln("The value is the same as the previous card.");
TextIO.putln("You lose on ties. Sorry!");
break; // End the game.
}
else if (nextCard.getValue() > currentCard.getValue()) {
if (guess == 'H') {
TextIO.putln("Your prediction was correct.");
correctGuesses++;
}
else {
TextIO.putln("Your prediction was incorrect.");
break; // End the game.
}
}
else { // nextCard is lower
if (guess == 'L') {
TextIO.putln("Your prediction was correct.");
correctGuesses++;
}
else {
TextIO.putln("Your prediction was incorrect.");
break; // End the game.
}
}
currentCard = nextCard;
TextIO.putln();
TextIO.putln("The card is " + currentCard);
} // end of while loop
TextIO.putln();
TextIO.putln("The game is over.");
TextIO.putln("You made " + correctGuesses
+ " correct predictions.");
TextIO.putln();
return correctGuesses;
} // end play()
} // end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.