From ad88776a5a333f427b40d1568fd31cfeca3502f6 Mon Sep 17 00:00:00 2001 From: danai Date: Mon, 6 May 2024 23:30:39 +0200 Subject: [PATCH] Fassade package commit, KniffelSpiel Klasse --- KniffelSpiel/src/fassade/KniffelSpiel.java | 189 +++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 KniffelSpiel/src/fassade/KniffelSpiel.java diff --git a/KniffelSpiel/src/fassade/KniffelSpiel.java b/KniffelSpiel/src/fassade/KniffelSpiel.java new file mode 100644 index 0000000..f1c8ad9 --- /dev/null +++ b/KniffelSpiel/src/fassade/KniffelSpiel.java @@ -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 getHighscores() { + + List 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."); + } + + +} +