Hi, im having trouble writing java code for this assignment. Instructions: Write
ID: 3542866 • Letter: H
Question
Hi, im having trouble writing java code for this assignment.
Instructions:
Write code that plays a simple dice game betweeen you and the computer. The dice you wil be using are very sepcial dice that have 20 sides instead of the usual 6 sided die. When the program runs, a loop should repeat 17 times. (you must use a for loop).
* Generate a random number in the range of 1 through 20. (Computers die)
* Generate a random number in the range of 1 through 20. (Users die)
* The die with the highest value wins. Display this result to the screen.
* Program should also keep count of the number of times the computer wins, and the number of times the user wins.
* Pring out if thagame was a tie.
* Afterloop iterates, the program should display who was the grand winniner as welll as the total number of wins the computer had, the total number of wins the user had, and the total number of ties In the event of a tie the program will state "Tie Game!"
*
I would be very thankful if I could get help on how this code looks
Explanation / Answer
import java.util.Scanner;
002
003
public class DiceGame
004
{
005
public static void main( String [] args )
006
{
007
//Rules of the Game
008
System.out.println("______________________________________");
009
System.out.println("/ Rules of the Game /");
010
System.out.println("/ ----------------- /");
011
System.out.println("/ 1)It's you vs computer. /");
012
System.out.println("/ 2)You play by rolling the dice. /");
013
System.out.println("/ 3)The first player to reach 100 /");
014
System.out.println("/ points wins. /");
015
System.out.println("/ 4)When a player rolls a 1 /");
016
System.out.println("/ the turn is over. /");
017
System.out.println("/ 5)The computer's turn is over /");
018
System.out.println("/ when turn total reach 20 points /");
019
System.out.println("/ in a single turn. /");
020
System.out.println("______________________________________");
021
022
PairOfDice d1 = new PairOfDice(); //Creating PairOfDice object
023
024
int turnTotal = 0;
025
int computerTotal = 0; //your total
026
int playerTotal = 0; //computer's total
027
int turnOver = 1; //when to give up die
028
int winner = 100; // amount to be reached before winning
029
030
Scanner in = new Scanner( System.in );
031
String answer; // named of what will take answer from user
032
033
// first do-while loop is for repeating the change between user and computer
034
do{
035
if (playerTotal <= winner && computerTotal <= winner)
036
{
037
System.out.println("Your turn.");
038
039
// do-while loop for the player's turn.
040
do
041
{
042
System.out.println("Type 'y' if ready and 'n' to end turn.");
043
answer = in.next();
044
045
if (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner)
046
{
047
d1.roll();
048
d1.getDie1();
049
d1.getDie2();
050
d1.toString();
051
System.out.println(d1);
052
053
// if and else statement to figure out whether user's turn is over or not.
054
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver){
055
System.out.println("You rolled a 1. Your turn is over.");
056
System.out.println("Please type 'done' when you are ready to turn the dice over to the Computer.");
057
answer = in.next();
058
}
059
else
060
{
061
turnTotal = turnTotal + d1.getDiceSum();
062
playerTotal = playerTotal + d1.getDiceSum();
063
System.out.println("Your Turn Total: " + turnTotal);
064
System.out.println("Your Grand Total: " + playerTotal);
065
}
066
}
067
}
068
069
while (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner);
070
turnTotal = 0; // turntotal assigned to 0 again.
071
System.out.println();
072
System.out.println("Your Grand Total is: " + playerTotal);
073
System.out.println("The Computer's Grand Total is: " + computerTotal);
074
System.out.println();
075
076
//Begin the Computer's turn
077
int endComputerTurn = 20;//when to end computer's turn
078
turnOver = 1; //what die equals for turn to be over
079
int answercomp = 1;
080
081
do
082
{
083
if (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner)
084
{
085
d1.roll();
086
d1.getDie1();
087
d1.getDie2();
088
d1.toString();
089
System.out.println(d1);
090
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver)
091
{
092
System.out.println("The Computer rolled a 1. Their turn is over.");
093
answercomp = 0;
094
}
095
096
else
097
{
098
turnTotal = turnTotal + d1.getDiceSum();
099
computerTotal = computerTotal + d1.getDiceSum();
100
System.out.println("The Computer's Turn Total is: " + turnTotal);
101
System.out.println("The Computer's Grand Total is: " + computerTotal);
102
}
103
}
104
}
105
106
while (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner && computerTotal <= winner);
107
turnTotal = 0; //turntotal assigned to 0 again.
108
109
if (playerTotal <= winner || computerTotal <= winner)
110
{
111
System.out.println();
112
System.out.println("The Computer's Grand Total is: " + computerTotal);
113
System.out.println("Your Grand Total is: " + playerTotal);
114
System.out.println();
115
}
116
117
else
118
{
119
System.out.println();
120
System.out.println();
121
}
122
}
123
}
124
125
while(playerTotal <= winner && computerTotal <= winner);
126
127
// if-else statements to check if there is a winner
128
if (playerTotal >= winner)
129
System.out.println("You win!");
130
else
131
System.out.println("You lose ): ");
132
133
}
134
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.