From ff31335a98adf37108697a353931589521573192 Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Sun, 1 Dec 2024 19:38:26 +0100 Subject: [PATCH] added a new class 'Die' to handle the dice. added this class to the class 'Game' --- .../model/src/main/java/pp/mdga/game/Die.java | 133 ++++++++++++++++++ .../src/main/java/pp/mdga/game/Game.java | 103 ++++---------- 2 files changed, 161 insertions(+), 75 deletions(-) create mode 100644 Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java new file mode 100644 index 00000000..047cf6ce --- /dev/null +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Die.java @@ -0,0 +1,133 @@ +package pp.mdga.game; + +import java.util.random.RandomGenerator; +import java.util.random.RandomGeneratorFactory; + +/** + * This class represents a simple die with the possibilities to shuffle and return the roll result. + * An important fact, the sum of ZERO_EYE, NORMAL_EYE and DOUBLE_EYE have to be 1.0; + */ +public class Die { + /** + * The maximum number of eyes on the die. + */ + public static final int MAXIMUM_EYES = 6; + + /** + * The probability of rolling a zero eye. + */ + public static final double ZERO_EYE = 0.2; + + /** + * The probability of rolling a normal eye. + */ + public static final double NORMAL_EYE = 0.2; + + /** + * The probability of rolling a double eye. + */ + public static final double DOUBLE_EYE = 0.6; + + /** + * The modifier applied to the die roll. + */ + private int dieModifier = 1; + + /** + * The random number generator used for die rolls. + */ + private final RandomGenerator random; + + /** + * The result of the last die roll. + */ + private int lastNumberOfDice; + + /** + * This constructor is used to create a new Die object with a random seed. + */ + public Die() { + this.random = RandomGeneratorFactory.of("Random").create(); + } + + /** + * This constructor is used to create a new Die object with a given seed. + * + * @param seed the seed for the random number generator + */ + public Die(long seed) { + this.random = RandomGeneratorFactory.of("Random").create(seed); + } + + /** + * This method will be used to return a random number generated by the random attribute of Die class. + * + * @return lastNumberOfDice as an Integer. + */ + public int shuffle() { + this.lastNumberOfDice = this.random.nextInt(MAXIMUM_EYES) + 1; + + return this.lastNumberOfDice; + } + + /** + * This method will be used to return the given roll parameter. + * It will be used for test cases. + * + * @param roll as the roll result which will be returned as an Integer. + * @return lastNumberOfDice as an Integer. + */ + public int shuffle(int roll) { + this.lastNumberOfDice = roll; + + return this.lastNumberOfDice; + } + + /** + * This method will be used to modify the value of dieModifier attribute of Die class. + */ + public void modify() { + float randomFloat = this.random.nextFloat(); + if (randomFloat < ZERO_EYE) { + this.dieModifier = 0; + } else if (ZERO_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE) { + this.dieModifier = 1; + } else if (ZERO_EYE + NORMAL_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE + DOUBLE_EYE) { + this.dieModifier = 2; + } + } + + /** + * This method will be used to reset all values of the Die class to its origins. + */ + public void reset() { + this.dieModifier = 1; + } + + /** + * This method will be used to return dieModifier attribute of Die class. + * + * @return dieModifier as an Integer. + */ + public int getDieModifier() { + return this.dieModifier; + } + + /** + * This method will be used return lastNumberOfDice attribute of Die class. + * + * @return lastNumberOfDice as an Integer. + */ + public int getLastNumberOfDice() { + return this.lastNumberOfDice; + } + + /** + * This method will be used to set dieModifier attribute of Die class to the given dieModifier parameter. + * + * @param dieModifier as the new value of dieModifier as an Integer. + */ + public void setDieModifier(int dieModifier) { + this.dieModifier = dieModifier; + } +} diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java index 883786d3..247f77f9 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Game.java @@ -9,25 +9,43 @@ */ public class Game { /** - * Constants. + * The number of turbo cards available in the game. */ public static final int AMOUNT_OF_TURBO_CARDS = 16; - public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; /** - * Attributes. + * The number of shield and swap cards available in the game. */ + public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; + + // The modifier applied to the dice roll. private int diceModifier = 1; + + // The number of eyes shown on the dice. private int diceEyes; + + // A map of player IDs to Player objects. private Map players = new HashMap<>(); + + // The statistics of the game. private Statistic gameStatistics; + + // The pile of bonus cards available for drawing. private List drawPile; + + // The pile of bonus cards that have been discarded. private List discardPile = new ArrayList<>(); + + // The game board. private Board board; + + // The die used in the game. + private Die die; + + // The color of the active player. private Color activeColor; - private final ArrayList observers = new ArrayList<>(); - private boolean allRanked = false; - private boolean movablePieces = false; + + // A flag indicating whether all players are ready. private boolean allReady = false; /** @@ -66,27 +84,10 @@ public void removePlayer(int id) { } /** - * This method adds an observer to the game. + * This method updates the active state of a player. * - * @param observer the observer to be added - */ - public void addObserver(Observer observer) { - observers.add(observer); - } - - /** - * This method removes an observer from the game. - * - * @param observer the observer to be removed - */ - public void removeObserver(Observer observer) { - observers.remove(observer); - } - - /** - * - * @param id - * @param active + * @param id the id of the player + * @param active the new active state */ public void updatePlayerActiveState(int id, boolean active) { this.players.get(id).setActive(active); @@ -139,7 +140,7 @@ public int getNumberOfActivePlayers() { * * @return the piece specified by the UUID */ - public Piece getPieceThroughUUID(UUID pieceId){ + public Piece getPieceThroughUUID(UUID pieceId) { for (var playerData : board.getPlayerData().values()) { for (var piece : playerData.getPieces()) { if (piece.getUuid().equals(pieceId)) { @@ -150,15 +151,6 @@ public Piece getPieceThroughUUID(UUID pieceId){ return null; } - /** - * This method notifies the observers. - */ - public void notifyObservers() { - for (Observer observer : new ArrayList<>(observers)) { - //TODO: observer.update(); - } - } - /** * This method returns the dice modifier. * @@ -231,15 +223,6 @@ public Color getActiveColor() { return activeColor; } - /** - * This method returns whether the game is interrupted. - * - * @return true if the game is interrupted, false otherwise - */ - public Boolean getMovablePieces() { - return movablePieces; - } - /** * This method sets the dice modifier. * @@ -312,35 +295,6 @@ public void setActiveColor(Color activeColor) { this.activeColor = activeColor; } - /** - * This method sets the game interruption state. - * - * @param movablePieces the new game interruption state - */ - public void setMovablePieces(Boolean movablePieces) { - this.movablePieces = movablePieces; - if (Boolean.FALSE.equals(movablePieces)) notifyObservers(); - } - - /** - * This method returns whether all players have ranked. - * - * @return true if all players have ranked, false otherwise - */ - public Boolean allRanked() { - return allRanked; - } - - /** - * This method sets whether all players have ranked. - * - * @param allRanked the new all ranked state - */ - public void setAllRanked(Boolean allRanked) { - this.allRanked = allRanked; - if (Boolean.TRUE.equals(allRanked)) notifyObservers(); - } - /** * This method returns the all ready state. * @@ -357,6 +311,5 @@ public Boolean allReady() { */ public void setAllReady(Boolean allReady) { this.allReady = allReady; - if (Boolean.TRUE.equals(allReady)) notifyObservers(); } }