diff --git a/KniffelSpiel/src/domain/Würfel.java b/KniffelSpiel/src/domain/Würfel.java new file mode 100644 index 0000000..bd29d1e --- /dev/null +++ b/KniffelSpiel/src/domain/Würfel.java @@ -0,0 +1,114 @@ +package domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Würfel { + + private static final int DEFAULT_NUM_WÜRFEL = 5; + private int[] values; + private boolean isSixSided = true; + public int numSides = (isSixSided)? 6 : 8; + private List gehalteneWürfelIndizes; + private Scanner scanner; + + + public Würfel(int numSides) { + this.numSides = numSides; + this.values = new int[DEFAULT_NUM_WÜRFEL]; + this.gehalteneWürfelIndizes = new ArrayList<>(); + this.scanner = new Scanner(System.in); + } + + + public void setNumSides(int numSides) { + this.numSides = numSides; + } + + + public int getNumSides() { + return numSides; + } + + + public void playRound() { + rollWürfel(); + //anwendengehalteneWürfel(); + this.scanner = new Scanner(System.in); + updateValues(); + } + + + public void rollWürfel() { + for (int i = 0; i < DEFAULT_NUM_WÜRFEL; i++) { + values[i] = (int) (Math.random() * numSides) + 1; // 1-8 eyes + } + } + + /* private void anwendengehalteneWürfel() { + if (!gehalteneWürfelIndizes.isEmpty()) { + List keptDice = new ArrayList<>(); + for (int index : gehalteneWürfelIndizes) { + keptDice.add(values[index]); + } + int additionalRolls = DEFAULT_NUM_WÜRFEL - keptDice.size(); + for (int j = 0; j < additionalRolls; j++) { + keptDice.add((int) (Math.random() * numSides) + 1); + } + + // Konvertiere List zurück zu int[] + values = new int[keptDice.size()]; + for (int i = 0; i < keptDice.size(); i++) { + values[i] = keptDice.get(i); + } + } + }*/ + + + + public void rerollDice() { + List keptDice = new ArrayList<>(); + for (int i = 0; i < DEFAULT_NUM_WÜRFEL; i++) { + System.out.println("Keep die number " + (i+1) + " for next roll? (y/n): "); + String answer = scanner.nextLine().toLowerCase(); + if (answer.equals("y")) { + keptDice.add(values[i]); + } + } + + int additionalRolls = DEFAULT_NUM_WÜRFEL - keptDice.size(); + for (int j = 0; j < additionalRolls; j++) { + keptDice.add((int) (Math.random() * numSides) + 1); + } + + // Конвертируем List обратно в int[] + for (int i = 0; i < keptDice.size(); i++) { + values[i] = keptDice.get(i); + } + } + + + + + + + + + + private void updateValues() { + for (int i = 0; i < DEFAULT_NUM_WÜRFEL; i++) { + System.out.println("Wurf " + (i + 1) + ": " + values[i]); + } + } + + + public int[] getValues() { + return values; + } + + public void releaseDice() { + gehalteneWürfelIndizes.clear(); + } + +}