Reputation: 1
I am working on coding a program to roll dice. I am very new to java still as I am taking classes for schooling. I am using multiple classes in different packages for this program, what I am trying to figure out is, in one class, for my package pairOfDice, I have created objects in a class pairOfDice, die1 and die2. Now I have another package rollDice, adn my goal is to use the pairOfDice class to roll two die and display the rolls. what I am struggling with is how to exactly do that. When I am rolling the die my results display as if I am only rolling one die. I have made adjustments to display two die every roll, although feel as if I am not doing it in a more proficient sort of way.
package die;
import java.util.Scanner;
/**
*
* @author <a href= "mailto:[email protected]" >Aaron Davis</a>
*/
public class RollDice
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PairOfDice dice = new PairOfDice();
// get amount of rolls from user
System.out.print("How many rolls do you want? ");
int numRolls = scan.nextInt();
int diceOne, diceTwo;
int boxCar, snakeEyes;
int j = 0, k = 0;
// rolls the dice the requested amount of times
for (int i = 0; i < numRolls; i++)
{
// first die to roll
diceOne = dice.roll();
// second die to roll
diceTwo = dice.roll();
// display rolled dice
System.out.println(diceOne + " " + diceTwo + "\n");
// store and display pairs of 1 rolls
if (diceOne == 1 && diceTwo == 1)
{
snakeEyes = ++j;
System.out.println("\nThe number of snake eyes you have is: "
+ snakeEyes + "\n");
}
// store and display pairs of 6 rolls
if (diceOne == 6 && diceTwo == 6)
{
boxCar = ++k;
System.out.println("\nThe number of box cars you have is: "
+ boxCar + "\n");
}
}
}
}
******************************************************************************
/*
the integers diceOne and diceTwo are my workarounds, my other package contains
public class PairOfDice extends Die
{
Die die1, die2;
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
public PairOfDice(int face)
{
die1 = new Die(face);
die2 = new Die(face);
}
}
*/
******************************************************************************
// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die
Upvotes: 0
Views: 650
Reputation: 1218
The PairOfDice
class does not represent your model, which is "a pair of dice".
If you have a pair of dice, when you are rolling them, you get two different numbers, thus either:
roll
method must return two values. You can use a RollResult
bean containing the two values for instanceroll
method can return just an integer from 2 to 12 and you can speculate over the dice rolling based on the sum of them: in your case it is always possible because you get a sum of 2 if and only if your dices are 1, 1; similarily, if you get a sum of 12 if and only if your dices are 6, 6.
It would not work, for instance, if you would test against the condition "dice 1=3, dice2=4", as there are many combinations of rollings returning 3+4=7Hope this helps.
Based on the comments, we have to proceed with the first solution.
Here it is an example that implements domain immutable objects and roll
domain function that return the result of the roll
action against a dice.
Here in the example I show the possibilities of having multiple types of dices.
import java.util.*;
import java.util.stream.Collectors;
public class RollingDices {
private static final Random RND = new Random();
private static interface Dice {
public int roll();
}
private static class UniformDice implements Dice {
public int roll() {
return RND.nextInt(6) + 1;
}
}
private static class TrickyDice implements Dice {
private final int value;
public TrickyDice(int value) {
this.value = value;
}
public int roll() {
return value;
}
}
private static class ProbabilityTableDice implements Dice {
private final Double[] probabilities;
public ProbabilityTableDice(Double ... probabilities) {
if (Arrays.stream(probabilities).mapToDouble(Double::doubleValue).sum() != 1.0) {
throw new RuntimeException();
}
this.probabilities = probabilities;
}
public int roll() {
final double randomValue = RND.nextDouble();
double curValue = 0.0;
for (int i = 0; i < this.probabilities.length; i++) {
curValue += this.probabilities[i];
if (curValue >= randomValue) {
return i + 1;
}
}
throw new RuntimeException();
}
}
private static class CollectionOfDices {
private final Dice[] dices;
public CollectionOfDices(Dice ... dices) {
this.dices = dices;
}
public List<Integer> roll() {
return Arrays.stream(dices).map(Dice::roll).collect(Collectors.toList());
}
}
private static class DicesFactory {
private static final DicesFactory INSTANCE = new DicesFactory();
public static DicesFactory instance() {
return INSTANCE;
}
private DicesFactory() {}
private final Dice uniformDice = new UniformDice();
public Dice createUniformDice() {
return this.uniformDice;
}
public Dice createTrickyDice(int fixedValue) {
return new TrickyDice(fixedValue);
}
public Dice createProbabilityTableDice(Double ... probabilities) {
return new ProbabilityTableDice(probabilities);
}
}
public static void main(String ... args) {
final Scanner scan = new Scanner(System.in);
final CollectionOfDices dice = new CollectionOfDices(
DicesFactory.instance().createUniformDice(),
DicesFactory.instance().createProbabilityTableDice(
0.15, 0.2, 0.3, 0.1, 0.25
)
);
// get amount of rolls from user
System.out.print("How many rolls do you want? ");
int numRolls = scan.nextInt();
int diceOne, diceTwo;
int boxCar, snakeEyes;
int j = 0, k = 0;
// rolls the dice the requested amount of times
for (int i = 0; i < numRolls; i++)
{
final List<Integer> rolls = dice.roll();
// first die to roll
diceOne = rolls.get(0);
// second die to roll
diceTwo = rolls.get(1);
// display rolled dice
System.out.println(diceOne + " " + diceTwo + "\n");
// store and display pairs of 1 rolls
if (diceOne == 1 && diceTwo == 1)
{
snakeEyes = ++j;
System.out.println("\nThe number of snake eyes you have is: "
+ snakeEyes + "\n");
}
// store and display pairs of 6 rolls
if (diceOne == 6 && diceTwo == 6)
{
boxCar = ++k;
System.out.println("\nThe number of box cars you have is: "
+ boxCar + "\n");
}
}
}
}
Upvotes: 0