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) {
//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);
}

View File

@ -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");
}

View File

@ -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));
}