Write a program that uses the Die class that was presented in Chapter 4 to write
ID: 3831692 • Letter: W
Question
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.
what would be the best to fill in the following according to the project above */Description: */ /** * @param * @return * @throws
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
########## Die.java #########################
import java.util.*;
public class Die
{
private final int TOTAL_SIDES = 6;
private int diceValue;
/**
* Default constructor
*/
Die()
{
rollDice();
}
/**
* roll the dice
*/
public void rollDice()
{
Random r = new Random();
diceValue = r.nextInt(TOTAL_SIDES) + 1;
}
/**
* @return diceValue
*/
public int getdiceValue()
{
return diceValue;
}
}
############### DieDemo.java #################
import java.util.Scanner;
public class DieDemo {
/**
* @param d
* @return rolled dice value
*/
public static int getRolldiceValue(Die d)
{
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;
}
/**
* @return boolean based on user input
*/
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!");
}
}
public static void main(String[] args)
{
int comp_Points = 0;
int player_Points = 0;
Die d = new Die(); // creating object of Die
while (againPlay() == true)
{
comp_Points = comp_Points + getRolldiceValue(d);
player_Points = player_Points + getRolldiceValue(d);
if (!checkLimit(player_Points)|| !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
printResults(comp_Points, player_Points);
}
}
/*
Sample run:
Roll the dice? (y/n) : y
Player Points: 10
Roll the dice? (y/n) : y
Player Points: 15
Roll the dice? (y/n) : n
Game Over
Player Points: 15
Computer Points: 17
Computer wins!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.