In chapter 4 you created a Die class from which you could instantiate an object
ID: 3668974 • Letter: I
Question
In chapter 4 you created a Die class from which you could instantiate an object containing a random value from 1 through 6. Now use the class to create a simple dice game in which the user chooses a number between 2 (the lowest total possible from two dice) and 12 (the highest total possible). The user "rolls" two dice up to three times. If the number chosen by the user comes up, the user wins and the game ends. If the number does not come up within three rolls, the computer wins. Save the application as TwoDice3.java.
Here's what I have so far:
public class Die
{
int HIGHEST_DIE_VALUE = 12;
int LOWEST_DIE_VALUE = 2
private int randomValue = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +
LOWEST_DIE_VALUE);
public int getRandomValue()
{
return randomValue;
}
public void setRandomValue(int value)
{
randomValue = value;
}
}
Explanation / Answer
Dice.java
public class Dice
{
private int dice = 0;
public int roll = 0;
// this constructor is called as soon as the class is instantiated.
public Dice(int dice) // connects the dice that the user enters to the private data member dice in the dice class.
{
this.dice = dice;
}
public void lowRange() // this method displays the low range for the number of dice specified.
{
if (dice == 1)
{
System.out.printf("Low Range: %d - %d. ", 1, 3);
}
else
{
System.out.printf("Low Range: %d - %d. ", ((1 + dice) - 1), ((3 * dice)));
}
}
public void highRange() // this method displays the high range for the number of dice specified.
{
if (dice == 1)
{
System.out.printf("High Range: %d - %d. ", 4, 6);
}
else
{
System.out.printf("High Range: %d - %d. ", ((3 * dice) + 1), ((6 * dice)));
}
}
}
Game.java
// header files
import java.util.Random;
import java.util.Scanner;
public class Game
{
public static void main(String[] args)
{
int dice;
int win = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("How many times would you like to play the game? "); // user enters how many times they want to play.
count = input.nextInt();
int []roll = new int [count]; // array used to keep track of rolls.
int []guess = new int [count]; // array used to keep track of guesses.
for (int i = 0; i < count; i++) // game plays for the amount of games specified by user.
{
System.out.print("Enter the number of dice to roll: "); // user enters number of dice to roll.
dice = input.nextInt();
Dice game = new Dice(dice); // instantiate the class Dice, to use the methods in the Dice class.
// the single parameter (in this case dice) is the amount of dice to roll.
if (dice == 1) // gets the roll if dice is 1.
{
int range = (dice * ((6 - 1)) + 1);
roll[i] = (int)(Math.random() * range) + 1;
}
else // gets the roll if dice is not 1
{
Random r = new Random();
roll[i] = r.nextInt((6 * dice) - (dice) + 1) + dice;
}
System.out.print("You rolled the dice, guess the number it landed on."
+ " In order to win your guess must be (+ or -) 3 units from the roll or equal to roll): ");
// guess has to be equal to the roll or less
guess[i] = input.nextInt(); // than or equal to (roll + 3)
// and greater than or equal to (roll - 3).
System.out.println(roll[i]);
game.lowRange();
game.highRange();
if ((roll[i] == guess[i] || (roll[i] - 3 <= guess[i] && guess[i] <= roll[i] + 3))) // guess has to be correct +-3 units to win.
{
System.out.println("You win! +1 point! ");
win++; // keeps track of win.
}
else
{
System.out.println("You lose! -1 point! ");
win--; // keeps track of loss
}
}
System.out.printf("You played the game %d time(s). ", count); // displays the number of games played.
System.out.printf("You have %d point(s). ", win); //displays total points.
System.out.println("The roll and guess for each game is:");
for (int i = 0; i < count; i++)
{
System.out.printf("%d) roll: %d. guess: %d ", i + 1, roll[i], guess[i] ); // uses a for loop to output the roll and guess for
// each game.
}
}
}
Sample output
How many times would you like to play the game? 3
Enter the number of dice to roll: 6
You rolled the dice, guess the number it landed on.
In order to win your guess must be (+ or -) 3 units from the roll or equal to roll): 3
7
Low Range: 6 - 18.
High Range: 19 - 36.
You lose! -1 point!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.