added classes for client and server state machine
- a client state machine consits out of a 'ClientState' (every state of the machine) and a 'ClientStateMachine' (every state, which consists out of states), the machine starts with the ClientAutomaton - analog for server - started to implement logic for the server, transition from 'Lobby' to 'GameState'
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package pp.mdga.game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import pp.mdga.server.Observer;
|
||||
|
||||
/**
|
||||
* The Game class represents the game state of the Ludo game.
|
||||
@@ -12,7 +12,7 @@
|
||||
public class Game {
|
||||
private int diceModifier = 1;
|
||||
private int diceEyes;
|
||||
private Map <Color, Player> players;
|
||||
private Map<Color, Player> players = new HashMap<Color, Player>();
|
||||
private Statistic gameStatistics;
|
||||
private ArrayList<BonusCard> drawPile;
|
||||
private ArrayList<BonusCard> discardPile = new ArrayList<>();
|
||||
@@ -21,13 +21,17 @@ public class Game {
|
||||
private LinkedList<Color> order;
|
||||
private Map<Color, Integer> playerConnectionID;
|
||||
|
||||
private ArrayList<Observer> observers = new ArrayList<>();
|
||||
private Boolean gameHasStarted = false;
|
||||
private Boolean playerHasDisconnected = 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(){
|
||||
public Game() {
|
||||
gameStatistics = new Statistic();
|
||||
drawPile = new ArrayList<>();
|
||||
for (int i = 0; i < AMOUNT_OF_TURBO_CARDS; i++) {
|
||||
@@ -223,7 +227,7 @@ public void setPlayerConnectionID(Map<Color, Integer> playerConnectionID) {
|
||||
/**
|
||||
* This method sets the player connection ID.
|
||||
*
|
||||
* @param color the color of the player
|
||||
* @param color the color of the player
|
||||
* @param connectionID the new connection ID
|
||||
*/
|
||||
public void setPlayerConnectionID(Color color, int connectionID) {
|
||||
@@ -243,7 +247,7 @@ public int getPlayerConnectionID(Color color) {
|
||||
/**
|
||||
* This method adds a player to the game.
|
||||
*
|
||||
* @param color the color of the player
|
||||
* @param color the color of the player
|
||||
* @param player the player to be added
|
||||
*/
|
||||
public void addPlayer(Color color, Player player) {
|
||||
@@ -258,4 +262,71 @@ public void addPlayer(Color color, Player 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;
|
||||
if (gameHasStarted) notifyObservers();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the player has disconnected.
|
||||
*
|
||||
* @return the player has disconnected
|
||||
*/
|
||||
public Boolean getPlayerHasDisconnected() {
|
||||
return playerHasDisconnected;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 notifies the observers.
|
||||
*/
|
||||
public void notifyObservers() {
|
||||
for (Observer observer : new ArrayList<>(observers)) {
|
||||
observer.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,4 +79,13 @@ public BonusCard removeHandCard(BonusCard card) {
|
||||
}
|
||||
return cardToRemove;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the id of the connection to the client represented by this player.
|
||||
*
|
||||
* @return the id
|
||||
*/
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user