addedn the conentents for all messages regarding the BPMN diagramm and own interpretation. also created an identifier for pieces to be used for network communication between server and client so that they talk about the same piece.
422 lines
10 KiB
Java
422 lines
10 KiB
Java
package pp.mdga.game;
|
|
|
|
import java.util.*;
|
|
|
|
import pp.mdga.server.Observer;
|
|
|
|
/**
|
|
* The Game class represents the game state of the Ludo game.
|
|
* It contains all the information needed to play the game.
|
|
* The game state is updated by the game logic.
|
|
*/
|
|
public class Game {
|
|
private int diceModifier = 1;
|
|
private int diceEyes;
|
|
private Map<Color, Player> players = new HashMap<Color, Player>();
|
|
private Statistic gameStatistics;
|
|
private ArrayList<BonusCard> drawPile;
|
|
private ArrayList<BonusCard> discardPile = new ArrayList<>();
|
|
private Board board;
|
|
private Color activeColor;
|
|
private LinkedList<Color> order;
|
|
private Map<Color, Integer> playerConnectionID;
|
|
|
|
private ArrayList<Observer> observers = new ArrayList<>();
|
|
private Player startPlayer;
|
|
private Boolean gameHasStarted = false;
|
|
private Boolean playerHasDisconnected = false;
|
|
private Boolean gameIsInterrupted = false;
|
|
private Boolean allRanked = false;
|
|
private Boolean movablePieces = false;
|
|
|
|
private static final int AMOUNT_OF_TURBO_CARDS = 16;
|
|
private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
|
|
|
|
/**
|
|
* This constructor creates a new Game object.
|
|
*/
|
|
public Game() {
|
|
gameStatistics = new Statistic();
|
|
drawPile = new ArrayList<>();
|
|
for (int i = 0; i < AMOUNT_OF_TURBO_CARDS; i++) {
|
|
drawPile.add(BonusCard.TURBO);
|
|
}
|
|
for (int i = 0; i < AMOUNT_OF_SHIELD_AND_SWAP_CARDS; i++) {
|
|
drawPile.add(BonusCard.SWAP);
|
|
drawPile.add(BonusCard.SHIELD);
|
|
}
|
|
board = new Board();
|
|
}
|
|
|
|
/**
|
|
* This method returns the dice modifier.
|
|
*
|
|
* @return the dice modifier
|
|
*/
|
|
public int getDiceModifier() {
|
|
return diceModifier;
|
|
}
|
|
|
|
/**
|
|
* This method sets the dice modifier.
|
|
*
|
|
* @param diceModifier the new dice modifier
|
|
*/
|
|
public void setDiceModifier(int diceModifier) {
|
|
this.diceModifier = diceModifier;
|
|
}
|
|
|
|
/**
|
|
* This method returns the dice eyes.
|
|
*
|
|
* @return the dice eyes
|
|
*/
|
|
public int getDiceEyes() {
|
|
return diceEyes;
|
|
}
|
|
|
|
/**
|
|
* This method sets the dice eyes.
|
|
*
|
|
* @param diceEyes the new dice eyes
|
|
*/
|
|
public void setDiceEyes(int diceEyes) {
|
|
this.diceEyes = diceEyes;
|
|
}
|
|
|
|
/**
|
|
* This method returns the players.
|
|
*
|
|
* @return the players
|
|
*/
|
|
public Map<Color, Player> getPlayers() {
|
|
return players;
|
|
}
|
|
|
|
/**
|
|
* This method sets the players.
|
|
*
|
|
* @param players the new players
|
|
*/
|
|
public void setPlayers(Map<Color, Player> players) {
|
|
this.players = players;
|
|
}
|
|
|
|
/**
|
|
* This method returns the game statistics.
|
|
*
|
|
* @return the game statistics
|
|
*/
|
|
public Statistic getGameStatistics() {
|
|
return gameStatistics;
|
|
}
|
|
|
|
/**
|
|
* This method sets the game statistics.
|
|
*
|
|
* @param gameStatistics the new game statistics
|
|
*/
|
|
public void setGameStatistics(Statistic gameStatistics) {
|
|
this.gameStatistics = gameStatistics;
|
|
}
|
|
|
|
/**
|
|
* This method returns the draw pile.
|
|
*
|
|
* @return the draw pile
|
|
*/
|
|
public ArrayList<BonusCard> getDrawPile() {
|
|
return drawPile;
|
|
}
|
|
|
|
/**
|
|
* This method sets the draw pile.
|
|
*
|
|
* @param drawPile the new draw pile
|
|
*/
|
|
public void setDrawPile(ArrayList<BonusCard> drawPile) {
|
|
this.drawPile = drawPile;
|
|
}
|
|
|
|
/**
|
|
* This method returns the discard pile.
|
|
*
|
|
* @return the discard pile
|
|
*/
|
|
public ArrayList<BonusCard> getDiscardPile() {
|
|
return discardPile;
|
|
}
|
|
|
|
/**
|
|
* This method sets the discard pile.
|
|
*
|
|
* @param discardPile the new discard pile
|
|
*/
|
|
public void setDiscardPile(ArrayList<BonusCard> discardPile) {
|
|
this.discardPile = discardPile;
|
|
}
|
|
|
|
/**
|
|
* This method returns the board.
|
|
*
|
|
* @return the board
|
|
*/
|
|
public Board getBoard() {
|
|
return board;
|
|
}
|
|
|
|
/**
|
|
* This method sets the board.
|
|
*
|
|
* @param board the new board
|
|
*/
|
|
public void setBoard(Board board) {
|
|
this.board = board;
|
|
}
|
|
|
|
/**
|
|
* This method returns the active color.
|
|
*
|
|
* @return the active color
|
|
*/
|
|
public Color getActiveColor() {
|
|
return activeColor;
|
|
}
|
|
|
|
/**
|
|
* This method sets the active color.
|
|
*
|
|
* @param activeColor the new active color
|
|
*/
|
|
public void setActiveColor(Color activeColor) {
|
|
this.activeColor = activeColor;
|
|
}
|
|
|
|
/**
|
|
* This method returns the order of the players.
|
|
*
|
|
* @return the order of the players
|
|
*/
|
|
public LinkedList<Color> getOrder() {
|
|
return order;
|
|
}
|
|
|
|
/**
|
|
* This method sets the order of the players.
|
|
*
|
|
* @param order the new order of the players
|
|
*/
|
|
public void setOrder(LinkedList<Color> order) {
|
|
this.order = order;
|
|
}
|
|
|
|
/**
|
|
* This method returns the player connection ID.
|
|
*
|
|
* @return the player connection ID
|
|
*/
|
|
public Map<Color, Integer> getPlayerConnectionID() {
|
|
return playerConnectionID;
|
|
}
|
|
|
|
/**
|
|
* This method sets the player connection ID.
|
|
*
|
|
* @param playerConnectionID the new player connection ID
|
|
*/
|
|
public void setPlayerConnectionID(Map<Color, Integer> playerConnectionID) {
|
|
this.playerConnectionID = playerConnectionID;
|
|
}
|
|
|
|
/**
|
|
* This method sets the player connection ID.
|
|
*
|
|
* @param color the color of the player
|
|
* @param connectionID the new connection ID
|
|
*/
|
|
public void setPlayerConnectionID(Color color, int connectionID) {
|
|
playerConnectionID.put(color, connectionID);
|
|
}
|
|
|
|
/**
|
|
* This method returns the player connection ID.
|
|
*
|
|
* @param color the color of the player
|
|
* @return the player connection ID
|
|
*/
|
|
public int getPlayerConnectionID(Color color) {
|
|
return playerConnectionID.get(color);
|
|
}
|
|
|
|
/**
|
|
* This method adds a player to the game.
|
|
*
|
|
* @param color the color of the player
|
|
* @param player the player to be added
|
|
*/
|
|
public void addPlayer(Color color, Player player) {
|
|
players.put(color, player);
|
|
}
|
|
|
|
/**
|
|
* This method removes a player from the game.
|
|
*
|
|
* @param color the color of the player
|
|
*/
|
|
public void removePlayer(Color color) {
|
|
players.remove(color);
|
|
}
|
|
|
|
/**
|
|
* This method adds an observer to the game.
|
|
*
|
|
* @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);
|
|
}
|
|
|
|
/**
|
|
* This method returns the game has started.
|
|
*
|
|
* @return the game has started
|
|
*/
|
|
public Boolean getGameHasStarted() {
|
|
return gameHasStarted;
|
|
}
|
|
|
|
/**
|
|
* This method sets the game has started.
|
|
*
|
|
* @param gameHasStarted the new game has started
|
|
*/
|
|
public void setGameHasStarted(Boolean gameHasStarted) {
|
|
this.gameHasStarted = gameHasStarted;
|
|
}
|
|
|
|
/**
|
|
* This method returns the player has disconnected.
|
|
*
|
|
* @return the player has disconnected
|
|
*/
|
|
public Boolean playerHasDisconnected() {
|
|
return playerHasDisconnected;
|
|
}
|
|
|
|
/**
|
|
* This method sets the game interruption state.
|
|
*
|
|
* @param gameIsInterrupted the new game interruption state
|
|
*/
|
|
public void setGameIsInterrupted(Boolean gameIsInterrupted) {
|
|
this.gameIsInterrupted = gameIsInterrupted;
|
|
if (!gameIsInterrupted) notifyObservers();
|
|
}
|
|
|
|
/**
|
|
* This method returns whether the game is interrupted.
|
|
*
|
|
* @return true if the game is interrupted, false otherwise
|
|
*/
|
|
public Boolean gameIsInterrupted() {
|
|
return gameIsInterrupted;
|
|
}
|
|
|
|
/**
|
|
* 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 game interruption state.
|
|
*
|
|
* @param movablePieces the new game interruption state
|
|
*/
|
|
public void setMovablePieces(Boolean movablePieces) {
|
|
this.movablePieces = movablePieces;
|
|
if (!movablePieces) notifyObservers();
|
|
}
|
|
|
|
/**
|
|
* This method sets the player has disconnected.
|
|
*
|
|
* @param playerHasDisconnected the new player has disconnected
|
|
*/
|
|
public void setPlayerHasDisconnected(Boolean playerHasDisconnected) {
|
|
this.playerHasDisconnected = playerHasDisconnected;
|
|
if (playerHasDisconnected) 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 (allRanked) notifyObservers();
|
|
}
|
|
|
|
/**
|
|
* This method returns the start player.
|
|
*
|
|
* @return the start player
|
|
*/
|
|
public Player getStartPlayer() {
|
|
return startPlayer;
|
|
}
|
|
|
|
/**
|
|
* This method sets the start player.
|
|
*
|
|
* @param startPlayer the new start player
|
|
*/
|
|
public void setStartPlayer(Player startPlayer) {
|
|
this.startPlayer = startPlayer;
|
|
}
|
|
|
|
/**
|
|
* This method notifies the observers.
|
|
*/
|
|
public void notifyObservers() {
|
|
for (Observer observer : new ArrayList<>(observers)) {
|
|
observer.update();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This method returns the piece through its identifier.
|
|
*
|
|
* @param identifier the identifier of the piece
|
|
* @return the piece
|
|
*/
|
|
public Piece getPieceThroughIdentifier(String identifier) {
|
|
String[] parts = identifier.split("-");
|
|
Color color = Color.valueOf(parts[0]);
|
|
int index = Integer.parseInt(parts[1]);
|
|
return board.getPlayerData().get(color).getPieces()[index];
|
|
}
|
|
}
|