From 808d8d496d324ff42db765746365b1c1743e05bb Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 14 Dec 2024 18:52:15 +0000 Subject: [PATCH] add interface from exercise sheet --- .../java/org/example/DiceInteraction.java | 5 +++ .../app/src/main/java/org/example/Player.java | 14 +++++--- .../java/org/example/SelectionException.java | 7 ++++ .../src/test/java/org/example/PlayerTest.java | 36 +++++++++++++++++-- 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 ueb08/app/src/main/java/org/example/DiceInteraction.java create mode 100644 ueb08/app/src/main/java/org/example/SelectionException.java diff --git a/ueb08/app/src/main/java/org/example/DiceInteraction.java b/ueb08/app/src/main/java/org/example/DiceInteraction.java new file mode 100644 index 00000000..be92f684 --- /dev/null +++ b/ueb08/app/src/main/java/org/example/DiceInteraction.java @@ -0,0 +1,5 @@ +package org.example; + +public interface DiceInteraction { + int changeDiceState(int diceIdx) throws SelectionException; +} diff --git a/ueb08/app/src/main/java/org/example/Player.java b/ueb08/app/src/main/java/org/example/Player.java index 040737f6..8322c602 100644 --- a/ueb08/app/src/main/java/org/example/Player.java +++ b/ueb08/app/src/main/java/org/example/Player.java @@ -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(); + } } diff --git a/ueb08/app/src/main/java/org/example/SelectionException.java b/ueb08/app/src/main/java/org/example/SelectionException.java new file mode 100644 index 00000000..faa0b5ba --- /dev/null +++ b/ueb08/app/src/main/java/org/example/SelectionException.java @@ -0,0 +1,7 @@ +package org.example; + +public class SelectionException extends IllegalArgumentException { + public SelectionException(String message) { + super(message); + } +} diff --git a/ueb08/app/src/test/java/org/example/PlayerTest.java b/ueb08/app/src/test/java/org/example/PlayerTest.java index 53d841c7..fd03277d 100644 --- a/ueb08/app/src/test/java/org/example/PlayerTest.java +++ b/ueb08/app/src/test/java/org/example/PlayerTest.java @@ -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)); + } }