Feat/Game Engine #233

Merged
j.kropff merged 18 commits from feat/game-engine into main 2026-04-05 11:26:12 +02:00
9 changed files with 440 additions and 0 deletions
Showing only changes of commit 041a5f135a - Show all commits
@@ -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;
}
}
@@ -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();
}
@@ -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
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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;
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}