Fix: improve round management, betting validation and game engine test coverage #290

Merged
j.kropff merged 13 commits from fix/game-turn-and-action-bugs into main 2026-04-19 20:31:45 +02:00
5 changed files with 217 additions and 233 deletions
Showing only changes of commit ab2adbf311 - Show all commits
@@ -56,6 +56,7 @@ public class TaskbarController {
private static final String STYLE_GRAY_INPUT = "gray-input-field"; private static final String STYLE_GRAY_INPUT = "gray-input-field";
private static final String STYLE_RED_INPUT = "red-input-field"; private static final String STYLE_RED_INPUT = "red-input-field";
private static final double SCALE_NORMAL = 1.0; private static final double SCALE_NORMAL = 1.0;
private static final int DEALER_OFFSET = 3;
private boolean inputActionAllowed; private boolean inputActionAllowed;
private int lastReferenceBet = BIG_BLIND; private int lastReferenceBet = BIG_BLIND;
@@ -97,7 +98,8 @@ public class TaskbarController {
} }
/** /**
* Sets the given button to a disabled state with red styling, indicating that the player is out. * Sets the given button to a disabled state with red styling, indicating that the player is
* out.
* *
* @param b The button to be styled as red and disabled. * @param b The button to be styled as red and disabled.
*/ */
@@ -217,8 +219,9 @@ public class TaskbarController {
public void initialize() { public void initialize() {
updateBasicButtons(false, false); updateBasicButtons(false, false);
if (taskbarInput != null) { if (taskbarInput != null) {
taskbarInput.textProperty().addListener((obs, oldValue, newValue) taskbarInput
-> refreshBetInputUi()); .textProperty()
.addListener((obs, oldValue, newValue) -> refreshBetInputUi());
} }
setBetButtonVisible(false); setBetButtonVisible(false);
@@ -228,12 +231,12 @@ public class TaskbarController {
* Updates the taskbar based on the current game state and the player's status. * Updates the taskbar based on the current game state and the player's status.
* *
* @param state The current GameState object representing the state of the game, which includes * @param state The current GameState object representing the state of the game, which includes
* information about the players, their bets, the current phase and other relevant * information about the players, their bets, the current phase and other relevant details
* details needed to update the taskbar UI accurately based on the player's status * needed to update the taskbar UI accurately based on the player's status and the game
* and the game context. * context.
* @param myPlayerId The PlayerId object representing the current player, used to identify the * @param myPlayerId The PlayerId object representing the current player, used to identify the
* player's status and update the taskbar UI accordingly based on whether * player's status and update the taskbar UI accordingly based on whether it is their turn
* it is their turn and whether they are out of the game. * and whether they are out of the game.
*/ */
public void update(GameState state, PlayerId myPlayerId) { public void update(GameState state, PlayerId myPlayerId) {
@@ -278,8 +281,8 @@ public class TaskbarController {
* and bet status. * and bet status.
* @param isMyTurn Indicates whether it is currently the player's turn, which affects whether * @param isMyTurn Indicates whether it is currently the player's turn, which affects whether
* actions can be taken. * actions can be taken.
* @param isOutOrFinished Indicates whether the player is out of the game (folded or all-in) * @param isOutOrFinished Indicates whether the player is out of the game (folded or all-in) or
* or if the game is finished, which disables actions. * if the game is finished, which disables actions.
*/ */
private void applyActionAvailability( private void applyActionAvailability(
GameState state, Player me, boolean isMyTurn, boolean isOutOrFinished) { GameState state, Player me, boolean isMyTurn, boolean isOutOrFinished) {
@@ -338,8 +341,8 @@ public class TaskbarController {
* Safely doubles the given value while preventing integer overflow. * Safely doubles the given value while preventing integer overflow.
* *
* @param value The integer value to be safely doubled. * @param value The integer value to be safely doubled.
* @return The safely doubled value, or 0 if the input is non-positive, or Integer.MAX_VALUE * @return The safely doubled value, or 0 if the input is non-positive, or Integer.MAX_VALUE if
* if doubling would overflow. * doubling would overflow.
*/ */
private int safeDouble(int value) { private int safeDouble(int value) {
if (value <= 0) { if (value <= 0) {
@@ -354,8 +357,8 @@ public class TaskbarController {
/** /**
* Sets the visibility of the Bet button in the taskbar. * Sets the visibility of the Bet button in the taskbar.
* *
* @param visible A boolean indicating whether the Bet button should be visible (true) * @param visible A boolean indicating whether the Bet button should be visible (true) or hidden
* or hidden (false). * (false).
*/ */
private void setBetButtonVisible(boolean visible) { private void setBetButtonVisible(boolean visible) {
if (betButton == null) { if (betButton == null) {
@@ -366,8 +369,8 @@ public class TaskbarController {
} }
/** /**
* Refreshes the user interface of the bet input field and the Bet button based on * Refreshes the user interface of the bet input field and the Bet button based on the current
* the current game state and the validity of the input. * game state and the validity of the input.
*/ */
private void refreshBetInputUi() { private void refreshBetInputUi() {
if (!inputActionAllowed || taskbarInput == null || taskbarInput.isDisabled()) { if (!inputActionAllowed || taskbarInput == null || taskbarInput.isDisabled()) {
@@ -389,9 +392,10 @@ public class TaskbarController {
/** /**
* Enables or disables the given button based on the provided boolean value. * Enables or disables the given button based on the provided boolean value.
* *
* @param button The Button object to be enabled or disabled based on the provided boolean value. * @param button The Button object to be enabled or disabled based on the provided boolean
* @param enabled A boolean value indicating whether the button should be enabled (true) * value.
* or disabled (false). * @param enabled A boolean value indicating whether the button should be enabled (true) or
* disabled (false).
*/ */
private void setActionEnabled(Button button, boolean enabled) { private void setActionEnabled(Button button, boolean enabled) {
if (enabled) { if (enabled) {
@@ -404,10 +408,10 @@ public class TaskbarController {
/** /**
* Calculates the amount needed to call the current bet in the game. * Calculates the amount needed to call the current bet in the game.
* *
* @param state The current GameState object representing the state of the game, which * @param state The current GameState object representing the state of the game, which includes
* includes information about the current bet and the player's bet status. * information about the current bet and the player's bet status.
* @param me The Player object representing the current player, used to determine how much * @param me The Player object representing the current player, used to determine how much they
* they have already invested in the current bet. * have already invested in the current bet.
* @return An integer representing the amount needed for the player to call the current bet. * @return An integer representing the amount needed for the player to call the current bet.
*/ */
private int getToCall(GameState state, Player me) { private int getToCall(GameState state, Player me) {
@@ -417,11 +421,11 @@ public class TaskbarController {
} }
/** /**
* Determines the effective call target for the current game state. If there is an active current * Determines the effective call target for the current game state. If there is an active
* bet, it returns that as the call target. * current bet, it returns that as the call target.
* *
* @param state The current GameState object representing the state of the game, which * @param state The current GameState object representing the state of the game, which includes
* includes information about the current bet and the last reference bet. * information about the current bet and the last reference bet.
* @return An integer representing the effective call target for the current game state. * @return An integer representing the effective call target for the current game state.
*/ */
private int effectiveCallTarget(GameState state) { private int effectiveCallTarget(GameState state) {
@@ -432,16 +436,16 @@ public class TaskbarController {
} }
/** /**
* Checks if the current player is the first to act in the pre-flop phase * Checks if the current player is the first to act in the pre-flop phase and if the initial
* and if the initial blind layout is still in place. * blind layout is still in place.
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes information about the phase of the game, the players, * information about the phase of the game, the players, the dealer position, and the active
* the dealer position, and the active player. * player.
* @param me The Player object representing the current player, used to determine * @param me The Player object representing the current player, used to determine their position
* their position in the player list and whether they are the active player. * in the player list and whether they are the active player.
* @return A boolean value indicating whether the current player is the first to act * @return A boolean value indicating whether the current player is the first to act in the
* in the pre-flop phase with only the initial blind layout in place. * pre-flop phase with only the initial blind layout in place.
*/ */
private boolean isFirstPreflopPlayerInputOnly(GameState state, Player me) { private boolean isFirstPreflopPlayerInputOnly(GameState state, Player me) {
if (state == null || me == null || !isPreflop(state.phase) || state.players == null) { if (state == null || me == null || !isPreflop(state.phase) || state.players == null) {
@@ -459,7 +463,7 @@ public class TaskbarController {
} }
int dealer = state.dealer; int dealer = state.dealer;
int firstIndex = (size == 2) ? dealer : (dealer + 3) % size; int firstIndex = (size == 2) ? dealer : (dealer + DEALER_OFFSET) % size;
if (state.activePlayer != firstIndex || myIndex != firstIndex) { if (state.activePlayer != firstIndex || myIndex != firstIndex) {
return false; return false;
} }
@@ -470,10 +474,10 @@ public class TaskbarController {
/** /**
* Checks if the initial blind layout is still in place during the pre-flop phase. * Checks if the initial blind layout is still in place during the pre-flop phase.
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes information about the players and their bets. * information about the players and their bets.
* @return A boolean value indicating whether the initial blind layout is still in * @return A boolean value indicating whether the initial blind layout is still in place during
* place during the pre-flop phase. * the pre-flop phase.
*/ */
private boolean isInitialPreflopBlindLayout(GameState state) { private boolean isInitialPreflopBlindLayout(GameState state) {
int sbCount = 0; int sbCount = 0;
@@ -502,10 +506,10 @@ public class TaskbarController {
/** /**
* Checks if the given phase string corresponds to the pre-flop phase of the game. * Checks if the given phase string corresponds to the pre-flop phase of the game.
* *
* @param phase The string representing the current phase of the game, which is expected * @param phase The string representing the current phase of the game, which is expected to be
* to be compared against "PREFLOP" to determine if it is the pre-flop phase. * compared against "PREFLOP" to determine if it is the pre-flop phase.
* @return A boolean value indicating whether the given phase string corresponds to the * @return A boolean value indicating whether the given phase string corresponds to the pre-flop
* pre-flop phase. * phase.
*/ */
private boolean isPreflop(String phase) { private boolean isPreflop(String phase) {
return "PREFLOP".equalsIgnoreCase(phase); return "PREFLOP".equalsIgnoreCase(phase);
@@ -514,9 +518,10 @@ public class TaskbarController {
/** /**
* Checks if the given phase string corresponds to the flop phase of the game. * Checks if the given phase string corresponds to the flop phase of the game.
* *
* @param phase The string representing the current phase of the game, which is * @param phase The string representing the current phase of the game, which is expected to be
* expected to be compared against "FLOP" to determine if it is the flop phase. * compared against "FLOP" to determine if it is the flop phase.
* @return A boolean value indicating whether the given phase string corresponds to the flop phase. * @return A boolean value indicating whether the given phase string corresponds to the flop
* phase.
*/ */
private boolean isFlop(String phase) { private boolean isFlop(String phase) {
return "FLOP".equalsIgnoreCase(phase); return "FLOP".equalsIgnoreCase(phase);
@@ -525,11 +530,10 @@ public class TaskbarController {
/** /**
* Checks if the given phase string corresponds to either the turn or river phase of the game. * Checks if the given phase string corresponds to either the turn or river phase of the game.
* *
* @param phase The string representing the current phase of the game, which is expected to * @param phase The string representing the current phase of the game, which is expected to be
* be compared against "TURN" and "RIVER" to determine if it is either the turn * compared against "TURN" and "RIVER" to determine if it is either the turn or river phase.
* or river phase. * @return A boolean value indicating whether the given phase string corresponds to either the
* @return A boolean value indicating whether the given phase string corresponds to either * turn or river phase.
* the turn or river phase.
*/ */
private boolean isTurnOrRiver(String phase) { private boolean isTurnOrRiver(String phase) {
return "TURN".equalsIgnoreCase(phase) || "RIVER".equalsIgnoreCase(phase); return "TURN".equalsIgnoreCase(phase) || "RIVER".equalsIgnoreCase(phase);
@@ -538,7 +542,8 @@ public class TaskbarController {
/** /**
* Checks if the hand is finished based on the current game state. * Checks if the hand is finished based on the current game state.
* *
* @param state The current GameState object representing the state of the game, which includes information * @param state The current GameState object representing the state of the game, which includes
* information
* @return A boolean value indicating whether the hand is finished, which can be determined by * @return A boolean value indicating whether the hand is finished, which can be determined by
* checking if the phase is "FINISHED" or "SHOWDOWN", * checking if the phase is "FINISHED" or "SHOWDOWN",
*/ */
@@ -547,8 +552,7 @@ public class TaskbarController {
return false; return false;
} }
if ("FINISHED".equalsIgnoreCase(state.phase) if ("FINISHED".equalsIgnoreCase(state.phase) || "SHOWDOWN".equalsIgnoreCase(state.phase)) {
|| "SHOWDOWN".equalsIgnoreCase(state.phase)) {
return true; return true;
} }
@@ -558,8 +562,8 @@ public class TaskbarController {
} }
/** /**
* Evaluates the risk level of a proposed bet based on the current game state, the amount of * Evaluates the risk level of a proposed bet based on the current game state, the amount of the
* the bet and the player's available chips. * bet and the player's available chips.
*/ */
private enum BetRisk { private enum BetRisk {
ALLOWED, ALLOWED,
@@ -567,18 +571,14 @@ public class TaskbarController {
BLOCKED BLOCKED
} }
/** /** Enum representing the types of actions that can be submitted from the taskbar. */
* Enum representing the types of actions that can be submitted from the taskbar.
*/
private enum ActionType { private enum ActionType {
INPUT, INPUT,
CALL_BUTTON, CALL_BUTTON,
RAISE_BUTTON RAISE_BUTTON
} }
/** /** Class representing the result of validating a proposed bet or action. */
* Class representing the result of validating a proposed bet or action.
*/
private static final class ValidationResult { private static final class ValidationResult {
private final boolean valid; private final boolean valid;
private final boolean warning; private final boolean warning;
@@ -607,8 +607,7 @@ public class TaskbarController {
* Submits a player action based on the specified ActionType. * Submits a player action based on the specified ActionType.
* *
* @param actionType The type of action being submitted, which can be an input-based action * @param actionType The type of action being submitted, which can be an input-based action
* (where the player types an amount) or a specific button action for * (where the player types an amount) or a specific button action for calling or raising.
* calling or raising.
*/ */
private void submitAction(ActionType actionType) { private void submitAction(ActionType actionType) {
if (gameService == null) { if (gameService == null) {
@@ -678,11 +677,11 @@ public class TaskbarController {
} }
/** /**
* Ensures that the latest game state is available for processing a player action. * Ensures that the latest game state is available for processing a player action. If the last
* If the last known state is null, it attempts to fetch the current state from the GameService. * known state is null, it attempts to fetch the current state from the GameService.
* *
* @param text The text associated with the action being processed, used for logging purposes * @param text The text associated with the action being processed, used for logging purposes to
* to indicate which action is being attempted when the state is not available. * indicate which action is being attempted when the state is not available.
* @return The latest GameState object if available, or null if the state cannot be retrieved, * @return The latest GameState object if available, or null if the state cannot be retrieved,
* indicating that the action cannot be processed. * indicating that the action cannot be processed.
*/ */
@@ -700,14 +699,13 @@ public class TaskbarController {
/** /**
* Finds the current player in the given game state based on the player's ID. * Finds the current player in the given game state based on the player's ID.
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes a list of players. * a list of players.
* @param text The text associated with the action being processed, used for logging * @param text The text associated with the action being processed, used for logging purposes to
* purposes to indicate which action is being attempted when the player * indicate which action is being attempted when the player cannot be found in the state.
* cannot be found in the state. * @return The Player object representing the current player if found in the game state, or null
* @return The Player object representing the current player if found in the game state, * if no matching player is found, indicating that the player cannot be identified in the
* or null if no matching player is found, indicating that the player cannot be identified * current game state.
* in the current game state.
*/ */
private ValidationResult validateTypedAmount(GameState state, String text) { private ValidationResult validateTypedAmount(GameState state, String text) {
Integer target = parseInputTarget(text); Integer target = parseInputTarget(text);
@@ -722,20 +720,19 @@ public class TaskbarController {
} }
/** /**
* Validates a proposed target bet against the current game state and the player's status. * Validates a proposed target bet against the current game state and the player's status. It
* It checks if the target bet is either a valid call or a valid raise * checks if the target bet is either a valid call or a valid raise (specifically, exactly
* (specifically, exactly double the call target). * double the call target).
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes information about the current bet, the phase of the game, * information about the current bet, the phase of the game, and the players.
* and the players. * @param me The Player object representing the current player, used to determine their current
* @param me The Player object representing the current player, used to determine their * bet, available chips, and to evaluate whether the proposed target bet is valid for this
* current bet, available chips, and to evaluate whether the proposed target * player based on their status in the game.
* bet is valid for this player based on their status in the game. * @param targetBet The integer value representing the proposed target bet that the player
* @param targetBet The integer value representing the proposed target bet that the * intends to make.
* player intends to make. * @return A ValidationResult object indicating whether the proposed target bet is valid, if it
* @return A ValidationResult object indicating whether the proposed target bet is valid, * triggers a warning, or if it is blocked due to being invalid or too risky.
* if it triggers a warning, or if it is blocked due to being invalid or too risky.
*/ */
private ValidationResult validateTarget(GameState state, Player me, int targetBet) { private ValidationResult validateTarget(GameState state, Player me, int targetBet) {
if (state == null || me == null) { if (state == null || me == null) {
@@ -777,11 +774,10 @@ public class TaskbarController {
/** /**
* Generates a warning message based on the current phase of the game. * Generates a warning message based on the current phase of the game.
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes information about the phase of the game. * information about the phase of the game.
* @return A string containing the warning message appropriate for the current phase * @return A string containing the warning message appropriate for the current phase of the game
* of the game when a player's bet triggers a warning due to being at least * when a player's bet triggers a warning due to being at least 50% of their stack.
* 50% of their stack.
*/ */
private String warningTextForPhase(GameState state) { private String warningTextForPhase(GameState state) {
String phase = state != null ? state.phase : null; String phase = state != null ? state.phase : null;
@@ -795,13 +791,13 @@ public class TaskbarController {
} }
/** /**
* Evaluates the risk level of a proposed bet based on the current game state, * Evaluates the risk level of a proposed bet based on the current game state, the amount of the
* the amount of the bet, and the player's available chips. * bet, and the player's available chips.
* *
* @param state The current GameState object representing the state of the game, * @param state The current GameState object representing the state of the game, which includes
* which includes information about the phase of the game. * information about the phase of the game.
* @param amount The integer value representing the amount of the proposed bet * @param amount The integer value representing the amount of the proposed bet that the player
* that the player intends to make. * intends to make.
* @param chips The integer value representing the player's available chips. * @param chips The integer value representing the player's available chips.
* @return A BetRisk enum value indicating the risk level of the proposed bet. * @return A BetRisk enum value indicating the risk level of the proposed bet.
*/ */
@@ -846,8 +842,8 @@ public class TaskbarController {
/** /**
* Displays a warning dialog with the specified title and content. * Displays a warning dialog with the specified title and content.
* *
* @param title The string representing the title of the warning dialog, which is displayed * @param title The string representing the title of the warning dialog, which is displayed in
* in the title bar of the dialog window. * the title bar of the dialog window.
* @param content The string representing the content of the warning message, which is displayed * @param content The string representing the content of the warning message, which is displayed
* in the body of the dialog. * in the body of the dialog.
*/ */
@@ -964,8 +960,8 @@ public class TaskbarController {
/** /**
* Submits a preset input for either calling or raising based on the specified mode. * Submits a preset input for either calling or raising based on the specified mode.
* *
* @param mode A string indicating the mode of the action, which can be "call" * @param mode A string indicating the mode of the action, which can be "call" for calling the
* for calling the current bet or "raise" for raising to double the current bet. * current bet or "raise" for raising to double the current bet.
*/ */
private void submitPresetInputAndProcess(String mode) { private void submitPresetInputAndProcess(String mode) {
GameState state = ensureLatestStateForAction(mode); GameState state = ensureLatestStateForAction(mode);
@@ -1054,8 +1050,8 @@ public class TaskbarController {
/** /**
* Ensures that the latest game state is available for processing a player action. * Ensures that the latest game state is available for processing a player action.
* *
* @param actionName The name of the action being processed, used for logging purposes to indicate * @param actionName The name of the action being processed, used for logging purposes to
* which action is being attempted when the state is not available. * indicate which action is being attempted when the state is not available.
* @return The latest GameState object if available, or null if the state cannot be retrieved, * @return The latest GameState object if available, or null if the state cannot be retrieved,
* indicating that the action cannot be processed due to the lack of a valid game state. * indicating that the action cannot be processed due to the lack of a valid game state.
*/ */
@@ -18,8 +18,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static com.sun.media.jfxmedia.MediaManager.getPlayer;
/** /**
* GameController is responsible for managing the flow of the poker game. It interacts with the * GameController is responsible for managing the flow of the poker game. It interacts with the
* GameEngine to process player actions and update the game state accordingly. * GameEngine to process player actions and update the game state accordingly.
@@ -27,10 +27,11 @@ public class RoundManager {
public static final int BIG_BLIND = 200; public static final int BIG_BLIND = 200;
/** /**
* Starts a new hand by resetting the game state, ensuring a deck is available, dealing hole cards * Starts a new hand by resetting the game state, ensuring a deck is available, dealing hole
* to players, posting blinds, and setting the first player to act for the preflop phase. * cards to players, posting blinds, and setting the first player to act for the preflop phase.
* *
* @param state The GameState object representing the current state of the game, which will be modified to * @param state The GameState object representing the current state of the game, which will be
* modified to
*/ */
public void startNewHand(GameState state) { public void startNewHand(GameState state) {
// Use GameState's canonical reset // Use GameState's canonical reset
@@ -52,8 +53,8 @@ public class RoundManager {
/** /**
* Checks if the current betting round is finished and advances the game phase if necessary. * Checks if the current betting round is finished and advances the game phase if necessary.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which may be
* which may be modified to advance the phase or end the hand. * modified to advance the phase or end the hand.
*/ */
public void progressIfNeeded(GameState state) { public void progressIfNeeded(GameState state) {
if (state.getPhase() == null) { if (state.getPhase() == null) {
@@ -72,14 +73,13 @@ public class RoundManager {
} }
/** /**
* Determines if the current betting round is finished by checking if all active * Determines if the current betting round is finished by checking if all active players have
* players have met the current bet or are all-in and by handling special cases for * met the current bet or are all-in and by handling special cases for preflop betting rounds.
* preflop betting rounds.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which is used
* which is used to evaluate the betting round status. * to evaluate the betting round status.
* @return true if the betting round is finished and the game can progress to the * @return true if the betting round is finished and the game can progress to the next phase,
* next phase, false otherwise. * false otherwise.
*/ */
private boolean isBettingRoundFinished(GameState state) { private boolean isBettingRoundFinished(GameState state) {
// A betting round only ends after every active player had at least one chance to act. // A betting round only ends after every active player had at least one chance to act.
@@ -87,14 +87,14 @@ public class RoundManager {
} }
/** /**
* Helper method to check if all active players have acted in the current betting round. * Helper method to check if all active players have acted in the current betting round. This is
* This is used to handle the special case of preflop rounds where no bets have been made yet, * used to handle the special case of preflop rounds where no bets have been made yet, but
* but players still need to have the opportunity to act. * players still need to have the opportunity to act.
* *
* @param state The GameState object representing the current state of the game, which is * @param state The GameState object representing the current state of the game, which is used
* used to check if all active players have acted in the current round. * to check if all active players have acted in the current round.
* @return true if all active players have acted in the current round, false if there are * @return true if all active players have acted in the current round, false if there are still
* still active players who have not acted yet. * active players who have not acted yet.
*/ */
private boolean allActivePlayersActedThisRound(GameState state) { private boolean allActivePlayersActedThisRound(GameState state) {
for (Player p : state.getPlayers()) { for (Player p : state.getPlayers()) {
@@ -109,12 +109,12 @@ public class RoundManager {
return true; return true;
} }
/** /**
* Advances the game phase to the next stage (FLOP, TURN, RIVER, SHOWDOWN) based on the current phase. * Advances the game phase to the next stage (FLOP, TURN, RIVER, SHOWDOWN) based on the current
* phase.
* *
* @param state The GameState object representing the current state of the game, which will be modified * @param state The GameState object representing the current state of the game, which will be
* to advance the phase and deal community cards as needed when progressing to the * modified to advance the phase and deal community cards as needed when progressing to the
* next stage of the hand. * next stage of the hand.
*/ */
private void advancePhase(GameState state) { private void advancePhase(GameState state) {
@@ -133,9 +133,8 @@ public class RoundManager {
/** /**
* Ensures that a deck of cards is available in the game state. * Ensures that a deck of cards is available in the game state.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to include a new shuffled deck if one * modified to include a new shuffled deck if one does not already exist.
* does not already exist.
*/ */
private void ensureDeck(GameState state) { private void ensureDeck(GameState state) {
Deck deck = state.getDeck(); Deck deck = state.getDeck();
@@ -147,11 +146,11 @@ public class RoundManager {
} }
/** /**
* Deals hole cards to each player in the game state by drawing two cards from the * Deals hole cards to each player in the game state by drawing two cards from the deck for each
* deck for each player and assigning them as their hole cards. * player and assigning them as their hole cards.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to assign hole cards to each active player. * modified to assign hole cards to each active player.
*/ */
private void dealHoleCards(GameState state) { private void dealHoleCards(GameState state) {
Deck deck = state.getDeck(); Deck deck = state.getDeck();
@@ -217,13 +216,12 @@ public class RoundManager {
} }
/** /**
* Deals the flop by drawing three community cards from the deck and adding them * Deals the flop by drawing three community cards from the deck and adding them to the game
* to the game state, then setting the game phase to FLOP and resetting bets for * state, then setting the game phase to FLOP and resetting bets for the new betting round.
* the new betting round.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to add three community cards for the flop, * modified to add three community cards for the flop, set the phase to FLOP, and reset bets
* set the phase to FLOP, and reset bets for the new betting round. * for the new betting round.
*/ */
private void dealFlop(GameState state) { private void dealFlop(GameState state) {
ensureDeck(state); ensureDeck(state);
@@ -241,13 +239,12 @@ public class RoundManager {
} }
/** /**
* Deals the turn by drawing one community card from the deck and adding it to * Deals the turn by drawing one community card from the deck and adding it to the game state,
* the game state, then setting the game phase to TURN and resetting bets for * then setting the game phase to TURN and resetting bets for the new betting round.
* the new betting round.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to add one community card for the turn, * modified to add one community card for the turn, set the phase to TURN, and reset bets
* set the phase to TURN, and reset bets for the new betting round. * for the new betting round.
*/ */
private void dealTurn(GameState state) { private void dealTurn(GameState state) {
ensureDeck(state); ensureDeck(state);
@@ -263,13 +260,12 @@ public class RoundManager {
} }
/** /**
* Deals the river by drawing one community card from the deck and adding it * Deals the river by drawing one community card from the deck and adding it to the game state,
* to the game state, then setting the game phase to RIVER and resetting bets * then setting the game phase to RIVER and resetting bets for the new betting round.
* for the new betting round.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to add one community card for the river, * modified to add one community card for the river, set the phase to RIVER, and reset bets
* set the phase to RIVER, and reset bets for the new betting round. * for the new betting round.
*/ */
private void dealRiver(GameState state) { private void dealRiver(GameState state) {
ensureDeck(state); ensureDeck(state);
@@ -287,8 +283,8 @@ public class RoundManager {
/** /**
* Handles the showdown phase by setting the game phase to SHOWDOWN. * Handles the showdown phase by setting the game phase to SHOWDOWN.
* *
* @param state The GameState object representing the current state of the game, * @param state The GameState object representing the current state of the game, which will be
* which will be modified to set the phase to SHOWDOWN. * modified to set the phase to SHOWDOWN.
*/ */
private void showdown(GameState state) { private void showdown(GameState state) {
state.setPhase(GamePhase.SHOWDOWN); state.setPhase(GamePhase.SHOWDOWN);
@@ -103,8 +103,8 @@ public class GameState {
} }
/** /**
* Returns the current state of the table, including information such as the current bet * Returns the current state of the table, including information such as the current bet and pot
* and pot size. * size.
* *
* @return the current table state. * @return the current table state.
*/ */
@@ -113,8 +113,8 @@ public class GameState {
} }
/** /**
* Returns the current pot, which includes the total amount of chips in the pot * Returns the current pot, which includes the total amount of chips in the pot and the
* and the contributions from each player. * contributions from each player.
* *
* @return the current pot. * @return the current pot.
*/ */
@@ -141,12 +141,12 @@ public class GameState {
} }
/** /**
* Returns the hole cards for the specified player. If the player does not have * Returns the hole cards for the specified player. If the player does not have hole cards yet,
* hole cards yet, an empty list is returned and stored in the map for future reference. * an empty list is returned and stored in the map for future reference.
* *
* @param playerId the ID of the player whose hole cards are being requested. * @param playerId the ID of the player whose hole cards are being requested.
* @return a list of hole cards for the specified player, which may be empty if * @return a list of hole cards for the specified player, which may be empty if the player has
* the player has not been dealt cards yet. * not been dealt cards yet.
*/ */
public List<Card> getHoleCards(PlayerId playerId) { public List<Card> getHoleCards(PlayerId playerId) {
return holeCards.computeIfAbsent(playerId, k -> new ArrayList<>()); return holeCards.computeIfAbsent(playerId, k -> new ArrayList<>());
@@ -162,20 +162,19 @@ public class GameState {
} }
/** /**
* Returns a map of player IDs to their respective hole cards. Each player's * Returns a map of player IDs to their respective hole cards. Each player's hole cards are
* hole cards are represented as a list of Card objects. If a player does not * represented as a list of Card objects. If a player does not have hole cards yet, they will be
* have hole cards yet, they will be associated with an empty list. * associated with an empty list.
* *
* @return a map where the key is the player's ID and the value is a list of * @return a map where the key is the player's ID and the value is a list of their hole cards.
* their hole cards.
*/ */
public Map<PlayerId, List<Card>> getPlayerCards() { public Map<PlayerId, List<Card>> getPlayerCards() {
return holeCards; return holeCards;
} }
/** /**
* Returns the number of players currently in the game, based on the size of the * Returns the number of players currently in the game, based on the size of the player order
* player order list. * list.
* *
* @return the number of players in the game. * @return the number of players in the game.
*/ */
@@ -192,8 +191,8 @@ public class GameState {
} }
/** /**
* Sets the current player index to the first player who should act preflop, * Sets the current player index to the first player who should act preflop, which is determined
* which is determined based on the dealer index and the number of players. * based on the dealer index and the number of players.
*/ */
public void setCurrentPlayerToPreflopFirstToAct() { public void setCurrentPlayerToPreflopFirstToAct() {
int size = playerOrder.size(); int size = playerOrder.size();
@@ -212,8 +211,8 @@ public class GameState {
} }
/** /**
* Sets the current player index to the first player who should act postflop, * Sets the current player index to the first player who should act postflop, which is the
* which is the player immediately to the left of the dealer (i.e., dealerIndex + 1). * player immediately to the left of the dealer (i.e., dealerIndex + 1).
*/ */
public void setCurrentPlayerToPostflopFirstToAct() { public void setCurrentPlayerToPostflopFirstToAct() {
int size = playerOrder.size(); int size = playerOrder.size();
@@ -242,8 +241,8 @@ public class GameState {
} }
/** /**
* Checks if the specified player has already acted in the current round by checking * Checks if the specified player has already acted in the current round by checking if their ID
* if their ID is present in the set of players who have acted this round. * is present in the set of players who have acted this round.
* *
* @param playerId the ID of the player to check for having acted this round. * @param playerId the ID of the player to check for having acted this round.
* @return true if the player has acted this round, false otherwise. * @return true if the player has acted this round, false otherwise.
@@ -280,8 +279,8 @@ public class GameState {
/** /**
* Sets the index of the dealer in the player order list. * Sets the index of the dealer in the player order list.
* *
* @param dealerIndex the index to set as the dealer index, which should * @param dealerIndex the index to set as the dealer index, which should be within the bounds of
* be within the bounds of the player order list if it is not empty. * the player order list if it is not empty.
*/ */
public void setDealerIndex(int dealerIndex) { public void setDealerIndex(int dealerIndex) {
this.dealerIndex = dealerIndex; this.dealerIndex = dealerIndex;
@@ -290,9 +289,8 @@ public class GameState {
/** /**
* Sets the current deck of cards being used in the game. * Sets the current deck of cards being used in the game.
* *
* @param deck the Deck object to set as the current deck of cards for the game. * @param deck the Deck object to set as the current deck of cards for the game. This should be
* This should be a valid Deck instance that can be used for dealing * a valid Deck instance that can be used for dealing cards during the game.
* cards during the game.
*/ */
public void setDeck(Deck deck) { public void setDeck(Deck deck) {
this.deck = deck; this.deck = deck;
@@ -302,8 +300,8 @@ public class GameState {
* Adds a new player to the game with the specified ID and initial chip count. * Adds a new player to the game with the specified ID and initial chip count.
* *
* @param id the PlayerId of the new player to add to the game. * @param id the PlayerId of the new player to add to the game.
* @param chips the initial number of chips the player has, which can be used * @param chips the initial number of chips the player has, which can be used for betting during
* for betting during the game. * the game.
*/ */
public void addPlayer(PlayerId id, int chips) { public void addPlayer(PlayerId id, int chips) {
Player player = new Player(id, chips); Player player = new Player(id, chips);
@@ -357,16 +355,16 @@ public class GameState {
} }
/** /**
* Helper method to move an entry in a map from an old key to a new key, * Helper method to move an entry in a map from an old key to a new key, with a fallback value
* with a fallback value if the old key is not present. * if the old key is not present.
* *
* @param map the map in which to move the entry, where the key is a PlayerId * @param map the map in which to move the entry, where the key is a PlayerId and the value is
* and the value is of type T. * of type T.
* @param oldId the existing PlayerId key that is being renamed. * @param oldId the existing PlayerId key that is being renamed.
* @param newId the new PlayerId key to which the entry should be moved. * @param newId the new PlayerId key to which the entry should be moved.
* @param fallback the value to use if the oldId is not present in the map. * @param fallback the value to use if the oldId is not present in the map.
* @param <T> the type of the values in the map, which can be any type that is * @param <T> the type of the values in the map, which can be any type that is used for
* used for player-related data in the game state. * player-related data in the game state.
*/ */
private <T> void moveMapEntry( private <T> void moveMapEntry(
Map<PlayerId, T> map, PlayerId oldId, PlayerId newId, T fallback) { Map<PlayerId, T> map, PlayerId oldId, PlayerId newId, T fallback) {
@@ -380,8 +378,8 @@ public class GameState {
* Returns the current bet amount for the specified player. * Returns the current bet amount for the specified player.
* *
* @param playerId the ID of the player whose current bet is being requested. * @param playerId the ID of the player whose current bet is being requested.
* @return the current bet amount for the specified player, * @return the current bet amount for the specified player, or 0 if the player does not have a
* or 0 if the player does not have a current bet. * current bet.
*/ */
public int getCurrentBet(PlayerId playerId) { public int getCurrentBet(PlayerId playerId) {
return currentBets.getOrDefault(playerId, 0); return currentBets.getOrDefault(playerId, 0);
@@ -397,9 +395,7 @@ public class GameState {
currentBets.put(playerId, amount); currentBets.put(playerId, amount);
} }
/** /** Reset bets to 0 for ALL players (do not clear the map). */
* Reset bets to 0 for ALL players (do not clear the map).
*/
public void resetBets() { public void resetBets() {
for (PlayerId id : playerOrder) { for (PlayerId id : playerOrder) {
currentBets.put(id, 0); currentBets.put(id, 0);
@@ -433,8 +429,8 @@ public class GameState {
* Retrieves the Player object associated with the given PlayerId. * Retrieves the Player object associated with the given PlayerId.
* *
* @param id the PlayerId of the player to retrieve. * @param id the PlayerId of the player to retrieve.
* @return the Player object associated with the specified PlayerId, or a RuntimeException * @return the Player object associated with the specified PlayerId, or a RuntimeException if
* if the player is not found. * the player is not found.
*/ */
public Player getPlayer(PlayerId id) { public Player getPlayer(PlayerId id) {
Player player = players.get(id); Player player = players.get(id);
@@ -448,10 +444,9 @@ public class GameState {
/** /**
* Retrieves the current bet commitment for the specified player. * Retrieves the current bet commitment for the specified player.
* *
* @param playerId the ID of the player whose current bet commitment * @param playerId the ID of the player whose current bet commitment is being requested.
* is being requested. * @return the current bet commitment for the specified player, or 0 if the player does not have
* @return the current bet commitment for the specified player, * a bet commitment.
* or 0 if the player does not have a bet commitment.
*/ */
public int getCurrentBetCommitment(PlayerId playerId) { public int getCurrentBetCommitment(PlayerId playerId) {
return playerBetCommitments.getOrDefault(playerId, 0); return playerBetCommitments.getOrDefault(playerId, 0);
@@ -483,16 +478,12 @@ public class GameState {
communityCards.add(card); communityCards.add(card);
} }
/** /** Resets the community cards by clearing the list of community cards on the table. */
* Resets the community cards by clearing the list of community cards on the table.
*/
public void resetCommunityCards() { public void resetCommunityCards() {
communityCards.clear(); communityCards.clear();
} }
/** /** Advances the current player index to the next player who can act. */
* Advances the current player index to the next player who can act.
*/
public void nextPlayer() { public void nextPlayer() {
if (playerOrder.isEmpty()) { if (playerOrder.isEmpty()) {
currentPlayerIndex = 0; currentPlayerIndex = 0;
@@ -511,11 +502,12 @@ public class GameState {
} }
/** /**
* Checks if the player at the specified index in the player order list is able to take an action. * Checks if the player at the specified index in the player order list is able to take an
* action.
* *
* @param index the index of the player in the player order list to check for actionability. * @param index the index of the player in the player order list to check for actionability.
* @return true if the player at the specified index can act (i.e., is not folded and not all-in), * @return true if the player at the specified index can act (i.e., is not folded and not
* false otherwise. * all-in), false otherwise.
*/ */
private boolean canPlayerAct(int index) { private boolean canPlayerAct(int index) {
if (index < 0 || index >= playerOrder.size()) { if (index < 0 || index >= playerOrder.size()) {
@@ -532,11 +524,11 @@ public class GameState {
} }
/** /**
* Returns the Player object representing the current dealer based on the dealer index in * Returns the Player object representing the current dealer based on the dealer index in the
* the player order list. * player order list.
* *
* @return the Player object representing the current dealer, * @return the Player object representing the current dealer, or null if there are no players or
* or null if there are no players or the dealer index is out of bounds. * the dealer index is out of bounds.
*/ */
public Player getDealer() { public Player getDealer() {
PlayerId id = playerOrder.get(dealerIndex); PlayerId id = playerOrder.get(dealerIndex);
@@ -588,8 +580,8 @@ public class GameState {
} }
/** /**
* Checks if the specified player has folded by retrieving their Player object * Checks if the specified player has folded by retrieving their Player object and checking
* and checking their folded status. * their folded status.
* *
* @param playerId the ID of the player to check for folded status. * @param playerId the ID of the player to check for folded status.
* @return true if the player has folded, false otherwise. * @return true if the player has folded, false otherwise.
@@ -50,7 +50,8 @@ public class GameControllerTest {
/** /**
* Helper method to simulate a call action for the current player in the game. * Helper method to simulate a call action for the current player in the game.
* *
* @param game The GameController instance on which to perform the call action for the current player. * @param game The GameController instance on which to perform the call action for the current
* player.
*/ */
private static void callCurrent(GameController game) { private static void callCurrent(GameController game) {
game.playerCall(currentPlayerId(game)); game.playerCall(currentPlayerId(game));
@@ -59,15 +60,16 @@ public class GameControllerTest {
/** /**
* Helper method to simulate a fold action for the current player in the game. * Helper method to simulate a fold action for the current player in the game.
* *
* @param game The GameController instance on which to perform the fold action for the current player. * @param game The GameController instance on which to perform the fold action for the current
* player.
*/ */
private static void foldCurrent(GameController game) { private static void foldCurrent(GameController game) {
game.playerFold(currentPlayerId(game)); game.playerFold(currentPlayerId(game));
} }
/** /**
* Verifies that the preflop phase ends once all active (non-folded) players * Verifies that the preflop phase ends once all active (non-folded) players have completed
* have completed their required actions for the current betting round. * their required actions for the current betting round.
*/ */
@Test @Test
public void testPreflopEndsAfterOneActionPerActivePlayer() { public void testPreflopEndsAfterOneActionPerActivePlayer() {
@@ -121,8 +123,8 @@ public class GameControllerTest {
} }
/** /**
* Ensures that during the preflop phase, folded players are excluded * Ensures that during the preflop phase, folded players are excluded from progression checks
* from progression checks and do not block phase advancement. * and do not block phase advancement.
*/ */
@Test @Test
public void testPreflopCountsOnlyNonFoldedPlayersForProgress() { public void testPreflopCountsOnlyNonFoldedPlayersForProgress() {