Java Code, use the code given, Put it in a file A Game of 21 Write a program tha
ID: 674411 • Letter: J
Question
Java Code, use the code given, Put it in a file
A Game of 21
Write a program that uses the Die class that was presented in Chapter 4 to write a program
that lets the user play against the computer in a variation of the popular blackjack card
game. In this variation of the game, two six-sided dice are used instead of cards. The dice
are rolled, and the player tries to beat the computer’s hidden total without going over 21.
Here are some suggestions for the game’s design:
Each round of the game is performed as an iteration of a loop that repeats as long as
the player agrees to roll the dice, and the player’s total does not exceed 21.
At the beginning of each round, the program will ask the user whether he or she wants
to roll the dice to accumulate points.
During each round, the program simulates the rolling of two six-sided dice. It rolls
the dice first for the computer, and then it asks the user if he or she wants to roll. (Use
the Die class that was demonstrated in Chapter 4 to simulate the dice).
The loop keeps a running total of both the computer and the user’s points.
The computer’s total should remain hidden until the loop has finished.
After the loop has finished, the computer’s total is revealed, and whoever the player
with the most points without going over 21 wins.
Die Class
import java.util.Random;
/**
* The Die class simulates a six-sided die.
*/
public class Die
{
private int sides; // Number of sides
private int value; // The die's value
/**
* The constructor performs an initial
* roll of the die. The number of sides
* for the die is passed as an argument.
*/
public Die(int numSides)
{
sides = numSides;
roll();
}
/**
* The roll method simlates the rolling of
* the die.
*/
public void roll()
{
// Create a Random object.
Random rand = new Random();
// Get a random value for the die.
value = rand.nextInt(sides) + 1;
}
/**
* The getSides method returns the
* number of sides for the die.
*/
public int getSides()
{
return sides;
}
/**
* The getValue method returns the
* value of the die.
*/
public int getValue()
{
return value;
}
}
/**
* This program simulates the rolling of dice.
*/
Explanation / Answer
public class Game of 21
{
static class Die
{
private final int NUMBER_OF_SIDES = 6;
private int value;
Die()
{
roll();
}
public void roll()
{
Random randomValue = new Random();
value = randomValue.nextInt(NUMBER_OF_SIDES) + 1;
}
public int getValue()
{
return value;
}
}
public static int getRollValue()
{
Die die = new Die();
int roll1Value = die.getValue();
die.roll();
int roll2Value = die.getValue();
return roll1Value + roll2Value;
}
public static boolean isUnderGameLimit(int value)
{
if (value > 21)
{
return false;
}
else
{
return true;
}
}
public static boolean playAgain()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Do you want to roll the dice? (y/n) : ");
String userResponse = keyboard.nextLine();
char letter = userResponse.charAt(0);
if (letter == 'Y' || letter == 'y')
{
return true;
}
else
{
return false;
}
}
public static void displayResults(int computerScore, int userScore)
{
System.out.println(" Game end ");
System.out.println("User's Points: " + userScore);
System.out.println("Computer's Points: " + computerScore);
System.out.println(getWinnerMessage(computerScore, userScore));
}
public static String getWinnerMessage(int computerScore, int userScore)
{
if (userScore > computerScore && isUnderGameLimit(userScore))
{
return "Congrats, you won!!!";
}
else if (isUnderGameLimit(userScore) && !isUnderGameLimit(computerScore))
{
return "Congrats, you win!!!";
}
else if (userScore == 21 && computerScore != 21)
{
return "Congrats, you win!!!";
}
else if (userScore == computerScore)
{
return "Game tied!";
}
else if (!isUnderGameLimit(userScore) && !isUnderGameLimit(computerScore))
{
return "HUUh!!The game ended without winner.";
}
else
{
return "computer won!";
}
}
public static void main(String[] args)
{
int computerPoints = 0;
int userPoints = 0;
while (playAgain())
{
computerPoints += getRollValue();
userPoints += getRollValue();
if (!isUnderGameLimit(userPoints)|| !isUnderGameLimit(computerPoints))
{
break;
}
System.out.println("User Points: " + userPoints);
}
if (userPoints == 0 && computerPoints == 0)
{
System.out.println("Gotta play to win!!!");
}
else
{
displayResults(computerPoints, userPoints);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.