The Game50 dice game is described in the textbook. Here is the partially written
ID: 3579861 • Letter: T
Question
The Game50 dice game is described in the textbook. Here is the partially written Game50 class. public class Game50{ static final int SIZE = 50; // target score to be exceeded private int bound; // number of allowable rolls to reach 50 private int rollCount; // number of rolls private int total; // accumulated dice roll sum private boolean winner; // game a win or loss? public Game50(int rollBound){ bound = rollBound; // rollBound value supplied in constructor call in driver class } public void playGame(){ initializeGame(); while(!gameOver()){ advancePlay(); showGame(); } judgeAndReport(); }
Explanation / Answer
I am writing the dice game code in java below check it once
import java.util.Scanner; // This package is used for scanner method
public class Game50
{
public static void main( String [] args )
{
pair_of_dice d1 = new pair_of_dice(); //Creating pair_of_dice object
int turnScore = 0;
int computerScore = 0; //computer's Score
int playerScore = 0; //your Score
int turnOver = 1; //when to give up die
int winner = 100; // amount to be reached before winning
Scanner in = new Scanner( System.in );
String answer;
// This loop will used for turns between user and computer
do{
if (playerScore <= winner && computerScore <= winner)
{
System.out.println("Your turn.");
// do-while loop for the player's turn.
do
{
System.out.println("Type 'y' if ready and 'n' to end turn.");
answer = in.next();
if (answer.equalsIgnoreCase("y") && playerScore <= winner && computerScore <= winner)
{
d1.roll();
d1.getDie1();
d1.getDie2();
d1.toString();
System.out.println(d1);
// if and else statement to figure out whether user's turn is over or not.
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver){
System.out.println("You rolled a 1. Your turn is over.");
System.out.println("Please type 'done' when you are ready to turn the dice over to the Computer.");
answer = in.next();
}
else
{
turnScore = turnScore + d1.getDiceSum();
playerScore = playerScore + d1.getDiceSum();
System.out.println("Your Turn Score: " + turnScore);
System.out.println("Your Grand Score: " + playerScore);
}
}
}
while (answer.equalsIgnoreCase("y") && playerScore <= winner && computerScore <= winner);
turnScore = 0; // turnScore assigned to 0 again.
System.out.println();
System.out.println("Your Grand Score is: " + playerScore);
System.out.println("The Computer's Grand Score is: " + computerScore);
System.out.println();
//Computer`s turn
int endComputerTurn = 20;//when to end computer's turn
turnOver = 1; // if die =1 then turn will be over
int answercomp = 1;
do
{
if (turnScore <= endComputerTurn && answercomp == 1 && playerScore <= winner && computerScore <= winner)
{
d1.roll();
d1.getDie1();
d1.getDie2();
d1.toString();
System.out.println(d1);
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver)
{
System.out.println("The Computer rolled a 1. It`s turn is over.");
answercomp = 0;
}
else
{
turnScore = turnScore + d1.getDiceSum();
computerScore = computerScore + d1.getDiceSum();
System.out.println("The Computer's Turn Score is: " + turnScore);
System.out.println("The Computer's Grand Score is: " + computerScore);
}
}
}
while (turnScore <= endComputerTurn && answercomp == 1 && playerScore <= winner && computerScore <= winner);
turnScore = 0; //turnScore assigned to 0 again.
if (playerScore <= winner || computerScore <= winner)
{
System.out.println();
System.out.println("The Computer's Grand Score is: " + computerScore);
System.out.println("Your Grand Score is: " + playerScore);
System.out.println();
}
else
{
System.out.println();
System.out.println();
}
}
}
while(playerScore <= winner && computerScore <= winner);
// if-else statements to check if there is a winner
if (playerScore >= winner)
System.out.println("You win!");
else
System.out.println("You lose ): ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.