A Game of 21 Write a program that uses the Die class that was presented in Chapt
ID: 3800244 • Letter: A
Question
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.
Explanation / Answer
//DieGame.java
import java.util.*;
public class DieGame
{
private final int TOTAL_SIDES = 6;
private int diceValue;
DieGame()
{
rollDice();
}
public void rollDice()
{
Random r = new Random();
diceValue = r.nextInt(TOTAL_SIDES) + 1;
}
public int getdiceValue()
{
return diceValue;
}
public static int getRolldiceValue()
{
DieGame d = new DieGame();
int roll1diceValue = d.getdiceValue();
d.rollDice();
int roll2diceValue = d.getdiceValue();
return (roll1diceValue + roll2diceValue);
}
public static boolean checkLimit(int diceValue)
{
if (diceValue > 21)
return false;
else
return true;
}
public static boolean againPlay()
{
Scanner scan = new Scanner(System.in);
System.out.print("Roll the dice? (y/n) : ");
String userResponse = scan.nextLine();
char choice = userResponse.charAt(0);
if (choice == 'Y' || choice == 'y')
return true;
else
return false;
}
public static void printResults(int c_poinys, int p_points)
{
System.out.println(" Game Over ");
System.out.println("Player Points: " + p_points);
System.out.println("Computer Points: " + c_poinys);
if (p_points > c_poinys && checkLimit(p_points))
{
System.out.println("You win!");
} else if (checkLimit(p_points)
&& !checkLimit(c_poinys))
{
System.out.println("You win!");
} else if (p_points == 21 && c_poinys != 21)
{
System.out.println("You win!");
} else if (p_points == c_poinys)
{
System.out.println("Game Tie !");
} else if (!checkLimit(p_points)&& !checkLimit(c_poinys))
{
System.out.println("No Winner");
} else
{
System.out.println("Computer wins!");
}
}
}
// DieGameTest.java
public class DieGameTest
{
public static void main(String[] args)
{
DieGame diegame = new DieGame();
int comp_Points = 0;
int player_Points = 0;
while (diegame.againPlay() == true)
{
comp_Points = comp_Points + diegame.getRolldiceValue();
player_Points = player_Points + diegame.getRolldiceValue();
if (!diegame.checkLimit(player_Points)|| !diegame.checkLimit(comp_Points))
{
break;
}
System.out.println("Player Points: " + player_Points);
}
if (player_Points == 0 && comp_Points == 0)
System.out.println("No play, No win!!!");
else
diegame.printResults(comp_Points, player_Points);
}
}
/*
output:
Roll the dice? (y/n) : y
Player Points: 7
Roll the dice? (y/n) : y
Player Points: 14
Roll the dice? (y/n) : y
Player Points: 21
Roll the dice? (y/n) : n
Game Over
Player Points: 21
Computer Points: 21
Game Tie !
Roll the dice? (y/n) : y
Player Points: 11
Roll the dice? (y/n) : y
Player Points: 20
Roll the dice? (y/n) : n
Game Over
Player Points: 20
Computer Points: 18
You win!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.