For this assignment, you will write a program that lets the user play against th
ID: 3748830 • Letter: F
Question
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’shidden 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 orshe wants to roll the dice to accumulate points.
During each round, the program simulates the rolling of two six-sided dice. It rolls thedice 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.
Explanation / Answer
import java.util.;
/*
Represents one die with faces showing values between 1 and the
number of faces on the die.
/
class Die
{
private static int MIN_SIDES = 4; // no less than 4 sides on a die
private int sides ; // The number of sides
private int value; // The value of the die
private Random rand; // Random number generator
/*
The Default constructor calls the roll method
creates the Random object, sets number of sides to 6 and
sets the intitial value of the die to a random
number.
/
public Die()
{
// create Random object
rand = new Random();
// set number of side on die
sides = 6;
// Call the roll method to randomly
// set the value of the die.
roll();
}
/*
Alternate constructor calls the roll allows user to specify
number of side son die. The method creates the Random object,
sets number of sides to value user specified and sets the intitial
value of the die to a random number.
@param numSides The number of sides on the die
/
public Die(int numSides)
{
// create Random object
rand = new Random();
// set number of side on die
sides = numSides;
// Call the roll method to randomly
// set the value of the die.
roll();
}
/*
The roll method sets the value of the die
to a random number.
/public void roll()
{
// Set the value to a random number.
value = rand.nextInt(sides) + 1;
}
/*
The getValue method returns the value
of the die.
*/
public int getValue()
{
return value;
}
}
public class game
{
public static void main(String[] args)
{
Die D1 = new Die(6);
Die D2 = new Die(6);
Scanner in = new Scanner(System.in);
int computer_sum =0;
int my_sum =0;
char ans='Y';
char acc = 'Y';
while(true)
{
System.out.println("Do you want to roll the dice (Y or N) :");
ans = in.next().charAt(0);
if(my_sum > computer_sum)
{
System.out.println("You Won the Game :");
System.out.println("You Total Score :" + my_sum);
System.out.println("Computer Total Score :" + computer_sum);
break;
}
if((ans=='y' || ans =='Y') && my_sum<21)// this is our turn
{
D1.roll();
D2.roll();
my_sum = my_sum + D1.getValue() + D2.getValue();
System.out.println("You Total Score :" + my_sum);
}
else // this is computer turn.
{
D1.roll();
D2.roll();
computer_sum = computer_sum + D1.getValue() + D2.getValue();
}
} // end while
} // end main
} // end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.