Class Game

java.lang.Object
it.polimi.ingsw.gc14.Model.Game
All Implemented Interfaces:
Serializable

public class Game extends Object implements Serializable
Represents the main game model. A Game object stores the players, the current state of the match, the slot assignments, the board, and the logic required to manage the game flow.
See Also:
  • Field Details

    • playerStanding

      private ArrayList<Player> playerStanding
      The final ranking of players at the end of the game.
    • totemChoiceQueue

      private final Queue<Player> totemChoiceQueue
      Queue containing the players who still have to choose their totem.
    • disconnectedPlayers

      private final Map<Player,Boolean> disconnectedPlayers
      Map tracking the players who are currently disconnected. Key: player; value: true if currently disconnected, false if reconnected.
    • playersList

      private ArrayList<Player> playersList
      The list of players participating in the game.
    • currentState

      private CurrentState currentState
      The current state of the game.
    • slotMap

      private HashMap<Slot,Player> slotMap
      The mapping between slots and the players assigned to them.
    • nPlayers

      private int nPlayers
      The configured number of players for this game.
    • optionalCardQueue

      private Queue<Player> optionalCardQueue
      The queue of players involved in optional card resolution.
    • orderLogicCard

      private OrderLogicCard orderLogicCard
      The order logic card associated with this game.
    • board

      private Board board
      The board associated with this game.
  • Constructor Details

    • Game

      public Game(int nPlayers) throws IllegalArgumentException
      Creates a game with the specified number of players.
      Parameters:
      nPlayers - the configured number of players for the game.
      Throws:
      IllegalArgumentException - if nPlayers < 0 or nPlayers > 5.
    • Game

      public Game()
      Creates a game with 0 configured players.
  • Method Details

    • getPlayerStanding

      public ArrayList<Player> getPlayerStanding()
      Returns the final ranking of players.
      Returns:
      the list of players ordered according to their final standing.
    • getPlayers

      public List<Player> getPlayers()
      Returns the list of players participating in the game.
      Returns:
      a copy of the list of players currently participating in the game.
    • getAvailableTotems

      public List<Totems> getAvailableTotems()
      Returns the list of totems that have not yet been assigned to any player.
      Returns:
      the list of currently available totems.
    • totemChoice

      public boolean totemChoice(Player player, Totems totem)
      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_CHOICE stage, 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:
      true if the choice is applied successfully, false otherwise.
    • getDisconnectedPlayers

      public Map<Player,Boolean> getDisconnectedPlayers()
      Returns an unmodifiable view of the disconnected-players map.
      Returns:
      map from player to disconnection status.
    • disconnectedPlayer

      public boolean disconnectedPlayer(Player player)
      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:
      true if the disconnection is handled successfully, false if the player was already marked as disconnected.
    • reconnectPlayer

      public boolean reconnectPlayer(Player player)
      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:
      true if the reconnection is handled successfully, false if 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

      public HashMap<Slot,Player> getSlotMap()
      Returns the map of slots assigned to players.
      Returns:
      the map that associates each slot with the corresponding player.
    • getOrderLogicCard

      public OrderLogicCard getOrderLogicCard()
      Returns the order logic card associated with this game.
      Returns:
      the order logic card.
    • getBoard

      public Board getBoard()
      Returns the game board.
      Returns:
      the board associated with the game.
    • getUpperListTribeCards

      public ArrayList<TribeCard> getUpperListTribeCards()
      Returns a copy of the upper tribe cards currently available on the board.
      Returns:
      a new list containing the upper tribe cards currently on the board.
    • getLowerListTribeCards

      public ArrayList<TribeCard> getLowerListTribeCards()
      Returns a copy of the lower tribe cards currently available on the board.
      Returns:
      a new list containing the lower tribe cards currently on the board.
    • getUpperListBuilding

      public ArrayList<BuildingCard> 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

      public ArrayList<BuildingCard> 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

      public CurrentState getCurrentState()
      Returns the current state of the game.
      Returns:
      the current state of the game.
    • getPlayerByUsername

      public Player getPlayerByUsername(String username) throws IndexOutOfBoundsException
      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 null if 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

      public boolean addPlayer(Player player)
      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 is WAITING, 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:
      true if the player is successfully added, false otherwise.
    • 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 to SLOT_CHOICE.
    • slotChoiceByIndex

      public boolean slotChoiceByIndex(Player player, int slotIndex)
      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 is SLOT_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:
      true if the slot choice succeeds, false otherwise.
    • drawUpperTribeCardByIndex

      public boolean drawUpperTribeCardByIndex(Player player, int cardIndex)
      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 is RESOLVING_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:
      true if the draw succeeds, false otherwise.
    • skipTurn

      public boolean skipTurn(Player player)
      Skips the turn for the specified player when no drawable cards are available. The operation succeeds only if the current game stage is RESOLVING_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:
      true if the skip succeeds, false otherwise.
    • drawLowerTribeCardByIndex

      public boolean drawLowerTribeCardByIndex(Player player, int cardIndex)
      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 is RESOLVING_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:
      true if the draw succeeds, false otherwise.
    • drawUpperBuildingCardByIndex

      public boolean drawUpperBuildingCardByIndex(Player player, int cardIndex)
      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 is RESOLVING_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:
      true if the draw succeeds, false otherwise.
    • drawLowerBuildingCardByIndex

      public boolean drawLowerBuildingCardByIndex(Player player, int cardIndex)
      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 is RESOLVING_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:
      true if the draw succeeds, false otherwise.
    • nextPlayerSetup

      private void nextPlayerSetup()
      Prepares the next player and updates the game state according to the current game stage. If the current stage is SLOT_CHOICE, the next player is taken from the order logic card. If no player is available, the game stage is updated to RESOLVING_ACTIONS and the first assigned slot is selected. If the current stage is RESOLVING_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 to OPTIONAL_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 to RESOLVING_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 to ENDING, and the game is ended. If the current stage is OPTIONAL_CARD_EFFECT, the next player is taken from the optional card queue. If no player is available, the game stage is updated to RESOLVING_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:
      true if at least one non-event upper tribe card is available, false otherwise.
    • 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:
      true if at least one non-event lower tribe card is available, false otherwise.
    • eventResolution

      private void eventResolution()
      Resolves all pending event cards if the current game stage is RESOLVING_EVENT. All pending events are activated on the player list. Event cards of type SUSTENANCE are 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 to ENDING and 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 to ENDED.
    • 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:
      true if the number of players is updated, false otherwise.
    • 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.