Reputation: 23
So I am building the petals on a rose game and the dice defined as dice1, dice 2 etc. i then run a loop to decide for each dice what its value is and add that to the game value. What i want is for every sequence of the loop for it to switch which variable it is looking at. So first run through look at dice1. and then second look at dice2.
public class Rosegame
{
private int dice1;
private int dice2;
private int dice3;
private int dice4;
private int dice5;
private int gameValue;
public void rollDice()
{
dice1 = (int) ((6-1+1) * Math.random()) + 1;
dice2 = (int) ((6-1+1) * Math.random()) + 1;
dice3 = (int) ((6-1+1) * Math.random()) + 1;
dice4 = (int) ((6-1+1) * Math.random()) + 1;
dice5 = (int) ((6-1+1) * Math.random()) + 1;
}
public void printValues()
{
System.out.println("Dice 1 is:" + dice1);
System.out.println("Dice 2 is:" + dice2);
System.out.println("Dice 3 is:" + dice3);
System.out.println("Dice 4 is:" + dice4);
System.out.println("Dice 5 is:" + dice5);
}
public int calculatePetalsOnRose()
{
gameValue = 0;
for(int i = 1; i <=5; i++)
{
if (dice1 == 5)
{
gameValue = gameValue + 4;
}
else if (dice1 == 3)
{
gameValue = gameValue + 2;
}
else
{
gameValue = gameValue;
}
}
return gameValue;
}
}
this is my current code and what I need is inside the if statement where it has dice1, i want that to be able to change everytime it loops. Their is also a driver that runs the methods and allows input but so far that is working. Thank you so much in advance
Edit:
switching the variables to an array i now have which gives me an error saying java.lang.NullPointerException. It happens when the rollDice function is being called.
public class PetalsGame
{
private int[] anArrayDice;
private int gameValue;
public void rollDice()
{
anArrayDice[0] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[1] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[2] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[3] = (int) ((6-1+1) * Math.random()) + 1;
anArrayDice[4] = (int) ((6-1+1) * Math.random()) + 1;
}
public void printDice()
{
System.out.println("Dice 1 is:" + anArrayDice[0]);
System.out.println("Dice 2 is:" + anArrayDice[1]);
System.out.println("Dice 3 is:" + anArrayDice[2]);
System.out.println("Dice 4 is:" + anArrayDice[3]);
System.out.println("Dice 5 is:" + anArrayDice[4]);
}
public int calculateAllPetals()
{
gameValue = 0;
for(int i = 0; i <=4; i++)
{
if (anArrayDice[i] == 5)
{
gameValue = gameValue + 4;
}
else if (anArrayDice[i] == 3)
{
gameValue = gameValue + 2;
}
else
{
gameValue = gameValue;
}
}
return gameValue;
}
}
I was able to fix it by adding anArrayDice = new int[5]; inside of the rollDice method. Thank you for the help.
Upvotes: 2
Views: 75
Reputation: 104
I think it would be alot easier to do this if you use an array to store your dice. Also it saves you from having to repeat the same lines of code over and over again.
public int [] diceArray;
private int gameValue;
public void rollDice()
{
for(int i =0; i < diceArray.length; i++)
{
diceArray[i] = 6 *((int)Math.random()+ 1);
}
}
public void printValues()
{
for(int i=0; i < diceArray.length; i++)
{
System.out.println("Dice" + (i+1) +"is:" + diceArray[i]);
}
}
public int calculatePetalsOnRose()
{
gameValue = 0;
for(int i = 0; i < diceArray.length; i++)
{
if(diceArray[i] == 5)
{
gameValue += 4;
}
else if (diceArray[i] == 3)
{
gameValue +=2;
}
}
return gameValue;
}
Upvotes: 0
Reputation: 201467
You only seem to roll one die (at least that is the only value you seem to be interested in), so I would start by extracting a method to roll one die. I would also prefer, for readability, Random.nextInt(int)
. Something like,
private static final Random rand = new Random();
private static int rollDie(int sides) {
return rand.nextInt(sides) + 1;
}
Then you can roll and calculate your rose with that method as needed. Also, you can use x += y;
instead of x = x + y;
. Something like,
public int calculatePetalsOnRose() {
int gameValue = 0;
for (int i = 1; i <= 5; i++) {
int dice = rollDie(6);
System.out.printf("Dice %d is: %d%n", i, dice);
if (dice == 5) {
gameValue += 4;
} else if (dice == 3) {
gameValue += 2;
}
}
return gameValue;
}
Upvotes: 1