A Game of 21 Using Dice For this assignment, you will write a program that lets
ID: 3698089 • Letter: A
Question
A Game of 21 Using Dice
For this assignment, you will 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 or not 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 whether he or she wants to roll. (Use the Die class that was shown in Code Listing 6-14 to simulate the dice.)
• The loop keeps a running total of both the computer’s 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 the player with the most points, without going over 21, wins.
The play should look like the attached, except you should be using a GUI.
Welcome to DrJava. A Game of Twenty One
> run AGameOfTwentyOne
Roll the dice? (y/n) : y
Points: 6
Roll the dice? (y/n) : y
Points: 11
Roll the dice? (y/n) : y
Points: 22
Game Over
User's Points: 22
Computer's Points: 17
The computer wins!
>
Explanation / Answer
static class Die {
//declaring and intilazing variablies as private
private final int NUMBER_OF_SIDES = 6;
private int score;
//default constructor
Die()
{
role();
}
//role method with return type void
public void role()
{
Random randomScore = new Random();
score = randomScore.nextInt(NUMBER_OF_SIDES) + 1;
}
//getScore method for returning score
public int getScore()
{
return score;
}
public static int getRoleScore()
{
Die die = new Die();
int score1 = die.getScore();
die.role();
int score2 = die.getScore();
return score1 + score2;
}
public static boolean limit(int score)
{
if (score > 21)
{
return false;
}
else
{
return true;
}
}
public static boolean play()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Role the dice? (y/n) : ");
String userResponse = keyboard.nextLine(); // Get a line of input.
char letter = userResponse.charAt(0); // Get the first character.
if (letter == 'Y' || letter == 'y') {
return true;
}
else
{
return false;
}
}
public static void displayResults(int score2, int score1)
{
// Display the computer and user's points.
System.out.println(" Game Over ");
System.out.println("User's Points: " + score1);
System.out.println("Computer's Points: " + score2);
System.out.println(winningMsg(score2, score1));
}
public static String winningMsg(int score2, int score1)
{
if (score1 > score2 && limit(score1))
{
return "Congrations, you have win!!!";
} else if (limit(score1)
&& !limit(score2)
{
return " Congrations, you have win!!!";
}
else if (score1 == 21 && score2 != 21)
{
return "Congrats, you win!!!";
}
else if (score1 == score2)
{
return "Tie game!";
}
else if (!limit(score1)
&& !limit(score2))
{
return "This game has ended without a winner.";
}
else
{
return "The computer wins!";
}
}
public static void main(String[] args)
{
//points for computer and initialized with zero
int points1 = 0;
//points for user and initialized with zero
int points2 = 0;
while (play())
{
points1 += getRoleScore();
points2 += getRoleScore();
// break the game if either user or computer go over the limit
if (!limit(points2) || !limit(points1))
{
break;
}
System.out.println("User Points: " + points2);
}
if (points2 == 0 && points1 == 0) {
System.out.println("we have chance play to win!!!");
}
else
{
displayResults(points1, points2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.