diff --git a/ueb08/app/src/main/java/org/example/Game.java b/ueb08/app/src/main/java/org/example/Game.java index 13b93906..26c0416b 100644 --- a/ueb08/app/src/main/java/org/example/Game.java +++ b/ueb08/app/src/main/java/org/example/Game.java @@ -16,6 +16,7 @@ public class Game { } private static void loop(Player... players) { + //noinspection InfiniteLoopStatement while (true) { for (Player player : players) { System.out.println("\n" + @@ -28,23 +29,23 @@ public class Game { private static void interactWith(Player player) { System.out.println(player); - if(player.misthrow()){ + if (player.misthrow()) { player.finishRound(); return; } while (true) { try { - System.out.println("" + - "[r] roll the dices\n" + - "[i] mark dice i\n" + - "[f] finish round"); + System.out.println(""" + [r] roll the dices + [i] mark dice i + [f] finish round"""); String line = scanner.nextLine(); switch (line) { case "r": - player.rollTheDices(); + player.rollTheDice(); System.out.println(player); - if(player.misthrow()){ + if (player.misthrow()) { player.finishRound(); return; } @@ -61,7 +62,7 @@ public class Game { } } catch (NumberFormatException | SelectionException | IllegalStateException e) { System.err.println(e.getMessage()); - } catch (NoSuchElementException e){ + } catch (NoSuchElementException e) { // stdin closed (e.g. by pressing STRG + D) System.exit(0); } diff --git a/ueb08/app/src/main/java/org/example/Player.java b/ueb08/app/src/main/java/org/example/Player.java index 6654526b..39c09a20 100644 --- a/ueb08/app/src/main/java/org/example/Player.java +++ b/ueb08/app/src/main/java/org/example/Player.java @@ -9,7 +9,7 @@ public class Player implements DiceInteraction{ private final String name; private int totalScore = 0; private int roundScore = 0; - private Dice[] dices = new Dice[]{ + private final Dice[] dices = new Dice[]{ new Dice(), new Dice(), new Dice(), new Dice(), new Dice(), new Dice() }; @@ -26,7 +26,7 @@ public class Player implements DiceInteraction{ .noneMatch(d -> d.getDiceEyes().points() > 0); } - public void rollTheDices() throws IllegalStateException{ + public void rollTheDice() throws IllegalStateException{ if (misthrow()){ throw new IllegalStateException("It is not allowed to roll the dices again after a misthrow"); } diff --git a/ueb08/app/src/test/java/org/example/PlayerTest.java b/ueb08/app/src/test/java/org/example/PlayerTest.java index 9b26aea3..a9339e07 100644 --- a/ueb08/app/src/test/java/org/example/PlayerTest.java +++ b/ueb08/app/src/test/java/org/example/PlayerTest.java @@ -32,16 +32,18 @@ class PlayerTest { void testToggleAlreadyFixed() { // Reproducible random values o.o DiceEyes.random = new Random(1337); - Player player = new Player("Player1"); + int diceIdx = 1; String expected; - int diceIdx = 1; + Player player = new Player("Player1"); + expected = "0 + 0: 4 5 6 6 2 6 "; + assertEquals(expected, player.toStringHelper()); player.toggleMarked(diceIdx); expected = "0 + 50: 4 (5) 6 6 2 6 "; assertEquals(expected, player.toStringHelper()); - player.rollTheDices(); + player.rollTheDice(); expected = "0 + 50: 1 [5] 2 5 4 2 "; assertEquals(expected, player.toStringHelper()); @@ -52,24 +54,25 @@ class PlayerTest { void testForUeb08() { // Reproducible random values o.o DiceEyes.random = new Random(1337); + int diceIdx = 1; + String expected; // A new player initially rolls the dices once. Player player = new Player("Player1"); - String expected; - - int diceIdx = 1; + expected = "0 + 0: 4 5 6 6 2 6 "; + assertEquals(expected, player.toStringHelper()); // Player marks dice 1 as fixed player.changeDiceState(diceIdx); expected = "0 + 50: 4 (5) 6 6 2 6 "; assertEquals(expected, player.toStringHelper()); - // Then rolls all other dices again. - player.rollTheDices(); + // Player then rolls all other dices again. + player.rollTheDice(); expected = "0 + 50: 1 [5] 2 5 4 2 "; assertEquals(expected, player.toStringHelper()); - // When they now try to unmark dice 1, + // When player now tries to unmark dice 1, // an exception will be thrown. assertThrows(IllegalArgumentException.class, () -> player.changeDiceState(diceIdx)); }