add interface from exercise sheet
This commit is contained in:
parent
a8471c9b98
commit
808d8d496d
5
ueb08/app/src/main/java/org/example/DiceInteraction.java
Normal file
5
ueb08/app/src/main/java/org/example/DiceInteraction.java
Normal file
@ -0,0 +1,5 @@
|
||||
package org.example;
|
||||
|
||||
public interface DiceInteraction {
|
||||
int changeDiceState(int diceIdx) throws SelectionException;
|
||||
}
|
@ -6,11 +6,11 @@ import java.util.stream.Collectors;
|
||||
import static org.example.DiceEyes.FIVE;
|
||||
import static org.example.DiceEyes.ONE;
|
||||
|
||||
public class Player {
|
||||
public class Player implements DiceInteraction{
|
||||
private final String name;
|
||||
private int totalScore = 0;
|
||||
private int roundScore = 0;
|
||||
Dice[] dices = new Dice[]{
|
||||
private Dice[] dices = new Dice[]{
|
||||
new Dice(), new Dice(), new Dice(),
|
||||
new Dice(), new Dice(), new Dice()
|
||||
};
|
||||
@ -50,10 +50,10 @@ public class Player {
|
||||
|
||||
public void toggleMark(int diceIdx) {
|
||||
if (diceIdx < 0 || diceIdx >= dices.length) {
|
||||
throw new IllegalArgumentException("Invalid dice index: " + diceIdx);
|
||||
throw new SelectionException("Invalid dice index: " + diceIdx);
|
||||
}
|
||||
if (dices[diceIdx].isFixed()) {
|
||||
throw new IllegalArgumentException("Dice " + diceIdx + " is already fixed");
|
||||
throw new SelectionException("Dice " + diceIdx + " is already fixed");
|
||||
}
|
||||
dices[diceIdx].toggleMarked();
|
||||
}
|
||||
@ -116,4 +116,10 @@ public class Player {
|
||||
.map(Dice::toString)
|
||||
.collect(Collectors.joining(" "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int changeDiceState(int diceIdx) throws SelectionException {
|
||||
toggleMark(diceIdx);
|
||||
return roundScore + selectionScore();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.example;
|
||||
|
||||
public class SelectionException extends IllegalArgumentException {
|
||||
public SelectionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -33,14 +33,44 @@ class PlayerTest {
|
||||
// Reproducible random values o.o
|
||||
DiceEyes.random = new Random(1337);
|
||||
Player player = new Player("Player1");
|
||||
String expected;
|
||||
|
||||
int diceIdx = 1;
|
||||
|
||||
// Manually fix one dice
|
||||
player.dices[diceIdx].fix();
|
||||
String expected = "0 + 50: 4 [5] 6 6 2 6 ";
|
||||
player.toggleMark(diceIdx);
|
||||
expected = "0 + 50: 4 (5) 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
player.rollTheDices();
|
||||
expected = "0 + 50: 1 [5] 2 5 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> player.toggleMark(diceIdx));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForUeb08() {
|
||||
// Reproducible random values o.o
|
||||
DiceEyes.random = new Random(1337);
|
||||
|
||||
// A new player initially rolls the dices once.
|
||||
Player player = new Player("Player1");
|
||||
String expected;
|
||||
|
||||
int diceIdx = 1;
|
||||
|
||||
// Player marks dice 1 as fixed
|
||||
player.changeDiceState(diceIdx);
|
||||
expected = "0 + 50: 4 (5) 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
// Then rolls all other dices again.
|
||||
player.rollTheDices();
|
||||
expected = "0 + 50: 1 [5] 2 5 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
// When they now try to unmark dice 1,
|
||||
// an exception will be thrown.
|
||||
assertThrows(IllegalArgumentException.class, () -> player.changeDiceState(diceIdx));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user