From d4d7d8ceb6e5dca058b371d6eb8fd3a7dea0a354 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 14 Dec 2024 18:05:35 +0000 Subject: [PATCH] dice game --- ueb08/.gitignore | 1 + ueb08/app/src/main/java/org/example/App.java | 14 --- ueb08/app/src/main/java/org/example/Dice.java | 67 ++++++++++ .../src/main/java/org/example/DiceEyes.java | 47 +++++++ ueb08/app/src/main/java/org/example/Game.java | 53 ++++++++ .../app/src/main/java/org/example/Player.java | 117 ++++++++++++++++++ .../src/test/java/org/example/AppTest.java | 14 --- .../src/test/java/org/example/GameTest.java | 4 + 8 files changed, 289 insertions(+), 28 deletions(-) delete mode 100644 ueb08/app/src/main/java/org/example/App.java create mode 100644 ueb08/app/src/main/java/org/example/Dice.java create mode 100644 ueb08/app/src/main/java/org/example/DiceEyes.java create mode 100644 ueb08/app/src/main/java/org/example/Game.java create mode 100644 ueb08/app/src/main/java/org/example/Player.java delete mode 100644 ueb08/app/src/test/java/org/example/AppTest.java create mode 100644 ueb08/app/src/test/java/org/example/GameTest.java diff --git a/ueb08/.gitignore b/ueb08/.gitignore index 939e2dd7..275c8c2a 100644 --- a/ueb08/.gitignore +++ b/ueb08/.gitignore @@ -1,4 +1,5 @@ /.idea/ +/app/build/ # Ignore Gradle project-specific cache directory /.gradle/ diff --git a/ueb08/app/src/main/java/org/example/App.java b/ueb08/app/src/main/java/org/example/App.java deleted file mode 100644 index e7e1af93..00000000 --- a/ueb08/app/src/main/java/org/example/App.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This source file was generated by the Gradle 'init' task - */ -package org.example; - -public class App { - public String getGreeting() { - return "Hello World!"; - } - - public static void main(String[] args) { - System.out.println(new App().getGreeting()); - } -} diff --git a/ueb08/app/src/main/java/org/example/Dice.java b/ueb08/app/src/main/java/org/example/Dice.java new file mode 100644 index 00000000..046e27d9 --- /dev/null +++ b/ueb08/app/src/main/java/org/example/Dice.java @@ -0,0 +1,67 @@ +package org.example; + +public class Dice { + private DiceEyes diceEyes; + private boolean marked = false; + private boolean fixed = false; + + public Dice() { + roll(); + } + + /** + * Rolls this dice. It will have a new random `DiceEyes` afterward. + */ + public void roll() { + diceEyes = DiceEyes.random(); + } + + public DiceEyes getDiceEyes() { + return diceEyes; + } + + public boolean isFixed() { + return fixed; + } + + public void fix() { + fixed = true; + marked = false; + } + + public void unFix() { + fixed = false; + } + + public boolean isMarked() { + return marked; + } + + public void toggleMarked(){ + if(isMarked()){ + unMark(); + }else{ + mark(); + } + } + + private void mark() { + marked = true; + fixed = false; + } + + public void unMark() { + marked = false; + } + + @Override + public String toString() { + if (isFixed()) { + return "[" + diceEyes + "]"; + } else if (isMarked()) { + return "(" + diceEyes + ")"; + } else { + return " " + diceEyes + " "; + } + } +} diff --git a/ueb08/app/src/main/java/org/example/DiceEyes.java b/ueb08/app/src/main/java/org/example/DiceEyes.java new file mode 100644 index 00000000..eafa7691 --- /dev/null +++ b/ueb08/app/src/main/java/org/example/DiceEyes.java @@ -0,0 +1,47 @@ +package org.example; + +public enum DiceEyes { + ONE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6); + + private final int i; + + DiceEyes(int i) { + this.i = i; + } + + public static DiceEyes random(){ + // In range [0,1) + double randDouble = Math.random(); + // In range [0,6) + randDouble *= 6d; + // Round floor -> One of the integers {0, 1, 2, 3, 4, 5} + int randInt = (int) randDouble; + + return DiceEyes.values()[randInt]; + } + + public int points(){ + return switch (this) { + case ONE -> 100; + case FIVE -> 50; + default -> 0; + }; + } + public int triplePoints(){ + return switch (this) { + case ONE -> 1000; + case FIVE -> 500; + default -> 0; + }; + } + + @Override + public String toString() { + return String.valueOf(i); + } +} diff --git a/ueb08/app/src/main/java/org/example/Game.java b/ueb08/app/src/main/java/org/example/Game.java new file mode 100644 index 00000000..d80156ec --- /dev/null +++ b/ueb08/app/src/main/java/org/example/Game.java @@ -0,0 +1,53 @@ +package org.example; + +import java.util.Scanner; + +public class Game { + private static final Scanner scanner = new Scanner(System.in); + + public static void main(String... args) { + loop( + new Player("Sam"), + new Player("Daniel") + ); + } + + private static void loop(Player... players) { + while (true) { + for (Player player : players) { + System.out.println("\n" + + "===================================\n" + + "Next player: " + player.getName()); + player.rollTheDices(); + System.out.println(player); + + interactWith(player); + } + } + } + + private static void interactWith(Player player) { + while (true) { + System.out.println("" + + "[r] roll the dices\n" + + "[i] mark dice i\n" + + "[f] finish round"); + String line = scanner.nextLine(); + switch (line) { + case "r": + player.rollTheDices(); + System.out.println(player); + break; + case "f": + player.finishRound(); + System.out.println(player); + return; + default: + int i = Integer.parseInt(line); + player.toggleMark(i); + System.out.println(player); + break; + } + } + } +} diff --git a/ueb08/app/src/main/java/org/example/Player.java b/ueb08/app/src/main/java/org/example/Player.java new file mode 100644 index 00000000..7d6372bc --- /dev/null +++ b/ueb08/app/src/main/java/org/example/Player.java @@ -0,0 +1,117 @@ +package org.example; + +import java.util.Arrays; +import java.util.stream.Collectors; + +import static org.example.DiceEyes.FIVE; +import static org.example.DiceEyes.ONE; + +public class Player { + private final String name; + private int totalScore = 0; + private int roundScore = 0; + private Dice[] dices = new Dice[]{ + new Dice(), new Dice(), new Dice(), + new Dice(), new Dice(), new Dice() + }; + + public Player(String name){ + this.name = name; + } + + public void rollTheDices() { + // If all dices are fixed and score points, + // (1) add selectionScore to roundScore, + // (2) unFix all dices + // (3) and roll the dices again + boolean cond = Arrays.stream(dices) + .filter(Dice::isFixed) + .filter(d -> d.getDiceEyes() == ONE || d.getDiceEyes() == FIVE) + .count() == 6; + if (cond) { + roundScore += selectionScore(); + Arrays.stream(dices).forEach(Dice::unFix); + rollUnfixedDices(); + return; + } + + // Fix all currently marked dices. + Arrays.stream(dices) + .filter(Dice::isMarked) + .forEach(Dice::fix); + rollUnfixedDices(); + } + + private void rollUnfixedDices() { + Arrays.stream(dices) + .filter(d -> !d.isFixed()) + .forEach(Dice::roll); + } + + public void toggleMark(int diceIdx) { + if (diceIdx < 0 || diceIdx >= dices.length) { + throw new IllegalArgumentException("Invalid dice index: " + diceIdx); + } + if (dices[diceIdx].isFixed()) { + throw new IllegalArgumentException("Dice " + diceIdx + " is already fixed"); + } + dices[diceIdx].toggleMarked(); + } + + public void finishRound() { + totalScore += roundScore + selectionScore(); + roundScore = 0; + + // Unfix and unmark all dices + for (Dice dice : dices) { + dice.unMark(); + dice.unFix(); + } + } + + private int selectionScore() { + int ones = 0; + int fives = 0; + for (Dice dice : dices) { + if (dice.isFixed() || dice.isMarked()) { + switch (dice.getDiceEyes()) { + case ONE: + ones++; + break; + case FIVE: + fives++; + break; + } + } + } + + int toggledScore = 0; + + while (ones >= 3) { + toggledScore += ONE.triplePoints(); + ones -= 3; + } + while (fives >= 3) { + toggledScore += FIVE.triplePoints(); + fives -= 3; + } + + toggledScore += ONE.points() * ones; + toggledScore += FIVE.points() * fives; + + return toggledScore; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return totalScore + " + " + (roundScore + selectionScore()) + ": " + dicesToString(); + } + + private String dicesToString() { + return Arrays.stream(dices).map(Dice::toString).collect(Collectors.joining(" ")); + } +} diff --git a/ueb08/app/src/test/java/org/example/AppTest.java b/ueb08/app/src/test/java/org/example/AppTest.java deleted file mode 100644 index f5ce33d3..00000000 --- a/ueb08/app/src/test/java/org/example/AppTest.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This source file was generated by the Gradle 'init' task - */ -package org.example; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -class AppTest { - @Test void appHasAGreeting() { - App classUnderTest = new App(); - assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); - } -} diff --git a/ueb08/app/src/test/java/org/example/GameTest.java b/ueb08/app/src/test/java/org/example/GameTest.java new file mode 100644 index 00000000..1d950171 --- /dev/null +++ b/ueb08/app/src/test/java/org/example/GameTest.java @@ -0,0 +1,4 @@ +package org.example; + +class GameTest { +}