Class Game
- All Implemented Interfaces:
Serializable
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BoardThe board associated with this game.private CurrentStateThe current state of the game.Map tracking the players who are currently disconnected.private intThe configured number of players for this game.The queue of players involved in optional card resolution.private OrderLogicCardThe order logic card associated with this game.The list of players participating in the game.The final ranking of players at the end of the game.The mapping between slots and the players assigned to them.Queue containing the players who still have to choose their totem. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAttempts to add the specified player to the game.voidClears the collection of disconnected players.booleandisconnectedPlayer(Player player) Marks the specified player as disconnected and updates the game flow accordingly.booleandrawLowerBuildingCardByIndex(Player player, int cardIndex) Attempts to draw the lower building card at the specified index for the specified player.booleandrawLowerTribeCardByIndex(Player player, int cardIndex) Attempts to draw the lower tribe card at the specified index for the specified player.booleandrawUpperBuildingCardByIndex(Player player, int cardIndex) Attempts to draw the upper building card at the specified index for the specified player.booleandrawUpperTribeCardByIndex(Player player, int cardIndex) Attempts to draw the upper tribe card at the specified index for the specified player.private voidendGame()Ends the game by applying all final building effects owned by each player and updating the game stage toENDED.voidEnds the game by forfeit and determines the final player standing.private voidResolves all pending event cards if the current game stage isRESOLVING_EVENT.Returns the list of totems that have not yet been assigned to any player.getBoard()Returns the game board.intReturns the current number of players participating in the game.Returns the current state of the game.Returns an unmodifiable view of the disconnected-players map.Returns a copy of the lower building cards currently available on the board.Returns a copy of the lower tribe cards currently available on the board.intReturns the configured number of players for this game.Returns the order logic card associated with this game.getPlayerByUsername(String username) Returns the player with the specified username, if present.Returns the list of players participating in the game.Returns the final ranking of players.Returns the map of slots assigned to players.Returns a copy of the upper building cards currently available on the board.Returns a copy of the upper tribe cards currently available on the board.private booleanChecks whether there are any drawable lower tribe cards on the board, i.e. lower tribe cards that are not event cards.private booleanChecks whether there are any drawable upper tribe cards on the board, i.e. upper tribe cards that are not event cards.voidinit()Initializes the game after all required players have been added.private voidPrepares the next player and updates the game state according to the current game stage.private voidAdvances the game to the next round.booleanreconnectPlayer(Player player) Marks the specified player as reconnected.booleansetNPlayer(int nPlayers) Sets the configured number of players for this game.booleanSkips the turn for the specified player when no drawable cards are available.booleanslotChoiceByIndex(Player player, int slotIndex) Attempts to assign the slot at the specified index to the specified player.booleantotemChoice(Player player, Totems totem) Assigns the selected totem to the specified player during the totem choice phase.private voidTransitions the game to the optional card phase or directly to the next round.
-
Field Details
-
playerStanding
-
totemChoiceQueue
-
disconnectedPlayers
-
playersList
-
currentState
The current state of the game. -
slotMap
-
nPlayers
private int nPlayersThe configured number of players for this game. -
optionalCardQueue
-
orderLogicCard
The order logic card associated with this game. -
board
The board associated with this game.
-
-
Constructor Details
-
Game
Creates a game with the specified number of players.- Parameters:
nPlayers- the configured number of players for the game.- Throws:
IllegalArgumentException- ifnPlayers < 0ornPlayers > 5.
-
Game
public Game()Creates a game with 0 configured players.
-
-
Method Details
-
getPlayerStanding
-
getPlayers
-
getAvailableTotems
-
totemChoice
Assigns the selected totem to the specified player during the totem choice phase.The choice is accepted only if the game is currently in the
GameStages.TOTEM_CHOICEstage, the player is the current one, and the selected totem is still available.After a valid choice, the method advances to the next player in the queue. If all players have completed the selection, the game moves to the slot choice phase. Disconnected players are automatically assigned a random available totem.
- Parameters:
player- the player making the totem choice.totem- the selected totem.- Returns:
trueif the choice is applied successfully,falseotherwise.
-
getDisconnectedPlayers
-
disconnectedPlayer
Marks the specified player as disconnected and updates the game flow accordingly.If the player disconnects during the waiting phase, they are removed from the player list and from the totem choice queue. If the disconnected player is the current one, the game advances to the next suitable player or, during the totem choice phase, handles the remaining selection flow automatically.
- Parameters:
player- the player who disconnected.- Returns:
trueif the disconnection is handled successfully,falseif the player was already marked as disconnected.
-
reconnectPlayer
Marks the specified player as reconnected.If the player reconnects during the slot choice phase, they are removed from the disconnected players map and reinserted into the order logic card when necessary.
- Parameters:
player- the player who reconnected.- Returns:
trueif the reconnection is handled successfully,falseif the player was not previously marked as disconnected.
-
clearDisconnected
public void clearDisconnected()Clears the collection of disconnected players. -
getCurrentPlayerNumber
public int getCurrentPlayerNumber()Returns the current number of players participating in the game.- Returns:
- the current number of players.
-
getSlotMap
-
getOrderLogicCard
Returns the order logic card associated with this game.- Returns:
- the order logic card.
-
getBoard
-
getUpperListTribeCards
-
getLowerListTribeCards
-
getUpperListBuilding
Returns a copy of the upper building cards currently available on the board.- Returns:
- a new list containing the upper building cards currently on the board.
-
getLowerListBuilding
Returns a copy of the lower building cards currently available on the board.- Returns:
- a new list containing the lower building cards currently on the board.
-
getCurrentState
Returns the current state of the game.- Returns:
- the current state of the game.
-
getPlayerByUsername
Returns the player with the specified username, if present.- Parameters:
username- the username of the player to search for.- Returns:
- the player with the specified username, or
nullif no such player exists. - Throws:
IndexOutOfBoundsException- if an index access error occurs.
-
getNPlayers
public int getNPlayers()Returns the configured number of players for this game.- Returns:
- the configured number of players for this game.
-
addPlayer
Attempts to add the specified player to the game. The operation succeeds only if the configured number of players is not 0, the current game stage isWAITING, and the player is not already present. If the number of players reaches the configured maximum, the game is initialized.- Parameters:
player- the player to add to the game.- Returns:
trueif the player is successfully added,falseotherwise.
-
init
public void init()Initializes the game after all required players have been added. The method creates the appropriate order logic card according to the number of players, selects the first current player, and updates the game stage toSLOT_CHOICE. -
slotChoiceByIndex
Attempts to assign the slot at the specified index to the specified player. The operation succeeds only if the index is valid, the current game stage isSLOT_CHOICE, the specified player is the current player, and the selected slot is not already assigned. If the slot is successfully assigned, the next player setup is triggered.- Parameters:
player- the player performing the slot choice.slotIndex- the index of the selected slot.- Returns:
trueif the slot choice succeeds,falseotherwise.
-
drawUpperTribeCardByIndex
Attempts to draw the upper tribe card at the specified index for the specified player. The operation succeeds only if the index is valid, the game stage isRESOLVING_ACTIONS, the specified player is the current player, at least one upper card draw is still available, and the selected tribe card is not an event card. If successful, the card is inserted into the player's collection, removed from the board, and the number of remaining upper draws is decremented. If both upper and lower draws become zero, the next player setup is triggered.- Parameters:
player- the player performing the draw.cardIndex- the index of the upper tribe card to draw.- Returns:
trueif the draw succeeds,falseotherwise.
-
skipTurn
Skips the turn for the specified player when no drawable cards are available. The operation succeeds only if the current game stage isRESOLVING_ACTIONS, the specified player is the current player, and the player can't draw tribe card (i.e. all remaining lower tribe cards are event cards).- Parameters:
player- the player skipping the turn .- Returns:
trueif the skip succeeds,falseotherwise.
-
drawLowerTribeCardByIndex
Attempts to draw the lower tribe card at the specified index for the specified player. The operation succeeds only if the index is valid, the game stage isRESOLVING_ACTIONS, the specified player is the current player, at least one lower card draw is still available, and the selected tribe card is not an event card. If successful, the card is inserted into the player's collection, removed from the board, and the number of remaining lower draws is decremented. If both lower and upper draws become zero, the next player setup is triggered.- Parameters:
player- the player performing the draw.cardIndex- the index of the lower tribe card to draw.- Returns:
trueif the draw succeeds,falseotherwise.
-
drawUpperBuildingCardByIndex
Attempts to draw the upper building card at the specified index for the specified player. The operation succeeds only if the index is valid, the game stage isRESOLVING_ACTIONS, the specified player is the current player, at least one upper card draw is still available, and the selected building card can be bought by the player. If successful, the building card is removed from the board and the number of remaining upper draws is decremented. If both upper and lower draws become zero, the next player setup is triggered.- Parameters:
player- the player performing the draw.cardIndex- the index of the upper building card to draw.- Returns:
trueif the draw succeeds,falseotherwise.
-
drawLowerBuildingCardByIndex
Attempts to draw the lower building card at the specified index for the specified player. The operation succeeds only if the index is valid, the game stage isRESOLVING_ACTIONS, the specified player is the current player, at least one lower card draw is still available, and the selected building card can be bought by the player. If successful, the building card is removed from the board and the number of remaining lower draws is decremented. If both lower and upper draws become zero, the next player setup is triggered.- Parameters:
player- the player performing the draw.cardIndex- the index of the lower building card to draw.- Returns:
trueif the draw succeeds,falseotherwise.
-
nextPlayerSetup
private void nextPlayerSetup()Prepares the next player and updates the game state according to the current game stage. If the current stage isSLOT_CHOICE, the next player is taken from the order logic card. If no player is available, the game stage is updated toRESOLVING_ACTIONSand the first assigned slot is selected. If the current stage isRESOLVING_ACTIONS, the current player is pushed back into the order logic card, the current slot is freed, and the next assigned slot is selected. If no assigned slots remain, the game stage is updated toOPTIONAL_CARD_EFFECT, the optional card queue is built from players owning building cards with effect id equal to 12, and the first player in that queue is selected. If no player is available for optional card resolution, the game stage is updated toRESOLVING_EVENT; then, if the round number is less than 10, the next round is prepared, otherwise event resolution is performed, the game stage is updated toENDING, and the game is ended. If the current stage isOPTIONAL_CARD_EFFECT, the next player is taken from the optional card queue. If no player is available, the game stage is updated toRESOLVING_EVENT. -
transitionToOptionalOrNextRound
private void transitionToOptionalOrNextRound()Transitions the game to the optional card phase or directly to the next round.Builds a queue of non-disconnected players who own Building 12 (optional card effect). If such players exist, the first one is set as the current player for their optional action. Otherwise, if the round counter is below 10, the next round begins and the game stage is set to
GameStages.SLOT_CHOICE; if round 10 has been completed, the game ends. -
hasDrawableUp
private boolean hasDrawableUp()Checks whether there are any drawable upper tribe cards on the board, i.e. upper tribe cards that are not event cards.- Returns:
trueif at least one non-event upper tribe card is available,falseotherwise.
-
hasDrawableDown
private boolean hasDrawableDown()Checks whether there are any drawable lower tribe cards on the board, i.e. lower tribe cards that are not event cards.- Returns:
trueif at least one non-event lower tribe card is available,falseotherwise.
-
eventResolution
private void eventResolution()Resolves all pending event cards if the current game stage isRESOLVING_EVENT. All pending events are activated on the player list. Event cards of typeSUSTENANCEare resolved after all other pending events. -
nextRound
private void nextRound()Advances the game to the next round. The method first resolves pending events. If the current round is 10, the game stage is updated toENDINGand the game is ended. Otherwise, the era is updated if the board changes era, and the round number is incremented. -
endGame
private void endGame()Ends the game by applying all final building effects owned by each player and updating the game stage toENDED. -
setNPlayer
public boolean setNPlayer(int nPlayers) Sets the configured number of players for this game. The operation succeeds only if the current configured number of players is 0.- Parameters:
nPlayers- the new configured number of players.- Returns:
trueif the number of players is updated,falseotherwise.
-
endGameForfeit
public void endGameForfeit()Ends the game by forfeit and determines the final player standing.The player who is still connected is declared the winner and placed in the first position of the final ranking. The remaining players are ordered by prestige value and, in case of a tie, by food value.
-