This commit is contained in:
Johannes Schmelz
2024-12-12 17:38:46 +01:00
parent 7c79bb77e7
commit e2db058fc7
78 changed files with 1087 additions and 369 deletions

View File

@@ -69,10 +69,13 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
/** The current state of the client game logic. */
private ClientState state = new LobbyState(this);
/** The player handler containing information about all players in the game. */
private PlayerHandler playerHandler;
/** The board manager containing information about all fields on the board. */
private BoardManager boardManager = new BoardManager();
/** The trade handler containing information about trades in the game. */
private TradeHandler tradeHandler;

View File

@@ -7,6 +7,7 @@
package pp.monopoly.game.server;
import java.lang.System.Logger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -39,24 +40,35 @@ import pp.monopoly.model.fields.WacheField;
* Class representing a player
*/
@Serializable
public class Player implements FieldVisitor<Void>{
public class Player implements FieldVisitor<Void> {
/** The logger for this class */
private final static Logger LOGGER = System.getLogger(Player.class.getName());
/** The player id */
private final int id;
/** The player name */
private String name;
/** The player account balance */
private int accountBalance = 15000;
/** The player figure */
private String figure;
/** The player properties */
private Set<Integer> properties = new HashSet<>();
/** The number of GetOutOfJailCards owned by the player */
private int getOutOfJailCard;
/** The current field id of the player */
private int fieldID;
/** The result of the last dice roll */
private DiceResult rollResult;
/** The player handler */
private transient final PlayerHandler handler;
/** The current state of the player */
private transient PlayerState state = new WaitForTurnState();
/**
* Default constructor for serialization purposes.
*/
private Player(){
id = 0;
handler = null;
this(0, null);
}
/**
@@ -69,6 +81,7 @@ public class Player implements FieldVisitor<Void>{
this.name = name;
this.id = id;
this.handler = handler;
LOGGER.log(Logger.Level.INFO, "Player created: " + name);
}
/**
@@ -77,8 +90,7 @@ public class Player implements FieldVisitor<Void>{
* @param handler the PlayerHandler this player is a part of
*/
public Player(int id, PlayerHandler handler) {
this.id = id;
this.handler = handler;
this(id, null, handler);
}
/**
@@ -97,6 +109,10 @@ public class Player implements FieldVisitor<Void>{
return figure;
}
/**
* Returns the color of the player
* @return the color of the player
*/
public static PlayerColor getColor(int id) {
switch ((id%6)+1) {
case 1: return PlayerColor.CYAN;
@@ -143,6 +159,10 @@ public class Player implements FieldVisitor<Void>{
return fieldID;
}
/**
* Sets the state of the player to active
* If the player is in jail, the player will be asked to pay the bail
*/
void setActive() {
System.out.println("State: "+state.getClass().getName());
if(!(state instanceof JailState)) {
@@ -153,6 +173,11 @@ public class Player implements FieldVisitor<Void>{
}
}
/**
* Finish the turn of the player
* If the player has a negative account balance, the player will be bankrupted
* @return true if the turn was finished successfully, false if the player is bankrupt
*/
boolean finishTurn() {
if(canFinishTurn()) {
if (state instanceof ActiveState) state = new WaitForTurnState();
@@ -164,14 +189,26 @@ public class Player implements FieldVisitor<Void>{
}
}
/**
* Checks if the player can finish the turn
* @return true if the player can finish the turn, false if the player is bankrupt
*/
boolean canFinishTurn() {
return accountBalance >= 0;
}
/**
* Returns the current state of the player
* @return the current state of the player
*/
public PlayerState getState() {
return state;
}
/**
* Sets the state of the player
* @param state the new state of the player
*/
public void setState(PlayerState state) {
this.state = state;
}
@@ -206,7 +243,7 @@ public class Player implements FieldVisitor<Void>{
return fieldID;
}
/**
/**
* Sets the player to the specified Position on the board
* @param position the position to move to
* @return the new position
@@ -297,7 +334,7 @@ public class Player implements FieldVisitor<Void>{
/**
* Return the number of GEtOutOfJailCards owned by this player
* @return
* @return the number of GetOutOfJailCards owned by this player
*/
public int getNumJailCard() {
return getOutOfJailCard;
@@ -335,7 +372,11 @@ public class Player implements FieldVisitor<Void>{
state.useJailCard();
}
private void sendRentNotification(String keyword, Player player, int amount) {
/**
* Sends a notification to the player
* @param keyword the keyword of the notification
*/
private void sendNotification(String keyword, Player player, int amount) {
NotificationMessage msg = new NotificationMessage(keyword);
msg.setRentAmount(amount);
msg.setRentOwnerId(player.getName());
@@ -351,8 +392,8 @@ public class Player implements FieldVisitor<Void>{
int rent = field.calcRent();
field.getOwner().earnMoney(rent);
pay(rent);
sendRentNotification("ReceivedRent", this, rent);
sendRentNotification("rent", field.getOwner(), rent);
sendNotification("ReceivedRent", this, rent);
sendNotification("rent", field.getOwner(), rent);
}
return null;
}
@@ -369,8 +410,8 @@ public class Player implements FieldVisitor<Void>{
int rent = rollResult.calcTotal()*factor;
field.getOwner().earnMoney(rent);
pay(rent);
sendRentNotification("ReceivedRent", this, rent);
sendRentNotification("rent", field.getOwner(), rent);
sendNotification("ReceivedRent", this, rent);
sendNotification("rent", field.getOwner(), rent);
}
return null;
}
@@ -384,8 +425,8 @@ public class Player implements FieldVisitor<Void>{
field.getOwner().earnMoney(rent);
pay(rent);
sendRentNotification("ReceivedRent", this, rent);
sendRentNotification("rent", field.getOwner(), rent);
sendNotification("ReceivedRent", this, rent);
sendNotification("rent", field.getOwner(), rent);
}
return null;
}
@@ -450,6 +491,9 @@ public class Player implements FieldVisitor<Void>{
handler.getLogic().send(this, new JailEvent(true));
}
/**
* Sends the player to jail by setting position and state.
*/
void jail() {
state = new JailState();
fieldID = 10;
@@ -473,6 +517,10 @@ public class Player implements FieldVisitor<Void>{
return count;
}
/**
* Return the number of Houses owned by the player
* @return the number of houses owned by the player
*/
public int getNumHouses() {
int total = 0;
for (PropertyField field : getPropertyFields()) {
@@ -483,6 +531,10 @@ public class Player implements FieldVisitor<Void>{
return total;
}
/**
* Return the number of Hotels owned by the player
* @return the number of hotels owned by the player
*/
public int getNumHotels() {
int total = 0;
for (PropertyField field : getPropertyFields()) {
@@ -526,7 +578,10 @@ public class Player implements FieldVisitor<Void>{
DiceResult rollDice() {
return state.rollDice();
}
/**
* Bankrupts the player by removing all properties and setting the account balance to 0.
*/
private void bankrupt() {
for (PropertyField field : getPropertyFields()) {
field.setOwner(null);
@@ -654,6 +709,10 @@ public class Player implements FieldVisitor<Void>{
}
}
/**
* A class to represent the WaitForTurn PlayerState
* Set when it is not the players turn
*/
private class WaitForTurnState implements PlayerState {
@Override
@@ -676,10 +735,18 @@ public class Player implements FieldVisitor<Void>{
return "Player{name=" + name + ", figure=" + figure + "}";
}
/**
* Adds a property to the player
* @param id the id of the property to be added
*/
public void addProperty(Integer id) {
properties.add(id);
}
/**
* Removes a property from the player
* @param id the id of the property to be removed
*/
public void removeProperty(Integer id) {
properties.remove(id);
}

View File

@@ -172,12 +172,20 @@ public class PlayerHandler {
players.get(0).setActive();
}
/**
* Sets the starting balance for all players
* @param amount the amount to be set
*/
public void setStartBalance(int amount) {
for (Player player : players) {
player.setAccountBalance(amount);
}
}
/**
* Sets a player to have an extra turn
* @param player the player to have an extra turn
*/
public void extraTurn(Player player) {
if (players.contains(player)) extra = player;
}

View File

@@ -38,15 +38,26 @@ import pp.monopoly.model.fields.PropertyField;
* Manages game states, player interactions, and message handling.
*/
public class ServerGameLogic implements ClientInterpreter {
/**
* The logger for the ServerGameLogic class.
*/
private static final Logger LOGGER = System.getLogger(ServerGameLogic.class.getName());
/** The game configuration. */
private final MonopolyConfig config;
/** The player handler for managing players. */
private final PlayerHandler playerHandler = new PlayerHandler(this);
/** The sender used to send messages to clients. */
private final ServerSender serverSender;
/** The current state of the game. */
private ServerState state = ServerState.LOBBY;
/** The maximum number of players allowed in the game. */
private static final int MAX_PLAYERS = 6;
/** The board manager for managing the game board. */
private BoardManager boardManager = new BoardManager();
/** The deck helper for managing card decks. */
private final DeckHelper deckHelper = new DeckHelper();
/** The amount of money each player starts with. */
private int startMoney;
/**
@@ -383,10 +394,21 @@ public class ServerGameLogic implements ClientInterpreter {
return boardManager;
}
/**
* Retrieves the player handler, which manages player actions and turns.
*
* @param id the id of the player to retrieve
* @return the PlayerHandler instance managing players
*/
public Player getPlayerById(int id) {
return playerHandler.getPlayerById(id);
}
/**
* Retrieves the deck helper, which manages decks of cards.
*
* @return the deck helper
*/
public DeckHelper getDeckHelper() {
return deckHelper;
}
@@ -452,6 +474,9 @@ public class ServerGameLogic implements ClientInterpreter {
updateAllPlayers();
}
/**
* Generates a predefined game state for testing and demo purposes.
*/
private void generatePredefinedGameState() {
if(playerHandler.getPlayerCount() < 2) {

View File

@@ -4,14 +4,24 @@ import java.util.Set;
import com.jme3.network.serializing.Serializable;
/**
* Sent by the client to the server to alter the properties of a field.
*/
@Serializable
public class AlterProperty extends ClientMessage{
/** The keyword to indicate what action should be performed */
private String keyword;
/** The properties that should be altered */
private Set<Integer> properties;
/** No-Args Construcotr for serilization */
private AlterProperty() {}
/**
* Create a new AlterProperty message.
* @param keyword The keyword to indicate what action should be performed
*/
public AlterProperty(String keyword) {
this.keyword = keyword;
}
@@ -21,14 +31,26 @@ public class AlterProperty extends ClientMessage{
interpreter.received(this, from);
}
/**
* Returns the keyword to indicate what action should be performed.
* @return keyword The keyword to indicate what action should be performed
*/
public String getKeyword() {
return keyword;
}
/**
* Sets the properties that should be altered.
* @param properties The properties that should be altered
*/
public void setProperties(Set<Integer> properties) {
this.properties = properties;
}
/**
* Returns the properties that should be altered.
* @return properties The properties that should be altered
*/
public Set<Integer> getProperties() {
return properties;
}

View File

@@ -2,17 +2,30 @@ package pp.monopoly.message.client;
import com.jme3.network.serializing.Serializable;
/**
* Sent by the client to answer a notification.
*/
@Serializable
public class NotificationAnswer extends ClientMessage{
/** The keyword to indicate the awnser */
private String keyword;
/** No-Args constructor for serilization */
private NotificationAnswer() {}
/**
* Create a new NotificationAnswer
* @param keyword the keyword to indicate the awnser
*/
public NotificationAnswer(String keyword) {
this.keyword = keyword;
}
/**
* Get the keyword to indicate the awnser
* @return the keyword to indicate the awnser
*/
public String getKeyword() {
return keyword;
}

View File

@@ -31,18 +31,34 @@ public class PlayerReady extends ClientMessage {
this.startMoney = startMoney;
}
/**
* Returns the name of the player.
* @return the name of the player
*/
public String getName() {
return name;
}
/**
* Returns the figure of the player.
* @return the figure of the player
*/
public String getFigure() {
return figure;
}
/**
* Returns whether the player is ready.
* @return whether the player is ready
*/
public boolean isReady() {
return isReady;
}
/**
* Returns the initial capital of the game.
* @return the initial capital of the game
*/
public int getStartMoney() {
return startMoney;
}

View File

@@ -25,6 +25,11 @@ public class TradeOffer extends ClientMessage{
this.tradehandler = tradehandler;
}
/**
* Returns the tradehandler.
*
* @return the tradehandler
*/
public TradeHandler getTradeHandler() { return tradehandler; }
@Override

View File

@@ -8,8 +8,10 @@ import com.jme3.network.serializing.Serializable;
@Serializable
public class ViewAssetsRequest extends ClientMessage{
public ViewAssetsRequest() {
}
/**
* Constructor needed for the serialization.
*/
public ViewAssetsRequest() {}
@Override
public void accept(ClientInterpreter interpreter, int from) {

View File

@@ -2,26 +2,44 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Sent from server to client to inform about a build action.
*/
@Serializable
public class BuildInfo extends ServerMessage {
/** Id of the field that was altered */
private final int id;
/** True if a house/hotel was added, false if it was removed */
private final boolean added;
/** No-args constructor required for serialization */
private BuildInfo() {
this(0, false);
}
/**
* Create a new BuildInfo message.
* @param id Id of the field that was altered
* @param added True if a house/hotel was added, false if it was removed
*/
public BuildInfo(int id, boolean added) {
this.id = id;
this.added = added;
}
/**
* Get the id of the field that was altered.
* @return Id of the field that was altered
*/
public int getId() {
return id;
}
/**
* Check if a house/hotel was added.
* @return True if a house/hotel was added, false if it was removed
*/
public boolean isAdded() {
return added;
}

View File

@@ -2,6 +2,9 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Sent by the server to the client to request the client to show a buy property popup.
*/
@Serializable
public class BuyPropertyRequest extends ServerMessage{

View File

@@ -4,10 +4,15 @@ import java.util.List;
import com.jme3.network.serializing.Serializable;
/**
* Sent by the server to the client to inform the client about the result of a dice roll.
*/
@Serializable
public class DiceResult extends ServerMessage{
/** The first value of the DiceRoll */
private final int a;
/** The second value of the DiceRoll */
private final int b;
/**
@@ -18,11 +23,20 @@ public class DiceResult extends ServerMessage{
b = 1;
}
/**
* Creates a new DiceResult with the given values.
* @param a The first value of the DiceRoll
* @param b The second value of the DiceRoll
*/
public DiceResult(int a, int b) {
this.a = a;
this.b = b;
}
/**
* Returns the values of the DiceRoll.
* @return The values of the DiceRoll
*/
public List<Integer> getRollResult() {
return List.of(a,b);
}
@@ -31,11 +45,19 @@ public class DiceResult extends ServerMessage{
public void accept(ServerInterpreter interpreter) {
interpreter.received(this);
}
/**
* Returns whether the DiceRoll is a doublets.
* @return Whether the DiceRoll is a doublets
*/
public boolean isDoublets() {
return a == b;
}
/**
* Returns the total value of the DiceRoll.
* @return The total value of the DiceRoll
*/
public int calcTotal() {
return a+b;
}

View File

@@ -2,8 +2,14 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Message to inform the player that the game is over and if he won or not.
*/
@Serializable
public class GameOver extends ServerMessage{
public class GameOver extends ServerMessage {
/**
* Whether the player is the winner.
*/
private boolean isWinner;
/**
@@ -11,10 +17,20 @@ public class GameOver extends ServerMessage{
*/
private GameOver() { /* empty */ }
/**
* Creates a new GameOver message.
*
* @param isWinner true if the player is the winner, false otherwise
*/
public GameOver(boolean isWinner) {
this.isWinner = isWinner;
}
/**
* Returns whether the player is the winner.
*
* @return true if the player is the winner, false otherwise
*/
public boolean isWinner() {
return isWinner;
}

View File

@@ -7,6 +7,9 @@ import pp.monopoly.game.server.PlayerHandler;
@Serializable
public class GameStart extends ServerMessage{
/**
* The PlayerHandler. This is the object that contains all the players and their information.
*/
private PlayerHandler playerHandler;
/**
@@ -14,10 +17,20 @@ public class GameStart extends ServerMessage{
*/
private GameStart() { /* empty */ }
/**
* Creates a new GameStart message.
*
* @param playerHandler the playerHandler
*/
public GameStart(PlayerHandler playerHandler) {
this.playerHandler = playerHandler;
}
/**
* Returns the PlayerHandler. This is the object that contains all the players and their information.
*
* @return the playerHandler
*/
public PlayerHandler getPlayerHandler() {
return playerHandler;
}

View File

@@ -2,9 +2,13 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Message for the client to inform him that a player is going to jail or leaving jail.
*/
@Serializable
public class JailEvent extends ServerMessage{
/** true if the player is going to jail, false if he is leaving jail */
private boolean goingToJail;
/**
@@ -12,10 +16,20 @@ public class JailEvent extends ServerMessage{
*/
private JailEvent() { /* empty */ }
/**
* Creates a new JailEvent.
*
* @param goingToJail true if the player is going to jail, false if he is leaving jail
*/
public JailEvent(boolean goingToJail) {
this.goingToJail = goingToJail;
}
/**
* Returns true if the player is going to jail, false if he is leaving jail.
*
* @return true if the player is going to jail, false if he is leaving jail
*/
public boolean isGoingToJail() {
return goingToJail;
}

View File

@@ -2,14 +2,17 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Message to inform the clients that the next player's turn has started.
* The client should now enable the buttons for the current player.
*/
@Serializable
public class NextPlayerTurn extends ServerMessage{
/**
* Default constructor for serialization purposes.
*/
public NextPlayerTurn() {
}
public NextPlayerTurn() {}
@Override
public void accept(ServerInterpreter interpreter) {

View File

@@ -2,36 +2,68 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* This class is used to send notifications to the client. The keyWord is used to identify the type of notification.
* The rentAmount is used to store the amount of rent that has to be paid and the rentOwner is used to store the id of the player that has to be paid.
*/
@Serializable
public class NotificationMessage extends ServerMessage{
/** keyWord is used to identify the type of notification */
private final String keyWord;
/** rentAmount is used to store the amount of rent that has to be paid */
private int rentAmount;
/** rentOwner is used to store the id of the player that has to be paid */
private String rentOwner;
/** Default constructor needed for serializiaton */
private NotificationMessage(){ keyWord = null;}
/**
* Constructor for the NotificationMessage
* @param keyWord the keyWord that is used to identify the type of notification
*/
public NotificationMessage(String keyWord) {
this.keyWord = keyWord;
}
/**
* Getter for the rentAmount
* @return the amount of rent that has to be paid
*/
public int getRentAmount() {
return rentAmount;
}
/**
* Setter for the rentAmount
* @param rentAmount the amount of rent that has to be paid
*/
public void setRentAmount(int rentAmount) {
this.rentAmount = rentAmount;
}
/**
* Setter for the rentOwnerId
* @param rentOwnerId the id of the player that has to be paid
*/
public void setRentOwnerId(String rentOwnerId) {
this.rentOwner = rentOwnerId;
}
/**
* Getter for the rentOwnerId
* @return the id of the player that has to be paid
*/
public String getRentOwner() {
return rentOwner;
}
/**
* Getter for the keyWord
* @return the keyWord that is used to identify the type of notification
*/
public String getKeyWord() {
return keyWord;
}

View File

@@ -4,6 +4,9 @@ import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.server.PlayerHandler;
/**
* Message for updating the status of a player.
*/
@Serializable
public class PlayerStatusUpdate extends ServerMessage{
@@ -14,10 +17,20 @@ public class PlayerStatusUpdate extends ServerMessage{
*/
private PlayerStatusUpdate() { /* empty */ }
/**
* Creates a new PlayerStatusUpdate.
*
* @param playerHandler the playerHandler
*/
public PlayerStatusUpdate(PlayerHandler playerHandler) {
this.playerHandler = playerHandler;
}
/**
* Returns the playerHandler.
*
* @return the playerHandler
*/
public PlayerHandler getPlayerHandler() {
return playerHandler;
}

View File

@@ -2,9 +2,15 @@ package pp.monopoly.message.server;
import com.jme3.network.serializing.Serializable;
/**
* Send to the client to inform the player that he has only a certain amount of time left to make his move.
*/
@Serializable
public class TimeOutWarning extends ServerMessage{
/**
* Remaining time in seconds.
*/
private int remainingTime;
/**
@@ -12,10 +18,18 @@ public class TimeOutWarning extends ServerMessage{
*/
private TimeOutWarning() { /* empty */ }
/**
* Create a new TimeOutWarning message.
* @param remainingTime Remaining time in seconds.
*/
public TimeOutWarning(int remainingTime) {
this.remainingTime = remainingTime;
}
/**
* Get the remaining time in seconds.
* @return Remaining time in seconds.
*/
public int getRemainingTime() {
return remainingTime;
}

View File

@@ -28,7 +28,18 @@ public class TradeReply extends ServerMessage{
this.tradeHandler = tradeHandler;
}
/**
* Returns whether the trade was accepted.
*
* @return true if the trade was accepted, false otherwise
*/
public boolean isAccepted() { return status; }
/**
* Returns the TradeHandler corresponding to the trade.
*
* @return the TradeHandler corresponding to the trade
*/
public TradeHandler getTradeHandler() {
return tradeHandler;
}

View File

@@ -26,6 +26,11 @@ public class TradeRequest extends ServerMessage{
this.tradehandler = tradehandler;
}
/**
* Returns the tradehandler.
*
* @return the tradehandler
*/
public TradeHandler getTradeHandler() { return tradehandler; }

View File

@@ -32,6 +32,11 @@ public class ViewAssetsResponse extends ServerMessage{
interpreter.received(this);
}
/**
* Returns the BoardManager representing the current Status
*
* @return the BoardManager representing the current Status
*/
public BoardManager getboard() {
return board;
}

View File

@@ -11,51 +11,52 @@ import pp.monopoly.notification.ItemAddedEvent;
import pp.monopoly.notification.ItemRemovedEvent;
/**
* Represents a rectangular map that holds figures and registers houses, hotels
* Represents a rectangular board that holds figures and registers houses, hotels
* It also supports event notification for game state changes such as item addition or removal.
* Valid positions on this map have x-coordinates in the range of 0 to width-1 and y-coordinates
* in the range of 0 to height-1.
*
* @see #getWidth()
* @see #getHeight()
*/
public class Board {
/**
* A list of items (figure, house, etc.) placed on the map.
* A list of items (figure, house, etc.) placed on the board.
*/
private final List<Item> items = new ArrayList<>();
/**
* The broker responsible for notifying registered listeners of events
* (such as when an item is added or removed from the map).
* (such as when an item is added or removed from the board).
* Can be null, in which case no notifications will be sent.
*/
private final GameEventBroker eventBroker;
/**
* The width (number of columns) of the board.
*/
private final int width;
/**
* The height (number of rows) of the board.
*/
private final int height;
/**
* Constructs an empty map with the given dimensions. The specified event broker
* will handle the notification of changes in the map state, such as adding or removing items.
* Constructs an empty board with the given dimensions. The specified event broker
* will handle the notification of changes in the board state, such as adding or removing items.
* Passing null as the event broker is allowed, but in that case, no notifications will occur.
*
* @param width the number of columns (width) of the map
* @param height the number of rows (height) of the map
* @param width the number of columns (width) of the board
* @param height the number of rows (height) of the board
* @param eventBroker the event broker used for notifying listeners, or null if event distribution is not needed
*/
public Board(int width, int height, GameEventBroker eventBroker) {
if (width < 1 || height < 1)
throw new IllegalArgumentException("Invalid map size");
throw new IllegalArgumentException("Invalid board size");
this.width = width;
this.height = height;
this.eventBroker = eventBroker;
}
/**
* Adds an item (e.g., a figure or a house) to the map and triggers the appropriate event.
* Adds an item (e.g., a figure or a house) to the board and triggers the appropriate event.
*
* @param item the item to be added to the map
* @param item the item to be added to the board
*/
private void addItem(Item item) {
items.add(item);
@@ -63,36 +64,36 @@ public class Board {
}
/**
* Adds a figure to the map and triggers an item addition event.
* Adds a figure to the board and triggers an item addition event.
*
* @param figure the figure to be added to the map
* @param figure the figure to be added to the board
*/
public void add(Figure figure) {
addItem(figure);
}
/**
* Adds a house to the map and triggers an item addition event.
* Adds a house to the board and triggers an item addition event.
*
* @param house the house to be added to the map
* @param house the house to be added to the board
*/
public void add(House house) {
addItem(house);
}
/**
* Adds a hotel to the map and triggers an item addition event.
* Adds a hotel to the board and triggers an item addition event.
*
* @param hotel the hotel to be added to the map
* @param hotel the hotel to be added to the board
*/
public void add(Hotel hotel) {
addItem(hotel);
}
/**
* Removes an item from the map and triggers an item removal event.
* Removes an item from the board and triggers an item removal event.
*
* @param item the item to be removed from the map
* @param item the item to be removed from the board
*/
public void remove(Item item) {
items.remove(item);
@@ -104,7 +105,7 @@ public class Board {
}
/**
* Removes all items from the map and triggers corresponding removal events for each.
* Removes all items from the board and triggers corresponding removal events for each.
*/
public void clear() {
new ArrayList<>(items).forEach(this::remove);
@@ -122,46 +123,62 @@ public class Board {
}
/**
* Returns a stream of all figures currently on the map.
* Returns a stream of all figures currently on the board.
*
* @return a stream of all figures on the map
* @return a stream of all figures on the board
*/
public Stream<Figure> getFigures() {
return getItems(Figure.class);
}
/**
* Returns a stream of all houses currently on the map.
* Returns a stream of all houses currently on the board.
*
* @return a stream of all houses on the map
* @return a stream of all houses on the board
*/
public Stream<House> getHouses() {
return getItems(House.class);
}
/**
* Returns the house on the specified field and stage.
* @param fieldId the id at which the house is located
* @param stage the stage of the house
* @return the house on the specified field and stage, or null if no such house exists
*/
public House getHouse(int fieldId, int stage) {
return getHouses().filter(house -> house.getFieldID() == fieldId && house.getStage() == stage).findFirst().orElse(null);
}
/**
* Returns the hotel on the specified field.
* @param fieldId the id at which the hotel is located
* @return the hotel on the specified field, or null if no such hotel exists
*/
public Hotel getHotel(int fieldId) {
return getHotels().filter(hotel -> hotel.getFieldID() == fieldId).findFirst().orElse(null);
}
/**
* Returns the figure with the specified id.
* @param playerId the id of the figure
* @return the figure with the specified id, or null if no such figure exists
*/
public Figure getFigure(int playerId) {
return getFigures().filter(figure -> figure.getId() == playerId).findFirst().orElse(null);
}
/**
* Returns a stream of all hotels currently on the map.
* Returns a stream of all hotels currently on the board.
*
* @return a stream of all hotels on the map
* @return a stream of all hotels on the board
*/
public Stream<Hotel> getHotels() {
return getItems(Hotel.class);
}
/**
* Returns an unmodifiable list of all items currently on the map.
* Returns an unmodifiable list of all items currently on the board.
*
* @return an unmodifiable list of all items
*/
@@ -170,27 +187,27 @@ public class Board {
}
/**
* Returns the width (number of columns) of the map.
* Returns the width (number of columns) of the board.
*
* @return the width of the map
* @return the width of the board
*/
public int getWidth() {
return width;
}
/**
* Returns the height (number of rows) of the map.
* Returns the height (number of rows) of the board.
*
* @return the height of the map
* @return the height of the board
*/
public int getHeight() {
return height;
}
/**
* Returns a string representation of the map.
* Returns a string representation of the board.
*
* @return a string representation of the map
* @return a string representation of the board
*/
@Override
public String toString() {

View File

@@ -10,14 +10,71 @@ import pp.monopoly.model.fields.GulagField;
import pp.monopoly.model.fields.TestStreckeField;
import pp.monopoly.model.fields.WacheField;
/**
* Interface for the visitor pattern for the fields.
* @param <T> The return type of the visit methods.
*/
public interface FieldVisitor<T> {
/**
* Visit method for the field.
* @param field The BuildingProperty to visit.
* @return The return value of the visit method.
*/
T visit(BuildingProperty field);
/**
* Visit method for the field.
* @param field The FoodField to visit.
* @return The return value of the visit method.
*/
T visit(FoodField field);
/**
* Visit method for the field.
* @param field The GateField to visit.
* @return The return value of the visit method.
*/
T visit(GateField field);
/**
* Visit method for the field.
* @param field The GulagField to visit.
* @return The return value of the visit method.
*/
T visit(GulagField field);
/**
* Visit method for the field.
* @param field The TestStreckeField to visit.
* @return The return value of the visit method.
*/
T visit(TestStreckeField field);
/**
* Visit method for the field.
* @param field The EventField to visit.
* @return The return value of the visit method.
*/
T visit(EventField field);
/**
* Visit method for the field.
* @param field The WacheField to visit.
* @return The return value of the visit method.
*/
T visit(WacheField field);
/**
* Visit method for the field.
* @param field The GoField to visit.
* @return The return value of the visit method.
*/
T visit(GoField field);
/**
* Visit method for the field.
* @param field The FineField to visit.
* @return The return value of the visit method.
*/
T visit(FineField field);
}

View File

@@ -5,13 +5,20 @@ import java.util.Random;
import com.jme3.math.Vector3f;
import com.jme3.network.serializing.Serializable;
/**
* Represents a figure that can be moved on the game board. A figure has a position and a rotation.
*/
@Serializable
public class Figure implements Item{
/** The id of the figure, corresponging to the id of the player it represents */
private final int id;
/** The type of the figure */
private final String type;
/** The position of the Figure */
private Vector3f position;
private Rotation rot; // The rotation of the Figure
/** The rotation of the Figure */
private Rotation rot;
/**
* Default constructor for serialization. Initializes a Figure with length 0,
@@ -98,6 +105,12 @@ public class Figure implements Item{
moveTo(calculateFieldPosition(fieldId));
}
/**
* Calculates the position of a field on the board.
*
* @param fieldID the ID of the field
* @return the position of the field
*/
public Vector3f calculateFieldPosition(int fieldID) {
float baseX = 0.0f;
float baseZ = 0.0f;
@@ -153,6 +166,11 @@ public class Figure implements Item{
return new Vector3f(baseX , 0, baseZ );
}
/**
* Returns the ID of the field the Figure is currently on.
*
* @return the ID of the current field
*/
public int getCurrentFieldID() {
Vector3f pos = getPos();
for (int fieldID = 0; fieldID < 40; fieldID++) {
@@ -206,17 +224,15 @@ public class Figure implements Item{
return visitor.visit(this);
}
/**
* Accepts a visitor that does not return a value. This method is part of the
* Visitor design pattern.
*
* @param visitor the visitor to accept
*/
@Override
public void accept(VoidVisitor visitor) {
visitor.visit(this);
}
/**
* Returns the type of the figure
* @return the type of the figure
*/
public String getType() {
return type;
}

View File

@@ -15,15 +15,27 @@ import com.jme3.network.serializing.Serializable;
@Serializable
public class TradeHandler {
/** The player initialing the trade */
private final Player sender;
/** The player receiving the trade offer */
private Player receiver;
/** The amount of money offered by the sender */
private int offeredAmount;
/** The properties offered by the sender */
private Set<PropertyField> offeredProperties = new HashSet<>();
/** The jail cards offered by the sender */
private int offeredJailCards;
/** The amount of money requested from the receiver */
private int requestedAmount;
/** The properties requested from the receiver */
private Set<PropertyField> requestedProperties = new HashSet<>();
/** The jail cards requested from the receiver */
private int requestedJailCards;
/**
* Constructs a TradeHandler with no trade details.
* Used for serialization.
*/
private TradeHandler() {
sender = null;
}
@@ -52,7 +64,7 @@ public class TradeHandler {
this.requestedJailCards = requestedJailCards;
}
/**
/**
* Constructs a TradeHandler for a single trade instance.
*
* @param sender the Player initiating the trade
@@ -61,62 +73,122 @@ public class TradeHandler {
this.sender = sender;
}
/**
* Returns the offered amount of money.
* @return the offered amount of money
*/
public int getOfferedAmount() {
return offeredAmount;
}
/**
* Returns the offered amount of jail cards.
* @return the offered amount of jail cards
*/
public int getOfferedJailCards() {
return offeredJailCards;
}
/**
* Returns the offered properties.
* @return the offered properties
*/
public Set<PropertyField> getOfferedProperties() {
return offeredProperties;
}
/**
* Returns the receiving player.
* @return the receiving player
*/
public Player getReceiver() {
return receiver;
}
/**
* Returns the requested amount of money.
* @return the requested amount of money
*/
public int getRequestedAmount() {
return requestedAmount;
}
/**
* Returns the requested amount of jail cards.
* @return the requested amount of jail cards
*/
public int getRequestedJailCards() {
return requestedJailCards;
}
/**
* Returns the requested properties.
* @return the requested properties
*/
public Set<PropertyField> getRequestedProperties() {
return requestedProperties;
}
/**
* Returns the sending player.
* @return the sending player
*/
public Player getSender() {
return sender;
}
/**
* Sets the offered amount of money.
* @param offeredAmount the offered amount of money
*/
public void setOfferedAmount(int offeredAmount) {
this.offeredAmount = offeredAmount;
}
/**
* Sets the offered amount of jail cards.
* @param offeredJailCards the offered amount of jail cards
*/
public void setOfferedJailCards(int offeredJailCards) {
this.offeredJailCards = offeredJailCards;
}
/**
* Sets the offered properties.
* @param offeredProperties the offered properties
*/
public void setOfferedProperties(Set<PropertyField> offeredProperties) {
this.offeredProperties = offeredProperties;
}
/**
* Sets the receiving player.
* @param receiver the receiving player
*/
public void setReceiver(Player receiver) {
this.receiver = receiver;
}
/**
* Sets the requested amount of money.
* @param requestedAmount the requested amount of money
*/
public void setRequestedAmount(int requestedAmount) {
this.requestedAmount = requestedAmount;
}
/**
* Sets the requested amount of jail cards.
* @param requestedJailCards the requested amount of jail cards
*/
public void setRequestedJailCards(int requestedJailCards) {
this.requestedJailCards = requestedJailCards;
}
/**
* Sets the requested properties.
* @param requestedProperties the requested properties
*/
public void setRequestedProperties(Set<PropertyField> requestedProperties) {
this.requestedProperties = requestedProperties;
}

View File

@@ -2,23 +2,50 @@ package pp.monopoly.model.card;
import pp.monopoly.game.server.Player;
/**
* Represents a event card in the game.
*/
public class Card {
/** Description of the card */
private final String description;
/** Keyword of the card */
private final String keyword;
/**
* Constructs a card with a description and a keyword.
*
* @param description the description of the card
* @param keyword the keyword of the card
*/
Card(String description, String keyword) {
this.description = description;
this.keyword = keyword;
}
/**
* Accepts a visitor and calls the visit method of the visitor.
*
* @param visitor the visitor
* @param player the player
*/
public void accept(DeckHelper visitor, Player player) {
visitor.visit(this, player);
}
/**
* Returns the description of the card.
*
* @return the description of the card
*/
public String getDescription() {
return description;
} // TODO wird gerade in der EventCard zur erstellung des Popup genutzt
}
/**
* Returns the keyword of the card.
*
* @return the keyword of the card
*/
public String getKeyword() {
return keyword;
}

View File

@@ -10,11 +10,19 @@ import pp.monopoly.game.server.Player;
import pp.monopoly.message.client.EndTurn;
import pp.monopoly.message.server.NotificationMessage;
/**
* Helper class for the deck of cards.
*/
public class DeckHelper{
/** The deck of cards */
private Queue<Card> cards;
/** The drawn cards */
private List<Card> drawn = new ArrayList<>();
/**
* Constructor for the deck helper.
*/
public DeckHelper() {
cards = new LinkedList<Card>();
cards.add(new Card("Du wurdest mit einem Dienst KFZ geblitzt. Zahle 800 EUR", "dienst-kfz-blitzer"));
@@ -53,6 +61,11 @@ public class DeckHelper{
shuffle();
}
/**
* Triggers the specified method for each card action
* @param card the card to be triggered
* @param player the player triggering it
*/
public void visit(Card card, Player player) {
switch (card.getKeyword()) {
case "dienst-kfz-blitzer":
@@ -320,6 +333,9 @@ public class DeckHelper{
}
/**
* Shuffles the deck of cards.
*/
private void shuffle() {
List<Card> cardList = new ArrayList<>(cards);
Collections.shuffle(cardList);
@@ -327,6 +343,10 @@ public class DeckHelper{
cards.addAll(cardList);
}
/**
* Draws a card from the deck.
* @return the drawn card
*/
public Card drawCard() {
if (cards.isEmpty()) {
drawn.forEach(cards::add);

View File

@@ -1,11 +1,12 @@
package pp.monopoly.notification;
/**
* Event that is sent when the buttons should be enabled or disabled.
*
* @param buttonsEnabled true if the buttons should be enabled, false if they should be disabled
*/
public record ButtonStatusEvent(boolean buttonsEnabled) implements GameEvent{
/**
* Notifies the game event listener of this event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);

View File

@@ -1,5 +1,11 @@
package pp.monopoly.notification;
/**
* Event that is sent when the dice are rolled.
*
* @param a the first number of the dice roll
* @param b the second number of the dice roll
*/
public record DiceRollEvent(int a, int b) implements GameEvent{
@Override

View File

@@ -1,5 +1,10 @@
package pp.monopoly.notification;
/**
* EventCardEvent is a class that represents an event that is sent to the GameEventListener when a player draws an event card.
*
* @param description the description of the card that was drawn
*/
public record EventCardEvent(String description) implements GameEvent{
@Override

View File

@@ -13,11 +13,7 @@ package pp.monopoly.notification;
* @param key the bundle key for the message
*/
public record InfoTextEvent(String key) implements GameEvent {
/**
* Notifies the game event listener of this event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);

View File

@@ -5,6 +5,9 @@ import pp.monopoly.model.Item;
/**
* Event that is triggered when an item is added to a board.
*
* @param board the board to which the item was added
* @param item the item that was added
*/
public record ItemAddedEvent(Board board, Item item) implements GameEvent {

View File

@@ -5,6 +5,9 @@ import pp.monopoly.model.Item;
/**
* Event that is triggered when an item is removed from a board.
*
* @param board the board from which the item was removed
* @param item the item that was removed
*/
public record ItemRemovedEvent(Board board, Item item) implements GameEvent {

View File

@@ -2,6 +2,12 @@ package pp.monopoly.notification;
import pp.monopoly.message.server.ServerMessage;
/**
* Event that is used to show a popup message to the user.
*
* @param msg a keyword to indicate the type of message
* @param message the message that should be shown
*/
public record PopUpEvent(String msg, ServerMessage message) implements GameEvent{
@Override

View File

@@ -14,11 +14,6 @@ package pp.monopoly.notification;
*/
public record SoundEvent(Sound sound) implements GameEvent {
/**
* Notifies the game event listener of this event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);

View File

@@ -1,11 +1,10 @@
package pp.monopoly.notification;
/**
* This class is used to notify the view that the player's information has been updated.
*/
public record UpdatePlayerView() implements GameEvent{
/**
* Notifies the game event listener of this event.
*
* @param listener the game event listener
*/
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);