Würfel class comit

main
danai 2024-05-06 23:27:58 +02:00
parent fa4f323932
commit a8e4e484ad
1 changed files with 114 additions and 0 deletions

View File

@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> обратно в 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();
}
}