From 041a5f135a548b01e2c5b0367753382e16d7ae89 Mon Sep 17 00:00:00 2001 From: Julian Kropff Date: Sat, 4 Apr 2026 23:34:01 +0200 Subject: [PATCH] Add: player actions for game engine --- .../domain/game/action/AbstractAction.java | 32 ++++++++ .../server/domain/game/action/Action.java | 18 +++++ .../server/domain/game/action/ActionType.java | 16 ++++ .../domain/game/action/AllInAction.java | 51 ++++++++++++ .../server/domain/game/action/BetAction.java | 65 +++++++++++++++ .../domain/game/action/BlindAction.java | 81 +++++++++++++++++++ .../server/domain/game/action/CallAction.java | 66 +++++++++++++++ .../server/domain/game/action/FoldAction.java | 45 +++++++++++ .../domain/game/action/RaiseAction.java | 66 +++++++++++++++ 9 files changed, 440 insertions(+) create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AbstractAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/Action.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/ActionType.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AllInAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BetAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BlindAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/CallAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/FoldAction.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/RaiseAction.java diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AbstractAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AbstractAction.java new file mode 100644 index 0000000..b73e651 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AbstractAction.java @@ -0,0 +1,32 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; + +/** + * AbstractAction serves as a base class for all player actions in the poker + * game. + * It implements the Action interface and provides a common implementation for + * retrieving the player ID associated with the action. + */ +public abstract class AbstractAction implements Action { + protected final PlayerId playerId; + + /** + * Constructs an AbstractAction with the specified player ID. + * + * @param playerId The ID of the player performing the action. + */ + public AbstractAction(PlayerId playerId) { + this.playerId = playerId; + } + + /** + * Retrieves the ID of the player performing the action. + * + * @return The player ID associated with this action. + */ + @Override + public PlayerId getPlayerId() { + return playerId; + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/Action.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/Action.java new file mode 100644 index 0000000..078146a --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/Action.java @@ -0,0 +1,18 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState;; + +/** + * The Action interface defines the structure for all player actions in the + * poker game. Each action must specify its type, provide an implementation for + * executing the action on the game state, and identify the player performing + * the action. + */ +public interface Action { + ActionType getType(); + + void execute(GameState state); + + PlayerId getPlayerId(); +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/ActionType.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/ActionType.java new file mode 100644 index 0000000..9913785 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/ActionType.java @@ -0,0 +1,16 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +/** + * ActionType is an enumeration that defines the various types of actions that + * players can perform in a poker game. Each action type corresponds to a + * specific move or decision that a player can make during their turn. + */ +public enum ActionType { + FOLD, + CALL, + RAISE, + CHECK, + BET, + ALL_IN, + BLIND +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AllInAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AllInAction.java new file mode 100644 index 0000000..0a0a55d --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/AllInAction.java @@ -0,0 +1,51 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +/** + * AllInAction represents the action of a player going all-in in a poker game. + * When + * a player goes all-in, they bet all of their remaining chips. This action + * updates the player's chip count, adds the bet to the pot, and updates the + * current bet for the player in the game state. + */ +public class AllInAction extends AbstractAction { + + /** + * Constructs an AllInAction for the specified player ID. + * + * @param playerId The ID of the player performing the all-in action. + */ + public AllInAction(PlayerId playerId) { + super(playerId); + } + + /** + * Retrieves the type of this action, which is ALL_IN. + * + * @return The ActionType corresponding to this action. + */ + @Override + public ActionType getType() { + return ActionType.ALL_IN; + } + + /** + * Executes the all-in action on the given game state. This method updates the + * player's chip count, adds the bet to the pot, and updates the current bet + * for the player in the game state. + * + * @param state The current game state on which to execute the action. + */ + @Override + public void execute(GameState state) { + Player player = state.getPlayer(playerId); + int chips = player.getChips(); + + player.removeChips(chips); + state.addToPot(chips); + state.setCurrentBet(playerId, state.getCurrentBet(playerId) + chips); + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BetAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BetAction.java new file mode 100644 index 0000000..2f47321 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BetAction.java @@ -0,0 +1,65 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +/** + * Represents a bet action where a player contributes a fixed amount of chips. + * The amount is deducted from the player's chips, added to the pot, and both + * the player's current bet and the table's current bet are set to this amount. + */ +public class BetAction extends AbstractAction { + + private final int amount; + + /** + * Constructs a BetAction for the specified player ID and bet amount. + * + * @param playerId The ID of the player performing the bet action. + * @param amount The amount of chips the player is betting. + */ + public BetAction(PlayerId playerId, int amount) { + super(playerId); + this.amount = amount; + } + + /** + * Retrieves the amount of chips being bet in this action. + * + * @return The amount of chips being bet. + */ + public int getAmount() { + return amount; + } + + /** + * Retrieves the type of this action, which is BET. + * + * @return The ActionType corresponding to this action. + */ + @Override + public ActionType getType() { + return ActionType.BET; + } + + /** + * Executes the bet action on the given game state. This method updates the + * player's chip count, adds the bet to the pot, and updates the current bet + * for the player in the game state. + * + * @param state The current game state on which to execute the action. + */ + @Override + public void execute(GameState state) { + + Player player = state.getPlayer(playerId); + + player.removeChips(amount); + + state.addToPot(amount); + state.setCurrentBet(playerId, amount); + + state.getTableState().setCurrentBet(amount); + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BlindAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BlindAction.java new file mode 100644 index 0000000..a82b369 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/BlindAction.java @@ -0,0 +1,81 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +/** + * Executes a blind by deducting chips from the player, adding them to the pot, + * increasing the player's current bet, and updating the table's current bet + * if the blind exceeds the existing bet. + */ +public class BlindAction implements Action { + + private final PlayerId playerId; + private final int amount; + + /** + * Constructs a BlindAction for the specified player ID and blind amount. + * + * @param playerId The ID of the player posting the blind bet. + * @param amount The amount of chips the player is posting as a blind bet. + */ + public BlindAction(PlayerId playerId, int amount) { + this.playerId = playerId; + this.amount = amount; + } + + /** + * Retrieves the type of this action, which is BLIND. + * + * @return The ActionType corresponding to this action. + */ + @Override + public ActionType getType() { + return ActionType.BLIND; + } + + /** + * Retrieves the ID of the player performing the action. + * + * @return The player ID associated with this action. + */ + @Override + public PlayerId getPlayerId() { + return playerId; + } + + /** + * Executes the blind action on the given game state. This method updates the + * player's chip count, adds the blind bet to the pot, and updates the current + * bet for the player in the game state. + * + * @param state The current game state on which to execute the action. + */ + @Override + public void execute(GameState state) { + + Player player = state.getPlayer(playerId); + + player.removeChips(amount); + + state.addToPot(amount); + + int alreadyPaid = state.getCurrentBet(playerId); + + state.setCurrentBet(playerId, alreadyPaid + amount); + + if (state.getTableState().getCurrentBet() < amount) { + state.getTableState().setCurrentBet(amount); + } + } + + /** + * Retrieves the amount of chips being posted as a blind bet in this action. + * + * @return The amount of chips being posted as a blind bet. + */ + public int getAmount() { + return amount; + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/CallAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/CallAction.java new file mode 100644 index 0000000..3d71b59 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/CallAction.java @@ -0,0 +1,66 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +/** + * CallAction represents the action of a player calling in a poker game. When a + * player calls, they match the current bet on the table by paying the + * difference between their current bet and the table's current bet. This action + * updates the player's chip count, adds the required amount to the pot, and + * updates the player's current bet accordingly. + */ +public class CallAction extends AbstractAction { + + /** + * Constructs a CallAction for the specified player. + * + * @param playerId the ID of the player performing the call action + */ + public CallAction(PlayerId playerId) { + super(playerId); + } + + /** + * Returns the type of this action, which is CALL. + * + * @return the ActionType representing a call action + */ + @Override + public ActionType getType() { + return ActionType.CALL; + } + + /** + * Executes the call action on the given game state. This method checks if + * the player is folded, calculates the amount to call, updates the player's + * chip count, adds the amount to the pot, and updates the player's current + * bet. + * + * @param state the current game state on which to execute the call action + */ + @Override + public void execute(GameState state) { + + Player player = state.getPlayer(playerId); + + if (player.isFolded()) { + return; + } + + int tableBet = state.getTableState().getCurrentBet(); + int playerBet = state.getCurrentBet(playerId); + + int toCall = tableBet - playerBet; + + if (toCall <= 0) { + return; + } + + player.removeChips(toCall); + state.addToPot(toCall); + + state.setCurrentBet(playerId, playerBet + toCall); + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/FoldAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/FoldAction.java new file mode 100644 index 0000000..e49c66a --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/FoldAction.java @@ -0,0 +1,45 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +/** + * FoldAction represents the action of a player folding in a poker game. When a + * player folds, they forfeit their hand and are no longer active in the current + * round. This action updates the player's status to indicate that they have + * folded. + */ +public class FoldAction extends AbstractAction { + + /** + * Constructs a FoldAction for the specified player ID. + * + * @param playerId The ID of the player performing the fold action. + */ + public FoldAction(PlayerId playerId) { + super(playerId); + } + + /** + * Retrieves the type of this action, which is FOLD. + * + * @return The ActionType corresponding to this action. + */ + @Override + public ActionType getType() { + return ActionType.FOLD; + } + + /** + * Executes the fold action on the given game state. This method updates the + * player's status to indicate that they have folded. + * + * @param state The current game state on which to execute the action. + */ + @Override + public void execute(GameState state) { + Player player = state.getPlayer(playerId); + player.setFolded(true); + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/RaiseAction.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/RaiseAction.java new file mode 100644 index 0000000..ceb94e1 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/server/domain/game/action/RaiseAction.java @@ -0,0 +1,66 @@ +package ch.unibas.dmi.dbis.cs108.casono.server.domain.game.action; + +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.Player; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.player.PlayerId; +import ch.unibas.dmi.dbis.cs108.casono.server.domain.game.state.GameState; + +public class RaiseAction extends AbstractAction { + + private final int raiseAmount; + + /** + * Constructs a RaiseAction for the specified player ID and raise amount. + * + * @param playerId The ID of the player performing the raise action. + * @param raiseAmount The amount of chips the player is raising. + */ + public RaiseAction(PlayerId playerId, int raiseAmount) { + super(playerId); + this.raiseAmount = raiseAmount; + } + + /** + * Retrieves the amount of chips being raised in this action. + * + * @return The amount of chips being raised. + */ + public int getAmount() { + return raiseAmount; + } + + /** + * Retrieves the type of this action, which is RAISE. + * + * @return The ActionType corresponding to this action. + */ + @Override + public ActionType getType() { + return ActionType.RAISE; + } + + /** + * Executes the raise action on the given game state. This method updates the + * player's chip count, adds the raise amount to the pot, and updates the + * current bet for the player in the game state. + * + * @param state The current game state on which to execute the action. + */ + @Override + public void execute(GameState state) { + + Player player = state.getPlayer(playerId); + + int alreadyPaid = state.getCurrentBet(playerId); + int toCall = state.getTableState().getCurrentBet() - alreadyPaid; + + int total = toCall + raiseAmount; + + player.removeChips(total); + state.addToPot(total); + + int newBet = state.getTableState().getCurrentBet() + raiseAmount; + + state.getTableState().setCurrentBet(newBet); + state.setCurrentBet(playerId, alreadyPaid + total); + } +}