Skip to content
Snippets Groups Projects
Commit eb31a43d authored by Thomas FABRE's avatar Thomas FABRE
Browse files

Refactor fours with Roll and generify summing method

parent 39a2e7fe
Branches
No related tags found
No related merge requests found
......@@ -19,16 +19,23 @@ public final class Yatzy {
return 0;
}
private static int sumDiceThatMatchWithValue(Roll roll, int value) {
return roll.toStream().filter(dice -> dice == value).sum();
}
public static int ones(Roll roll) {
return roll.toStream().filter(dice -> dice == 1).sum();
return sumDiceThatMatchWithValue(roll, 1);
}
public static int twos(Roll roll) {
return roll.toStream().filter(dice -> dice == 2).sum();
return sumDiceThatMatchWithValue(roll, 2);
}
public static int threes(Roll roll) {
return roll.toStream().filter(dice -> dice == 3).sum();
return sumDiceThatMatchWithValue(roll, 3);
}
public static int fours(Roll roll) {
return sumDiceThatMatchWithValue(roll, 4);
}
public static int pair(int d1, int d2, int d3, int d4, int d5) {
......@@ -168,16 +175,6 @@ public final class Yatzy {
return 0;
}
public int fours() {
int sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
public int fives() {
int s = 0;
for (int i = 0; i < dice.length; i++) {
......
......@@ -57,9 +57,9 @@ class YatzyTest {
@Test
void foursShouldSumAllFourDices() {
assertAll(
() -> assertEquals(12, new Yatzy(4, 4, 4, 5, 5).fours()),
() -> assertEquals(8, new Yatzy(4, 4, 5, 5, 5).fours()),
() -> assertEquals(4, new Yatzy(4, 5, 5, 5, 5).fours())
() -> assertEquals(12, Yatzy.fours(roll(4, 4, 4, 5, 5))),
() -> assertEquals(8, Yatzy.fours(roll(4, 4, 5, 5, 5))),
() -> assertEquals(4, Yatzy.fours(roll(4, 5, 5, 5, 5)))
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment