* To be written in Java Write a program that plays a simple dice game between th
ID: 672065 • Letter: #
Question
* To be written in Java
Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop should repeat 10 times. Each iteration of the loop should do the following:
1) Generate a random integer in the range of 1 through 6. This is the value of the computer's die.
2) Generate another random integer in the range of 1 through 6. This is the value of the user's die.
3) The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.)
As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user.
Explanation / Answer
import java.util.Scanner;
public class DiceGame
{
public static void main( String [] args )
{
//Rules of the Game
System.out.println("______________________________________");
System.out.println("/ Rules of the Game /");
System.out.println("/ ----------------- /");
System.out.println("/ 1)It's you vs computer. /");
System.out.println("/ 2)You play by rolling the dice. /");
System.out.println("/ 3)The first player to reach 100 /");
System.out.println("/ points wins. /");
System.out.println("/ 4)When a player rolls a 1 /");
System.out.println("/ the turn is over. /");
System.out.println("/ 5)The computer's turn is over /");
System.out.println("/ when turn total reach 20 points /");
System.out.println("/ in a single turn. /");
System.out.println("______________________________________");
PairOfDice d1 = new PairOfDice(); //Creating PairOfDice object
int turnTotal = 0;
int playerTotal = 0; //computer's total
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; // named of what will take answer from user
// first do-while loop is for repeating the change between user and computer
do{
if (playerTotal <= winner && computerTotal <= 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") && playerTotal <= winner && computerTotal <= 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
{
turnTotal = turnTotal + d1.getDiceSum();
playerTotal = playerTotal + d1.getDiceSum();
System.out.println("Your Turn Total: " + turnTotal);
System.out.println("Your Grand Total: " + playerTotal);
}
}
}
while (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner);
turnTotal = 0; // turntotal assigned to 0 again.
System.out.println();
System.out.println("Your Grand Total is: " + playerTotal);
System.out.println("The Computer's Grand Total is: " + computerTotal);
System.out.println();
//Begin the Computer's turn
int endComputerTurn = 20;//when to end computer's turn
turnOver = 1; //what die equals for turn to be over
int answercomp = 1;
do
{
if (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= 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. Their turn is over.");
answercomp = 0;
}
else
{
turnTotal = turnTotal + d1.getDiceSum();
computerTotal = computerTotal + d1.getDiceSum();
System.out.println("The Computer's Turn Total is: " + turnTotal);
System.out.println("The Computer's Grand Total is: " + computerTotal);
}
}
}
while (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner);
turnTotal = 0; //turntotal assigned to 0 again.
if (playerTotal <= winner || computerTotal <= winner)
{
System.out.println();
System.out.println("The Computer's Grand Total is: " + computerTotal);
System.out.println("Your Grand Total is: " + playerTotal);
System.out.println();
}
else
{
System.out.println();
System.out.println();
}
}
}
while(playerTotal <= winner && computerTotal <= winner);
// if-else statements to check if there is a winner
if (playerTotal >= winner)
System.out.println("You win!");
else
System.out.println("You lose ): ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.