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 number of one rolls etc. It works for all of my test cases except when the numbers rolled are 3, 4, 5, 5, 6. I can't use anything like a hash table as it has to be in vanilla Java.
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. I am new to Java so I appreciate any help.
CodePudding user response:
You need to check for two cases :
First : check for 0,1,2,3 (first four rolls)
Second : check for 1,2,3,4 (next four rolls)
These cases cover the five rolls.
CodePudding user response:
There are more test cases which will fail. E.g. 1,2,3,5 will return 30 which is wrong. Also 3, 4, 5, 6, x whith x between 3 and 6 will fail.
Why? Because your if else is wrong (but it is close to be correctly).
Here is how it should look like:
if (x[i] != 0) {
count ;
} else {
count = 0; // reset count if no match
}
if (count >= 4) { // always check and not just if there is no match
score = 30;
break;
}
You could simplify:
if (x[i] > 0) {
if ( count == 4)
return 30;
} else {
count = 0;
}
