I'm trying to create a Craps game that rolls two six-sided dice using the Dice class below. The issue is, however, that some of the rolls are greater than 6 which ruins the entire functionality of the game. Here is an output example of what I'm talking about:
Come out roll is (3)(12) sum is 15
Enter point Round with Point Value = 15
Roll is (10)(8) with sum 18
Roll is (9)(3) with sum 12
Roll is (4)(5) with sum 9
Roll is (8)(7) with sum 15
Pass Line bet wins 5
import java.util.Random;
public class Dice {
private int numDice;
private int numSides;
private Random random;
public Dice(int numDice, int numSides) {
this.numDice = numDice;
this.numSides = numSides;
this.random = new Random();
}
public int roll() {
int total = 0;
for (int i = 0; i < numDice; i++) {
total += random.nextInt(numSides) + 1;
}
return total;
}
}
Code I'm using in game class from Dice class:
public Game() {
## filler
dice = new Dice(2, 6);
}
int roll1 = dice.roll();
int roll2 = dice.roll();
int sum = roll1 + roll2;
System.out.println("Come out roll is (" + roll1 + ")(" + roll2 + ") sum is " + sum);
I have to roll two dice for the game. It seems like the two rolls are being added together in the Dice class? I'm not sure.
Your Dice
class already includes the functionality to roll the same dice multiple times and return the sum.
dice = new Dice(2, 6);
Here you define that dice
should consist of two dice rolls.
When you run
int roll1 = dice.roll();
it will generate two random numbers and return the sum of them.
If you want to manually roll the same single dice multiple times you need to change the constructor call:
dice = Dice(1,6)