Fix: resolve checkstyle magic number and apply spotless formatting
This commit is contained in:
+110
-114
@@ -56,6 +56,7 @@ public class TaskbarController {
|
||||
private static final String STYLE_GRAY_INPUT = "gray-input-field";
|
||||
private static final String STYLE_RED_INPUT = "red-input-field";
|
||||
private static final double SCALE_NORMAL = 1.0;
|
||||
private static final int DEALER_OFFSET = 3;
|
||||
private boolean inputActionAllowed;
|
||||
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.
|
||||
*/
|
||||
@@ -217,8 +219,9 @@ public class TaskbarController {
|
||||
public void initialize() {
|
||||
updateBasicButtons(false, false);
|
||||
if (taskbarInput != null) {
|
||||
taskbarInput.textProperty().addListener((obs, oldValue, newValue)
|
||||
-> refreshBetInputUi());
|
||||
taskbarInput
|
||||
.textProperty()
|
||||
.addListener((obs, oldValue, newValue) -> refreshBetInputUi());
|
||||
}
|
||||
|
||||
setBetButtonVisible(false);
|
||||
@@ -228,12 +231,12 @@ public class TaskbarController {
|
||||
* 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
|
||||
* information about the players, their bets, the current phase and other relevant
|
||||
* details needed to update the taskbar UI accurately based on the player's status
|
||||
* and the game context.
|
||||
* information about the players, their bets, the current phase and other relevant details
|
||||
* needed to update the taskbar UI accurately based on the player's status and the game
|
||||
* context.
|
||||
* @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
|
||||
* it is their turn and whether they are out of the game.
|
||||
* player's status and update the taskbar UI accordingly based on whether it is their turn
|
||||
* and whether they are out of the game.
|
||||
*/
|
||||
public void update(GameState state, PlayerId myPlayerId) {
|
||||
|
||||
@@ -275,11 +278,11 @@ public class TaskbarController {
|
||||
*
|
||||
* @param state The current game state to evaluate action availability.
|
||||
* @param me The player object representing the current player, used to determine their chips
|
||||
* and bet status.
|
||||
* and bet status.
|
||||
* @param isMyTurn Indicates whether it is currently the player's turn, which affects whether
|
||||
* actions can be taken.
|
||||
* @param isOutOrFinished Indicates whether the player is out of the game (folded or all-in)
|
||||
* or if the game is finished, which disables actions.
|
||||
* actions can be taken.
|
||||
* @param isOutOrFinished Indicates whether the player is out of the game (folded or all-in) or
|
||||
* if the game is finished, which disables actions.
|
||||
*/
|
||||
private void applyActionAvailability(
|
||||
GameState state, Player me, boolean isMyTurn, boolean isOutOrFinished) {
|
||||
@@ -338,8 +341,8 @@ public class TaskbarController {
|
||||
* Safely doubles the given value while preventing integer overflow.
|
||||
*
|
||||
* @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
|
||||
* if doubling would overflow.
|
||||
* @return The safely doubled value, or 0 if the input is non-positive, or Integer.MAX_VALUE if
|
||||
* doubling would overflow.
|
||||
*/
|
||||
private int safeDouble(int value) {
|
||||
if (value <= 0) {
|
||||
@@ -354,8 +357,8 @@ public class TaskbarController {
|
||||
/**
|
||||
* Sets the visibility of the Bet button in the taskbar.
|
||||
*
|
||||
* @param visible A boolean indicating whether the Bet button should be visible (true)
|
||||
* or hidden (false).
|
||||
* @param visible A boolean indicating whether the Bet button should be visible (true) or hidden
|
||||
* (false).
|
||||
*/
|
||||
private void setBetButtonVisible(boolean visible) {
|
||||
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
|
||||
* the current game state and the validity of the input.
|
||||
* Refreshes the user interface of the bet input field and the Bet button based on the current
|
||||
* game state and the validity of the input.
|
||||
*/
|
||||
private void refreshBetInputUi() {
|
||||
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.
|
||||
*
|
||||
* @param button The Button object to be enabled or disabled based on the provided boolean value.
|
||||
* @param enabled A boolean value indicating whether the button should be enabled (true)
|
||||
* or disabled (false).
|
||||
* @param button The Button object to be enabled or disabled based on the provided boolean
|
||||
* value.
|
||||
* @param enabled A boolean value indicating whether the button should be enabled (true) or
|
||||
* disabled (false).
|
||||
*/
|
||||
private void setActionEnabled(Button button, boolean enabled) {
|
||||
if (enabled) {
|
||||
@@ -404,10 +408,10 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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
|
||||
* includes 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
|
||||
* they have already invested in the current bet.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* 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 they
|
||||
* have already invested in 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) {
|
||||
@@ -417,11 +421,11 @@ public class TaskbarController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the effective call target for the current game state. If there is an active current
|
||||
* bet, it returns that as the call target.
|
||||
* Determines the effective call target for the current game state. If there is an active
|
||||
* current bet, it returns that as the call target.
|
||||
*
|
||||
* @param state The current GameState object representing the state of the game, which
|
||||
* includes information about the current bet and the last reference bet.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* information about the current bet and the last reference bet.
|
||||
* @return An integer representing the effective call target for the current game 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
|
||||
* and if the initial blind layout is still in place.
|
||||
* Checks if the current player is the first to act in the pre-flop phase and if the initial
|
||||
* blind layout is still in place.
|
||||
*
|
||||
* @param state The current GameState object representing the state of the game,
|
||||
* which includes information about the phase of the game, the players,
|
||||
* the dealer position, and the active player.
|
||||
* @param me The Player object representing the current player, used to determine
|
||||
* their position 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
|
||||
* in the pre-flop phase with only the initial blind layout in place.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* information about the phase of the game, the players, the dealer position, and the active
|
||||
* player.
|
||||
* @param me The Player object representing the current player, used to determine their position
|
||||
* 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 in the
|
||||
* pre-flop phase with only the initial blind layout in place.
|
||||
*/
|
||||
private boolean isFirstPreflopPlayerInputOnly(GameState state, Player me) {
|
||||
if (state == null || me == null || !isPreflop(state.phase) || state.players == null) {
|
||||
@@ -459,7 +463,7 @@ public class TaskbarController {
|
||||
}
|
||||
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
@@ -470,10 +474,10 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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,
|
||||
* which includes information about the players and their bets.
|
||||
* @return A boolean value indicating whether 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, which includes
|
||||
* information about the players and their bets.
|
||||
* @return A boolean value indicating whether the initial blind layout is still in place during
|
||||
* the pre-flop phase.
|
||||
*/
|
||||
private boolean isInitialPreflopBlindLayout(GameState state) {
|
||||
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.
|
||||
*
|
||||
* @param phase The string representing the current phase of the game, which is expected
|
||||
* to be 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
|
||||
* pre-flop phase.
|
||||
* @param phase The string representing the current phase of the game, which is expected to be
|
||||
* 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 pre-flop
|
||||
* phase.
|
||||
*/
|
||||
private boolean isPreflop(String 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.
|
||||
*
|
||||
* @param phase The string representing the current phase of the game, which is
|
||||
* expected to be 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.
|
||||
* @param phase The string representing the current phase of the game, which is expected to be
|
||||
* 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.
|
||||
*/
|
||||
private boolean isFlop(String 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.
|
||||
*
|
||||
* @param phase The string representing the current phase of the game, which is expected to
|
||||
* be compared against "TURN" and "RIVER" to determine if it is either the turn
|
||||
* or river phase.
|
||||
* @return A boolean value indicating whether the given phase string corresponds to either
|
||||
* the turn or river phase.
|
||||
* @param phase The string representing the current phase of the game, which is expected to be
|
||||
* compared against "TURN" and "RIVER" to determine if it is either the turn or river phase.
|
||||
* @return A boolean value indicating whether the given phase string corresponds to either the
|
||||
* turn or river phase.
|
||||
*/
|
||||
private boolean isTurnOrRiver(String phase) {
|
||||
return "TURN".equalsIgnoreCase(phase) || "RIVER".equalsIgnoreCase(phase);
|
||||
@@ -538,17 +542,17 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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
|
||||
* checking if the phase is "FINISHED" or "SHOWDOWN",
|
||||
* checking if the phase is "FINISHED" or "SHOWDOWN",
|
||||
*/
|
||||
private boolean isHandFinished(GameState state) {
|
||||
if (state == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("FINISHED".equalsIgnoreCase(state.phase)
|
||||
|| "SHOWDOWN".equalsIgnoreCase(state.phase)) {
|
||||
if ("FINISHED".equalsIgnoreCase(state.phase) || "SHOWDOWN".equalsIgnoreCase(state.phase)) {
|
||||
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
|
||||
* the bet and the player's available chips.
|
||||
* Evaluates the risk level of a proposed bet based on the current game state, the amount of the
|
||||
* bet and the player's available chips.
|
||||
*/
|
||||
private enum BetRisk {
|
||||
ALLOWED,
|
||||
@@ -567,18 +571,14 @@ public class TaskbarController {
|
||||
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 {
|
||||
INPUT,
|
||||
CALL_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 final boolean valid;
|
||||
private final boolean warning;
|
||||
@@ -607,8 +607,7 @@ public class TaskbarController {
|
||||
* Submits a player action based on the specified ActionType.
|
||||
*
|
||||
* @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
|
||||
* calling or raising.
|
||||
* (where the player types an amount) or a specific button action for calling or raising.
|
||||
*/
|
||||
private void submitAction(ActionType actionType) {
|
||||
if (gameService == null) {
|
||||
@@ -678,13 +677,13 @@ public class TaskbarController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the latest game state is available for processing a player action.
|
||||
* If the last known state is null, it attempts to fetch the current state from the GameService.
|
||||
* Ensures that the latest game state is available for processing a player action. If the last
|
||||
* 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
|
||||
* to indicate which action is being attempted when the state is not available.
|
||||
* @param text The text associated with the action being processed, used for logging purposes to
|
||||
* 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,
|
||||
* indicating that the action cannot be processed.
|
||||
* indicating that the action cannot be processed.
|
||||
*/
|
||||
private Integer parseInputTarget(String text) {
|
||||
if (text == null || text.trim().isEmpty()) {
|
||||
@@ -700,14 +699,13 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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,
|
||||
* which includes a list of players.
|
||||
* @param text The text associated with the action being processed, used for logging
|
||||
* purposes to indicate which action is being attempted when the player
|
||||
* cannot be found in the state.
|
||||
* @return The Player object representing the current player if found in the game state,
|
||||
* or null if no matching player is found, indicating that the player cannot be identified
|
||||
* in the current game state.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* a list of players.
|
||||
* @param text The text associated with the action being processed, used for logging purposes to
|
||||
* indicate which action is being attempted when the player cannot be found in the state.
|
||||
* @return The Player object representing the current player if found in the game state, or null
|
||||
* if no matching player is found, indicating that the player cannot be identified in the
|
||||
* current game state.
|
||||
*/
|
||||
private ValidationResult validateTypedAmount(GameState state, String 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.
|
||||
* It checks if the target bet is either a valid call or a valid raise
|
||||
* (specifically, exactly double the call target).
|
||||
* Validates a proposed target bet against the current game state and the player's status. It
|
||||
* checks if the target bet is either a valid call or a valid raise (specifically, exactly
|
||||
* double the call target).
|
||||
*
|
||||
* @param state The current GameState object representing the state of the game,
|
||||
* which includes information about the current bet, the phase of the game,
|
||||
* and the players.
|
||||
* @param me The Player object representing the current player, used to determine their
|
||||
* current bet, available chips, and to evaluate whether the proposed target
|
||||
* 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 intends to make.
|
||||
* @return A ValidationResult object indicating whether the proposed target bet is valid,
|
||||
* if it triggers a warning, or if it is blocked due to being invalid or too risky.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* information about the current bet, the phase of the game, and the players.
|
||||
* @param me The Player object representing the current player, used to determine their current
|
||||
* bet, available chips, and to evaluate whether the proposed target 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
|
||||
* intends to make.
|
||||
* @return A ValidationResult object indicating whether the proposed target bet is valid, 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) {
|
||||
if (state == null || me == null) {
|
||||
@@ -777,11 +774,10 @@ public class TaskbarController {
|
||||
/**
|
||||
* Generates a warning message based on the current phase of the game.
|
||||
*
|
||||
* @param state The current GameState object representing the state of the game,
|
||||
* which includes information about the phase of the game.
|
||||
* @return A string containing the warning message appropriate for the current phase
|
||||
* of the game when a player's bet triggers a warning due to being at least
|
||||
* 50% of their stack.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* information about the phase of the game.
|
||||
* @return A string containing the warning message appropriate for the current phase of the game
|
||||
* when a player's bet triggers a warning due to being at least 50% of their stack.
|
||||
*/
|
||||
private String warningTextForPhase(GameState state) {
|
||||
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,
|
||||
* the amount of the bet, and the player's available chips.
|
||||
* Evaluates the risk level of a proposed bet based on the current game state, the amount of the
|
||||
* bet, and the player's available chips.
|
||||
*
|
||||
* @param state The current GameState object representing the state of the game,
|
||||
* which includes information about the phase of the game.
|
||||
* @param amount The integer value representing the amount of the proposed bet
|
||||
* that the player intends to make.
|
||||
* @param state The current GameState object representing the state of the game, which includes
|
||||
* information about the phase of the game.
|
||||
* @param amount The integer value representing the amount of the proposed bet that the player
|
||||
* intends to make.
|
||||
* @param chips The integer value representing the player's available chips.
|
||||
* @return A BetRisk enum value indicating the risk level of the proposed bet.
|
||||
*/
|
||||
@@ -846,10 +842,10 @@ public class TaskbarController {
|
||||
/**
|
||||
* Displays a warning dialog with the specified title and content.
|
||||
*
|
||||
* @param title The string representing the title of the warning dialog, which is displayed
|
||||
* in the title bar of the dialog window.
|
||||
* @param title The string representing the title of the warning dialog, which is displayed in
|
||||
* the title bar of the dialog window.
|
||||
* @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.
|
||||
*/
|
||||
private void showWarningDialog(String title, String content) {
|
||||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||||
@@ -964,8 +960,8 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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"
|
||||
* for calling the current bet or "raise" for raising to double the current bet.
|
||||
* @param mode A string indicating the mode of the action, which can be "call" for calling the
|
||||
* current bet or "raise" for raising to double the current bet.
|
||||
*/
|
||||
private void submitPresetInputAndProcess(String mode) {
|
||||
GameState state = ensureLatestStateForAction(mode);
|
||||
@@ -1054,10 +1050,10 @@ public class TaskbarController {
|
||||
/**
|
||||
* 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
|
||||
* which action is being attempted when the state is not available.
|
||||
* @param actionName The name of the action being processed, used for logging purposes to
|
||||
* 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,
|
||||
* 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.
|
||||
*/
|
||||
private GameState ensureLatestStateForAction(String actionName) {
|
||||
try {
|
||||
|
||||
@@ -18,8 +18,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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
|
||||
* GameEngine to process player actions and update the game state accordingly.
|
||||
|
||||
+47
-51
@@ -27,10 +27,11 @@ public class RoundManager {
|
||||
public static final int BIG_BLIND = 200;
|
||||
|
||||
/**
|
||||
* Starts a new hand by resetting the game state, ensuring a deck is available, dealing hole cards
|
||||
* to players, posting blinds, and setting the first player to act for the preflop phase.
|
||||
* Starts a new hand by resetting the game state, ensuring a deck is available, dealing hole
|
||||
* 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) {
|
||||
// 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.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which may be modified to advance the phase or end the hand.
|
||||
* @param state The GameState object representing the current state of the game, which may be
|
||||
* modified to advance the phase or end the hand.
|
||||
*/
|
||||
public void progressIfNeeded(GameState state) {
|
||||
if (state.getPhase() == null) {
|
||||
@@ -72,14 +73,13 @@ public class RoundManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current betting round is finished by checking if all active
|
||||
* players have met the current bet or are all-in and by handling special cases for
|
||||
* preflop betting rounds.
|
||||
* Determines if the current betting round is finished by checking if all active players have
|
||||
* met the current bet or are all-in and by handling special cases for preflop betting rounds.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which is used to evaluate the betting round status.
|
||||
* @return true if the betting round is finished and the game can progress to the
|
||||
* next phase, false otherwise.
|
||||
* @param state The GameState object representing the current state of the game, which is used
|
||||
* to evaluate the betting round status.
|
||||
* @return true if the betting round is finished and the game can progress to the next phase,
|
||||
* false otherwise.
|
||||
*/
|
||||
private boolean isBettingRoundFinished(GameState state) {
|
||||
// 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.
|
||||
* This is used to handle the special case of preflop rounds where no bets have been made yet,
|
||||
* but players still need to have the opportunity to act.
|
||||
* Helper method to check if all active players have acted in the current betting round. This is
|
||||
* used to handle the special case of preflop rounds where no bets have been made yet, but
|
||||
* players still need to have the opportunity to act.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game, which is
|
||||
* used 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
|
||||
* still active players who have not acted yet.
|
||||
* @param state The GameState object representing the current state of the game, which is used
|
||||
* 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 still
|
||||
* active players who have not acted yet.
|
||||
*/
|
||||
private boolean allActivePlayersActedThisRound(GameState state) {
|
||||
for (Player p : state.getPlayers()) {
|
||||
@@ -109,13 +109,13 @@ public class RoundManager {
|
||||
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
|
||||
* to advance the phase and deal community cards as needed when progressing to the
|
||||
* next stage of the hand.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to advance the phase and deal community cards as needed when progressing to the
|
||||
* next stage of the hand.
|
||||
*/
|
||||
private void advancePhase(GameState state) {
|
||||
switch (state.getPhase()) {
|
||||
@@ -133,9 +133,8 @@ public class RoundManager {
|
||||
/**
|
||||
* Ensures that a deck of cards is available in the game state.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to include a new shuffled deck if one
|
||||
* does not already exist.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to include a new shuffled deck if one does not already exist.
|
||||
*/
|
||||
private void ensureDeck(GameState state) {
|
||||
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
|
||||
* deck for each player and assigning them as their hole cards.
|
||||
* Deals hole cards to each player in the game state by drawing two cards from the deck for each
|
||||
* player and assigning them as their hole cards.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to assign hole cards to each active player.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to assign hole cards to each active player.
|
||||
*/
|
||||
private void dealHoleCards(GameState state) {
|
||||
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
|
||||
* to the game state, then setting the game phase to FLOP and resetting bets for
|
||||
* the new betting round.
|
||||
* Deals the flop by drawing three community cards from the deck and adding them to the game
|
||||
* state, then setting the game phase to FLOP and resetting bets for the new betting round.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to add three community cards for the flop,
|
||||
* set the phase to FLOP, and reset bets for the new betting round.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to add three community cards for the flop, set the phase to FLOP, and reset bets
|
||||
* for the new betting round.
|
||||
*/
|
||||
private void dealFlop(GameState 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
|
||||
* the game state, then setting the game phase to TURN and resetting bets for
|
||||
* the new betting round.
|
||||
* Deals the turn by drawing one community card from the deck and adding it to the game state,
|
||||
* then setting the game phase to TURN and resetting bets for the new betting round.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to add one community card for the turn,
|
||||
* set the phase to TURN, and reset bets for the new betting round.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to add one community card for the turn, set the phase to TURN, and reset bets
|
||||
* for the new betting round.
|
||||
*/
|
||||
private void dealTurn(GameState state) {
|
||||
ensureDeck(state);
|
||||
@@ -263,13 +260,12 @@ public class RoundManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deals the river by drawing one community card from the deck and adding it
|
||||
* to the game state, then setting the game phase to RIVER and resetting bets
|
||||
* for the new betting round.
|
||||
* Deals the river by drawing one community card from the deck and adding it to the game state,
|
||||
* then setting the game phase to RIVER and resetting bets for the new betting round.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to add one community card for the river,
|
||||
* set the phase to RIVER, and reset bets for the new betting round.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to add one community card for the river, set the phase to RIVER, and reset bets
|
||||
* for the new betting round.
|
||||
*/
|
||||
private void dealRiver(GameState state) {
|
||||
ensureDeck(state);
|
||||
@@ -287,8 +283,8 @@ public class RoundManager {
|
||||
/**
|
||||
* Handles the showdown phase by setting the game phase to SHOWDOWN.
|
||||
*
|
||||
* @param state The GameState object representing the current state of the game,
|
||||
* which will be modified to set the phase to SHOWDOWN.
|
||||
* @param state The GameState object representing the current state of the game, which will be
|
||||
* modified to set the phase to SHOWDOWN.
|
||||
*/
|
||||
private void showdown(GameState state) {
|
||||
state.setPhase(GamePhase.SHOWDOWN);
|
||||
|
||||
+52
-60
@@ -103,8 +103,8 @@ public class GameState {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current state of the table, including information such as the current bet
|
||||
* and pot size.
|
||||
* Returns the current state of the table, including information such as the current bet and pot
|
||||
* size.
|
||||
*
|
||||
* @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
|
||||
* and the contributions from each player.
|
||||
* Returns the current pot, which includes the total amount of chips in the pot and the
|
||||
* contributions from each player.
|
||||
*
|
||||
* @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
|
||||
* hole cards yet, an empty list is returned and stored in the map for future reference.
|
||||
* Returns the hole cards for the specified player. If the player does not have hole cards yet,
|
||||
* 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.
|
||||
* @return a list of hole cards for the specified player, which may be empty if
|
||||
* the player has not been dealt cards yet.
|
||||
* @return a list of hole cards for the specified player, which may be empty if the player has
|
||||
* not been dealt cards yet.
|
||||
*/
|
||||
public List<Card> getHoleCards(PlayerId playerId) {
|
||||
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
|
||||
* hole cards are represented as a list of Card objects. If a player does not
|
||||
* have hole cards yet, they will be associated with an empty list.
|
||||
* Returns a map of player IDs to their respective hole cards. Each player's hole cards are
|
||||
* represented as a list of Card objects. If a player does not have hole cards yet, they will be
|
||||
* associated with an empty list.
|
||||
*
|
||||
* @return a map where the key is the player's ID and the value is a list of
|
||||
* their hole cards.
|
||||
* @return a map where the key is the player's ID and the value is a list of their hole cards.
|
||||
*/
|
||||
public Map<PlayerId, List<Card>> getPlayerCards() {
|
||||
return holeCards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of players currently in the game, based on the size of the
|
||||
* player order list.
|
||||
* Returns the number of players currently in the game, based on the size of the player order
|
||||
* list.
|
||||
*
|
||||
* @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,
|
||||
* which is determined based on the dealer index and the number of players.
|
||||
* Sets the current player index to the first player who should act preflop, which is determined
|
||||
* based on the dealer index and the number of players.
|
||||
*/
|
||||
public void setCurrentPlayerToPreflopFirstToAct() {
|
||||
int size = playerOrder.size();
|
||||
@@ -212,8 +211,8 @@ public class GameState {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current player index to the first player who should act postflop,
|
||||
* which is the player immediately to the left of the dealer (i.e., dealerIndex + 1).
|
||||
* Sets the current player index to the first player who should act postflop, which is the
|
||||
* player immediately to the left of the dealer (i.e., dealerIndex + 1).
|
||||
*/
|
||||
public void setCurrentPlayerToPostflopFirstToAct() {
|
||||
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
|
||||
* if their ID is present in the set of players who have acted this round.
|
||||
* Checks if the specified player has already acted in the current round by checking if their ID
|
||||
* 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.
|
||||
* @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.
|
||||
*
|
||||
* @param dealerIndex the index to set as the dealer index, which should
|
||||
* be within the bounds of the player order list if it is not empty.
|
||||
* @param dealerIndex the index to set as the dealer index, which should be within the bounds of
|
||||
* the player order list if it is not empty.
|
||||
*/
|
||||
public void setDealerIndex(int dealerIndex) {
|
||||
this.dealerIndex = dealerIndex;
|
||||
@@ -290,9 +289,8 @@ public class GameState {
|
||||
/**
|
||||
* 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.
|
||||
* This should be a valid Deck instance that can be used for dealing
|
||||
* cards during the game.
|
||||
* @param deck the Deck object to set as the current deck of cards for the game. This should be
|
||||
* a valid Deck instance that can be used for dealing cards during the game.
|
||||
*/
|
||||
public void setDeck(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.
|
||||
*
|
||||
* @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
|
||||
* for betting during the game.
|
||||
* @param chips the initial number of chips the player has, which can be used for betting during
|
||||
* the game.
|
||||
*/
|
||||
public void addPlayer(PlayerId id, int 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,
|
||||
* with a fallback value if the old key is not present.
|
||||
* Helper method to move an entry in a map from an old key to a new key, with a fallback value
|
||||
* if the old key is not present.
|
||||
*
|
||||
* @param map the map in which to move the entry, where the key is a PlayerId
|
||||
* and the value is of type T.
|
||||
* @param map the map in which to move the entry, where the key is a PlayerId and the value is
|
||||
* of type T.
|
||||
* @param oldId the existing PlayerId key that is being renamed.
|
||||
* @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 <T> the type of the values in the map, which can be any type that is
|
||||
* used for player-related data in the game state.
|
||||
* @param <T> the type of the values in the map, which can be any type that is used for
|
||||
* player-related data in the game state.
|
||||
*/
|
||||
private <T> void moveMapEntry(
|
||||
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.
|
||||
*
|
||||
* @param playerId the ID of the player whose current bet is being requested.
|
||||
* @return the current bet amount for the specified player,
|
||||
* or 0 if the player does not have a current bet.
|
||||
* @return the current bet amount for the specified player, or 0 if the player does not have a
|
||||
* current bet.
|
||||
*/
|
||||
public int getCurrentBet(PlayerId playerId) {
|
||||
return currentBets.getOrDefault(playerId, 0);
|
||||
@@ -397,9 +395,7 @@ public class GameState {
|
||||
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() {
|
||||
for (PlayerId id : playerOrder) {
|
||||
currentBets.put(id, 0);
|
||||
@@ -433,8 +429,8 @@ public class GameState {
|
||||
* Retrieves the Player object associated with the given PlayerId.
|
||||
*
|
||||
* @param id the PlayerId of the player to retrieve.
|
||||
* @return the Player object associated with the specified PlayerId, or a RuntimeException
|
||||
* if the player is not found.
|
||||
* @return the Player object associated with the specified PlayerId, or a RuntimeException if
|
||||
* the player is not found.
|
||||
*/
|
||||
public Player getPlayer(PlayerId id) {
|
||||
Player player = players.get(id);
|
||||
@@ -448,10 +444,9 @@ public class GameState {
|
||||
/**
|
||||
* Retrieves the current bet commitment for the specified player.
|
||||
*
|
||||
* @param playerId the ID of the player whose current bet commitment
|
||||
* is being requested.
|
||||
* @return the current bet commitment for the specified player,
|
||||
* or 0 if the player does not have a bet commitment.
|
||||
* @param playerId the ID of the player whose current bet commitment is being requested.
|
||||
* @return the current bet commitment for the specified player, or 0 if the player does not have
|
||||
* a bet commitment.
|
||||
*/
|
||||
public int getCurrentBetCommitment(PlayerId playerId) {
|
||||
return playerBetCommitments.getOrDefault(playerId, 0);
|
||||
@@ -483,16 +478,12 @@ public class GameState {
|
||||
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() {
|
||||
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() {
|
||||
if (playerOrder.isEmpty()) {
|
||||
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.
|
||||
* @return true if the player at the specified index can act (i.e., is not folded and not all-in),
|
||||
* false otherwise.
|
||||
* @return true if the player at the specified index can act (i.e., is not folded and not
|
||||
* all-in), false otherwise.
|
||||
*/
|
||||
private boolean canPlayerAct(int index) {
|
||||
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
|
||||
* the player order list.
|
||||
* Returns the Player object representing the current dealer based on the dealer index in the
|
||||
* player order list.
|
||||
*
|
||||
* @return the Player object representing the current dealer,
|
||||
* or null if there are no players or the dealer index is out of bounds.
|
||||
* @return the Player object representing the current dealer, or null if there are no players or
|
||||
* the dealer index is out of bounds.
|
||||
*/
|
||||
public Player getDealer() {
|
||||
PlayerId id = playerOrder.get(dealerIndex);
|
||||
@@ -588,8 +580,8 @@ public class GameState {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified player has folded by retrieving their Player object
|
||||
* and checking their folded status.
|
||||
* Checks if the specified player has folded by retrieving their Player object and checking
|
||||
* their folded status.
|
||||
*
|
||||
* @param playerId the ID of the player to check for folded status.
|
||||
* @return true if the player has folded, false otherwise.
|
||||
|
||||
+8
-6
@@ -50,7 +50,8 @@ public class GameControllerTest {
|
||||
/**
|
||||
* 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) {
|
||||
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.
|
||||
*
|
||||
* @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) {
|
||||
game.playerFold(currentPlayerId(game));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the preflop phase ends once all active (non-folded) players
|
||||
* have completed their required actions for the current betting round.
|
||||
* Verifies that the preflop phase ends once all active (non-folded) players have completed
|
||||
* their required actions for the current betting round.
|
||||
*/
|
||||
@Test
|
||||
public void testPreflopEndsAfterOneActionPerActivePlayer() {
|
||||
@@ -121,8 +123,8 @@ public class GameControllerTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that during the preflop phase, folded players are excluded
|
||||
* from progression checks and do not block phase advancement.
|
||||
* Ensures that during the preflop phase, folded players are excluded from progression checks
|
||||
* and do not block phase advancement.
|
||||
*/
|
||||
@Test
|
||||
public void testPreflopCountsOnlyNonFoldedPlayersForProgress() {
|
||||
|
||||
Reference in New Issue
Block a user