Fassade package commit, KniffelSpiel Klasse

main
danai 2024-05-06 23:30:39 +02:00
parent e39e4af44d
commit ad88776a5a
1 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,189 @@
package fassade;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import domain.Spiel;
import domain.Spieler;
import domain.Würfel;
public class KniffelSpiel {
private Spiel spiel;
private Scanner scanner;
private Würfel würfel;
public KniffelSpiel() {
System.out.println("Willkommen zum Star Wars Day Kniffel!");
this.scanner = new Scanner(System.in);
int numPlayers;
int numSides;
do {
System.out.print("Enter number of players (1-6): ");
numPlayers = scanner.nextInt();
} while (numPlayers < 1 || numPlayers > 6);
do {
System.out.print("Enter number of dice sides (6 or 8): ");
numSides = scanner.nextInt();
} while (numSides != 6 && numSides != 8);
scanner.nextLine();
this.spiel = new Spiel(numPlayers, numSides);
}
public void startGame() {
for (Spieler player : spiel.getSpieler()) {
System.out.print("Enter player " + player.getNumber() + " name: ");
player.setName(scanner.nextLine());
}
playGame();
}
private void playGame() {
int numPlayers = spiel.getSpieler().size();
while (true) {
spiel.nextPlayer();
int currentPlayerIndex = spiel.getCurrentPlayerIndex();
Spieler currentPlayer = spiel.getSpieler().get(currentPlayerIndex);
System.out.println("Player " + currentPlayer.getNumber() + " of " + numPlayers + "'s turn:");
rollAndRerollDice();
displayCategories();
askUserForCategory();
displayScore();
if (isGameFinished()) {
break;
}
}
displayFinalScore();
}
private void displayDiceValues() {
int[] diceValues = spiel.getWürfelValues();
System.out.println("Dice values: " + Arrays.toString(diceValues));
}
public void rollAndRerollDice() {
// Бросаем все кубики в первый раз
spiel.rollWürfel();
displayDiceValues();
int currentRoll = 1;
int maxRolls = 3;
while (currentRoll < maxRolls) {
System.out.println("Do you want to keep some dice for the next roll? (y/n): ");
String answer = scanner.nextLine().toLowerCase();
if (answer.equals("y")) {
// Сохраняем выбранные кубики и перебрасываем остальные
spiel.rerollWürfel();
displayDiceValues();
currentRoll++;
} else {
break;
}
}
}
public void displayCategories() {
System.out.println("Available categories:");
for (int i = 1; i <= 17; i++) {
System.out.println(i + ". " + spiel.getCategoryName(i));
}
}
private void askUserForCategory() {
System.out.print("Enter category (1-17): ");
int categoryIndex = scanner.nextInt();
scanner.nextLine(); // consume newline left-over
//spiel.setKategorieScore(categoryIndex, calculateCategoryScore(categoryIndex));
int score = calculateCategoryScore(categoryIndex, würfel);
spiel.setKategorieScore(categoryIndex, score);
// Здесь добавьте код для присвоения результата игроку
int playerIndex = spiel.getCurrentPlayerIndex();
spiel.getSpieler().get(playerIndex).setScoreForCategory(categoryIndex, score);
}
private int calculateCategoryScore(int categoryIndex, Würfel würfel) {
return spiel.calculateKategorieScore(categoryIndex, würfel);
}
private void displayScore() {
System.out.println("Score: " + spiel.calculateTotalScore(würfel));
}
private boolean isGameFinished() {
// implement game finish condition here
return false;
}
private void displayFinalScore() {
System.out.println("Final score: " + spiel.calculateTotalScore(würfel));
}
public int[] getDiceValues() {
return spiel.getWürfelValues();
}
public void setCategoryScore(int categoryIndex, int score) {
spiel.setKategorieScore(categoryIndex, score);
}
public boolean hasCategoryBeenUsed(int categoryIndex) {
return spiel.hasCategoryBeenUsed(categoryIndex);
}
public List<String> getHighscores() {
List<String> highscores = spiel.getHighscores();
System.out.println("Highscores:");
for (String score : highscores) {
System.out.println(score);
}
return highscores;
}
public void deleteHighscores() {
spiel.getHighscores().clear();
System.out.println("Highscores have been deleted.");
}
}