Reputation: 35
I am trying to make a program for a dice game where three dice are rolled in the game. Each ‘die’ has numbers from ‘1’ to ‘6’ on their sides. If the numbers are all 6, the user gets 500 points. If two numbers match, but not three, the user gets 50 points. If the user has two 6s (but not three 6s), then he/she gets 100 points. If no numbers match, then the user loses one point.
So far I have this:
import java.util.Random;
public class diceGame {
public static void main (String args[])
{
Random randomGenerator = new Random ();
int a;//first face of die
a = randomGenerator.nextInt((5)+1);
System.out.println(a);
int b;//second face of die
b = randomGenerator.nextInt((5)+1);
Random randomGenerator2 = new Random ();
System.out.println(b);
int c;//third face of die
c = randomGenerator.nextInt((5)+1);
Random randomGenerator3 = new Random ();
System.out.println(c);
...
}
...
}
But as soon as I get into the nested if statements I get stuck.
Upvotes: 2
Views: 2295
Reputation: 414
Without completely solving your problem, here's one possible design that encapsulates your roll logic.
import java.util.Random;
public class DiceGame
{
private class DiceRoll {
private static Random rng = new Random();
private int a, b, c; // rolls
public DiceRoll() {
this.a = rng.nextInt(6);
this.b = rng.nextInt(6);
this.c = rng.nextInt(6);
}
public int getScore() {
if (a == 6 && b == 6 && c == 6)
return 500;
...
// other cases
}
public String toString() {
return String.format("Rolled a %d, %d, and %d", a, b, c);
}
}
public static void main (String args[])
{
DiceRoll d;
while (true) {
d = new DiceRoll();
d.getScore(); // gives you how much to change the player score
}
// finish client
}
}
Upvotes: 1
Reputation: 1365
if (a == 6 && b == 6 && c == 6){
//Add to score
}
else if ((a == b == 6 && b != c) || (b == c == 6 && c !=a)){
//Add to score
}
else if ((a == b && b != c) || (b == c && c !=a)){
//Add to score
}
//...
Upvotes: 1