10 A Game of TwentyOne For this assignment you will write a
10)- A Game of Twenty-One
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.
twentyone.java
public class twentyone
{
public static void main(String args[])
{
int total = 0;
}
}
die.java
import java.util.Random;
public class die
{
private int sides;
private int value;
public die()
{
sides = 6;
roll();
}
public void roll()
{
Random r = new Random();
value = r.nextInt(sides + 1);
}
public int getSides()
{
return sides;
}
public int getValue()
{
return value;
}
}
| 10)- A Game of Twenty-One 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.
public class twentyone die.java import java.util.Random; public class die public die() public void roll() public int getSides() public int getValue() |
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class twentyone
{
public static void main(String args[])
{
die d1 = new die();
die d2 = new die();
Scanner sc = new Scanner(System.in);
while(true){
int computerTotal = 0;
int userTotal = 0;
while(computerTotal <= 21 && userTotal <=21){
int computerRoll = d1.getValue()+d1.getSides();
// validating computer total
if((computerRoll + computerTotal ) > 21){
break;
}
computerTotal = computerRoll + computerTotal;
// user input
System.out.print(\"Do you want to roll(y/n) ? \");
char c = sc.next().charAt(0);
if(c == \'Y\' || c == \'y\'){
int userRoll = d1.getValue()+d1.getSides();
if((userRoll + userTotal ) > 21){
break;
}
userTotal = userRoll + userTotal;
}
if(computerTotal == userTotal && userTotal <= 21){
System.out.println(\"You won!!!\");
break;
}
}
System.out.println(\"Computet total: \"+computerTotal);
System.out.println(\"User total: \"+userTotal);
// user input
System.out.print(\"\ Do you want to play again(y/n) ? \");
char c = sc.next().charAt(0);
if(c != \'y\' && c != \'Y\')
break;
}
}
}
/*
Sample run:
Do you want to roll(y/n) ? y
You won!!!
Computet total: 7
User total: 7
Do you want to play again(y/n) ? y
Do you want to roll(y/n) ? n
Do you want to roll(y/n) ? y
Do you want to roll(y/n) ? y
Computet total: 21
User total: 14
Do you want to play again(y/n) ? n
*/




