I have created a method to calculate whether 5 dice rolls make a small straight in Java. According to my task, a small straight is defined by rolling 4 consecutive numbers in any order.
I am using an array that tracks the frequency of the rolls in my method. arr[0] being the quantity of 1 dice rolled, arr[1] the quantity of 2 dice rolled, etc.
It works for all of my test cases except when the numbers rolled are 3, 4, 5, 5, 6.
The frequency array for the case that will not work is [0, 0, 1, 1, 2, 1].
public int getSmallStraightScore() {
int score = 0;
int count = 0;
int[] x = getRollFrequency();
for (int i = 0; i < x.length; i++) {
if (x[i] != 0) {
count++;
} else if (count == 4) {
score = 30;
break;
}
}
return score;
}
I have tried changing else if (count >= 4) which does not work either.
I can't use anything like a hash table as it has to be in vanilla Java.