I am getting the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at score.checkWinnings(score.java:29)
at yahtzee.main(yahtzee.java:52)
Line 52 in main is:
points.checkWinnings(Dice, wins);
Line 29 in score is:
if (Dice[y] == 1)
I am trying to get the score of dice rolled but am not sure why lines 29 and 52 are messing it up. I have tried:
for (y = 0; y < Dice[y]; y++);
//this one still gives me an error
for (y = 0; y < 5; y++);
//this one only works when I input that I want to use 5 dice
Would anyone know why what I have/ what i've tried isn't working? Any help would be greatly appreciated!
I have included the relevant sections of my code below:
Main:
import java.util.ArrayList;
public class yahtzee
{
public static void main(String[] args)
{
int play = 1, scorea = 0, sum = 0;
int[] wins = new int[15];
while ((play == 1) && (sum < 15))
{
yahtzeeConfig config= new yahtzeeConfig();
config.start();
int[] Dice = new int[config.hand];
//hand in score
sum = 0;;
int roll = 0;
int x, z;
int rerolla = 0;
dice die = new dice();
System.out.println("\nHere is your roll:\n");
//sets the dice values
ArrayList<Integer> arList= new ArrayList<Integer>(config.hand);
for (x = 0; x < config.hand; x++)
{
die.roll();
Dice[x] = die.get();
arList.add(Dice[x]);
}
//prints out dice values
for (int i =0; i< config.hand; i++)
{
System.out.println("Die " + (i+1) + ": " + Dice[i]);
}
//re-rolls dice
do {
for (int i =0; i< config.hand; i++)
{
play = inputInt("\nWould you like to reroll " + Dice[i] + " ? (1=yes, 2=no)");
if (play == 1)
{
Dice[i]= die.roll();
//roll++;
}
System.out.println("Die " + (i+1) + ": " + Dice[i]);
}
}
//checks score
while ((roll < 2) && (rerolla > 0));
score points = new score();
points.checkWinnings(Dice, wins);
for (z = 0; z < 15; z++) {
sum += wins[z];
}
scorea += points.return_score();
System.out.println("Your total score is: " + scorea);
Score Class:
public class score
{
private int score;
public score()
{
score = 0;
}
public void checkWinnings(int[] Dice, int[] wins)
{
System.out.println("\nLet's Check Score: \n ");
int x = 0, y = 0, winings = 0, winingsa = 0;
int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;
//counts numbers
for (y = 0; y < Dice.length; y++);
//for (y = 0; y < Dice[y]; y++);
//for (y = 0; y < 5; y++);
{
if (Dice[y] == 1)
{
ones++;
}
if (Dice[y] == 2)
{
twos++;
}
if (Dice[y] == 3)
{
threes++;
}
if (Dice[y] == 4)
{
fours++;
}
if (Dice[y] == 5)
{
fives++;
}
if (Dice[y] == 6)
{
sixes++;
}
}
//upper total
System.out.println ("Ones: " + ones);
System.out.println("Twos: " + twos);
System.out.println("Threes: " + threes);
System.out.println("Fours: " + fours);
System.out.println("Fives: " + fives);
System.out.println("Sixes: " + sixes + "\n");
}