Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: CI

on:
push:
pull_request:

jobs:
test:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.vbetsch.yams;

public class InvalidRollSizeError extends IllegalArgumentException {
public InvalidRollSizeError(int actualSize) {
super("Roll must contain exactly " + Yams.DICES_AMOUNT + " dices but got " + actualSize);
}
}
9 changes: 6 additions & 3 deletions src/main/java/io/github/vbetsch/yams/Yams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.stream.IntStream;

public class Yams {
private final int DICES_AMOUNT = 5;
static final int DICES_AMOUNT = 5;

private int calculateSumOfDices(List<Integer> roll) {
return roll
Expand Down Expand Up @@ -43,7 +43,7 @@ private boolean containsDuplicatesNth(List<Integer> roll, int duplicatesNumber)
.stream()
.mapToInt(Integer::intValue)
.distinct();
return reducedRoll.count() == (this.DICES_AMOUNT - duplicatesNumber + 1);
return reducedRoll.count() == (Yams.DICES_AMOUNT - duplicatesNumber + 1);
}

private int handleThreeOfAKindScore(List<Integer> roll) throws IllegalArgumentException {
Expand All @@ -60,7 +60,10 @@ private int handleFourOfAKindScore(List<Integer> roll) throws IllegalArgumentExc
return this.calculateSumOfDices(roll);
}

public int computeScore(List<Integer> roll, CategoryEnum category) {
public int computeScore(List<Integer> roll, CategoryEnum category) throws InvalidRollSizeError {
if(roll.size() != Yams.DICES_AMOUNT) {
throw new InvalidRollSizeError(roll.size());
}
return switch (category) {
case CategoryEnum.CHANCE -> this.handleChanceScore(roll);
case CategoryEnum.YAMS -> this.handleYamsScore(roll);
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/io/github/vbetsch/yams/YamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,46 @@ void fourOfAKind_returnIllegalArgumentException_whenGivenLargeStraight() {
"We cannot compute score with category FourOfAKind for this roll"
);
}

@Test
void shouldThrowInvalidRollSizeErrorWhenGivenLessThanFiveDices() {
// Arrange
Yams yams = new Yams();
List<Integer> roll = List.of(1, 2, 3, 4);

// Act & Assert
assertThrows(
InvalidRollSizeError.class,
() -> yams.computeScore(roll, CategoryEnum.CHANCE),
"Roll must contain exactly 5 dices"
);
}

@Test
void shouldThrowInvalidRollSizeErrorWhenGivenMoreThanFiveDices() {
// Arrange
Yams yams = new Yams();
List<Integer> roll = List.of(1, 2, 3, 4, 5, 6);

// Act & Assert
assertThrows(
InvalidRollSizeError.class,
() -> yams.computeScore(roll, CategoryEnum.CHANCE),
"Roll must contain exactly 5 dices"
);
}

@Test
void shouldThrowInvalidRollSizeErrorWhenGivenNoDice() {
// Arrange
Yams yams = new Yams();
List<Integer> roll = List.of();

// Act & Assert
assertThrows(
InvalidRollSizeError.class,
() -> yams.computeScore(roll, CategoryEnum.CHANCE),
"Roll must contain exactly 5 dices"
);
}
}
Loading