This commit is contained in:
Daniel Langbein 2024-12-15 18:37:34 +00:00
parent c1c87f07d8
commit 8c6b57bb76
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 23 additions and 19 deletions

View File

@ -16,6 +16,7 @@ public class Game {
} }
private static void loop(Player... players) { private static void loop(Player... players) {
//noinspection InfiniteLoopStatement
while (true) { while (true) {
for (Player player : players) { for (Player player : players) {
System.out.println("\n" + System.out.println("\n" +
@ -28,23 +29,23 @@ public class Game {
private static void interactWith(Player player) { private static void interactWith(Player player) {
System.out.println(player); System.out.println(player);
if(player.misthrow()){ if (player.misthrow()) {
player.finishRound(); player.finishRound();
return; return;
} }
while (true) { while (true) {
try { try {
System.out.println("" + System.out.println("""
"[r] roll the dices\n" + [r] roll the dices
"[i] mark dice i\n" + [i] mark dice i
"[f] finish round"); [f] finish round""");
String line = scanner.nextLine(); String line = scanner.nextLine();
switch (line) { switch (line) {
case "r": case "r":
player.rollTheDices(); player.rollTheDice();
System.out.println(player); System.out.println(player);
if(player.misthrow()){ if (player.misthrow()) {
player.finishRound(); player.finishRound();
return; return;
} }
@ -61,7 +62,7 @@ public class Game {
} }
} catch (NumberFormatException | SelectionException | IllegalStateException e) { } catch (NumberFormatException | SelectionException | IllegalStateException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} catch (NoSuchElementException e){ } catch (NoSuchElementException e) {
// stdin closed (e.g. by pressing STRG + D) // stdin closed (e.g. by pressing STRG + D)
System.exit(0); System.exit(0);
} }

View File

@ -9,7 +9,7 @@ public class Player implements DiceInteraction{
private final String name; private final String name;
private int totalScore = 0; private int totalScore = 0;
private int roundScore = 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(),
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); .noneMatch(d -> d.getDiceEyes().points() > 0);
} }
public void rollTheDices() throws IllegalStateException{ public void rollTheDice() throws IllegalStateException{
if (misthrow()){ if (misthrow()){
throw new IllegalStateException("It is not allowed to roll the dices again after a misthrow"); throw new IllegalStateException("It is not allowed to roll the dices again after a misthrow");
} }

View File

@ -32,16 +32,18 @@ class PlayerTest {
void testToggleAlreadyFixed() { void testToggleAlreadyFixed() {
// Reproducible random values o.o // Reproducible random values o.o
DiceEyes.random = new Random(1337); DiceEyes.random = new Random(1337);
Player player = new Player("Player1"); int diceIdx = 1;
String expected; 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); player.toggleMarked(diceIdx);
expected = "0 + 50: 4 (5) 6 6 2 6 "; expected = "0 + 50: 4 (5) 6 6 2 6 ";
assertEquals(expected, player.toStringHelper()); assertEquals(expected, player.toStringHelper());
player.rollTheDices(); player.rollTheDice();
expected = "0 + 50: 1 [5] 2 5 4 2 "; expected = "0 + 50: 1 [5] 2 5 4 2 ";
assertEquals(expected, player.toStringHelper()); assertEquals(expected, player.toStringHelper());
@ -52,24 +54,25 @@ class PlayerTest {
void testForUeb08() { void testForUeb08() {
// Reproducible random values o.o // Reproducible random values o.o
DiceEyes.random = new Random(1337); DiceEyes.random = new Random(1337);
int diceIdx = 1;
String expected;
// A new player initially rolls the dices once. // A new player initially rolls the dices once.
Player player = new Player("Player1"); Player player = new Player("Player1");
String expected; expected = "0 + 0: 4 5 6 6 2 6 ";
assertEquals(expected, player.toStringHelper());
int diceIdx = 1;
// Player marks dice 1 as fixed // Player marks dice 1 as fixed
player.changeDiceState(diceIdx); player.changeDiceState(diceIdx);
expected = "0 + 50: 4 (5) 6 6 2 6 "; expected = "0 + 50: 4 (5) 6 6 2 6 ";
assertEquals(expected, player.toStringHelper()); assertEquals(expected, player.toStringHelper());
// Then rolls all other dices again. // Player then rolls all other dices again.
player.rollTheDices(); player.rollTheDice();
expected = "0 + 50: 1 [5] 2 5 4 2 "; expected = "0 + 50: 1 [5] 2 5 4 2 ";
assertEquals(expected, player.toStringHelper()); 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. // an exception will be thrown.
assertThrows(IllegalArgumentException.class, () -> player.changeDiceState(diceIdx)); assertThrows(IllegalArgumentException.class, () -> player.changeDiceState(diceIdx));
} }