wo six-sided dice are used instead of cards. The dice are rolled, and the player
ID: 3543436 • Letter: W
Question
wo 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 or not 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 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.
here is what i have for the dice class. I keep getting errors that say class die is public. should be declared in Die.java i just need the rest of the code.
public class Die;
{
private final int SIDES = 6;
private int value;
Die()
{
roll();
}
public void roll()
{
Random rand = new Random();
value = rand.nextInt(SIDES) + 1;
}
public int getValue()
{
return value;
}
Explanation / Answer
please rate - thanks
any questions ask
each class must be in its own file
so you will have 2 files
import java.util.*;
public class Die
{
private final int SIDES = 6;
private int value;
Die()
{
roll();
}
public void roll()
{
Random rand = new Random();
value = rand.nextInt(SIDES) + 1;
}
public int getValue()
{
return value;
}
}
-----------------------------------------
import java.util.*;
public class main
{public static void main(String[] args)
{int totdealer=0,totplayer=0;
boolean again=true;
Scanner in=new Scanner(System.in);
Die d=new Die();
totdealer+=d.getValue();
again=askPlayer(in);
while(again&&totplayer<=21)
{d.roll();
totplayer+=d.getValue();
System.out.println("player total: "+totplayer);
d.roll();
totdealer+=d.getValue();
again=askPlayer(in);
}
System.out.println("computer total: "+totdealer);
winner(totdealer,totplayer);
}
public static boolean askPlayer(Scanner in)
{System.out.print("do you want to roll? y/n ");
char y=in.next().charAt(0);
if(y=='y'||y=='Y')
return true;
else
return false;
}
public static void winner(int d, int p)
{if(d>21)
if(p>21)
System.out.println("no one wins - both scores over 21");
else
System.out.println("you/human win");
else
if(p>21)
System.out.println("computer wins");
else
if(p>d)
System.out.println("you/hyman win");
else
System.out.println("computer wins");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.