# Class Game

java.lang.Object
it.polimi.ingsw.is25am31.model.Game

public class Game extends Object
Represents a Galaxy Trucker game session. Manages game state, players, components, and game flow according to Galaxy Trucker rules. Handles initialization, ship building, flight phase, card effects, and scoring

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Game()`
  Constructs a new Game with default settings Initializes player list and default max players to 5

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `abandonRace(int playerId)`
  Removes player from current race
  `boolean`
  `addComponentToShip(int playerId, int line, int column, Component component)`
  Adds component to player's ship
  `void`
  `addPlayer(int id, String name)`
  Adds a player with unique id and nickname to the game
  `int`
  `applyCardEffect(Player player, Map<String,Object> parameters)`
  Applies current card's effect to player
  `void`
  `assignRocketPositionsToFinishedPlayers(List<Player> finishingOrder)`
  Assigns starting positions based on build completion order
  `boolean`
  `automaticEngineLost(int playerId)`
  Checks and handles engine loss elimination
  `int`
  `automaticLost(int playerId)`
  Automatically eliminates player if conditions met
  `boolean`
  `canContinuePlaying(Player player)`
  Checks if player can continue racing
  `void`
  `changeRocketPosition(Player player, int change)`
  Changes a player's rocket position with collision avoidance logic
  `Map<Player,Integer>`
  `checkDeck()`
  Checks if deck is empty (ends flight phase)
  `void`
  `completeAssembly(int playerId)`
  Completes ship assembly and starts flight if all ready
  `Card`
  `drawCard(int playerId)`
  Draws next card for flight phase
  `Component`
  `drawComponent()`
  Draws a random face-down component
  `void`
  `drawFaceUpComponent(Component component)`
  Draws a specific face-up component
  `List<Card>`
  `drawSubDeck(int index)`
  Views a sub-deck of cards
  `Map<Player,Integer>`
  `endFlightPhase()`
  Ends flight phase and calculates scores
  `Component`
  `getComponent(int id)`
  Gets component by ID
  `Card`
  `getCurrentCard()`
  Gets current active card
  `Phase`
  `getCurrentPhase()`
  Gets the current phase of the game
  `Player`
  `getCurrentPlayer()`
  `int`
  `getDiceResult()`
  Gets last dice roll result
  `List<Player>`
  `getEliminatedPlayers()`
  Gets players eliminated from the current race
  `List<Component>`
  `getFaceUpComponentList()`
  Lists all face-up components
  `List<Player>`
  `getFinishingOrder()`
  Gets ship completion order
  `Gameboard`
  `getGameboard()`
  Gets the game board
  `int`
  `getLeader()`
  Gets current race leader ID
  `int`
  `getLevel()`
  Gets current game difficulty level
  `int`
  `getMaxPlayers()`
  Gets maximum allowed players.
  `Player`
  `getPlayer(int id)`
  Finds a player by id among active and eliminated
  `List<Player>`
  `getPlayers()`
  `List<Player>`
  `getRemainingPlayers()`
  Gets players still actively participating in the race
  `Component`
  `getReservedComponent(Player player, int index)`
  Retrieves a reserved component from a player's ship
  `Map<Integer,Integer>`
  `getUpdatedPositions()`
  Gets current rocket positions
  `void`
  `handleCrewRequest(int playerId, int x, int y, AlienColor alienColor)`
  Handles crew/alien placement request
  `void`
  `initializeCrew(Player player)`
  Initializes crew members for player
  `void`
  `initializeShip()`
  Initializes ships for all players and assigns initial crew
  `void`
  `instantiateRocketForPlayer(int playerId)`
  Creates rocket for player
  `boolean`
  `isGameStarted()`
  Checks if the game has started by verifying maxPlayers has changed from default
  `void`
  `orderPlayers()`
  Orders players by rocket position (descending)
  `void`
  `orderPlayersReverse()`
  Orders players by rocket position (ascending)
  `void`
  `passTurn()`
  Passes turn to next player
  `void`
  `putComponentBack(Component component)`
  Returns a component to the board face-up
  `void`
  `removeAlien(int playerId, int x, int y)`
  Removes alien from cabin
  `boolean`
  `removeComponentFromShip(int playerId, int line, int column)`
  Removes component from player's ship
  `void`
  `resetDiceResult()`
  Resets dice result to 0.
  `int`
  `rollDice(int playerId)`
  Rolls dice for card effect resolution
  `void`
  `sellGoodsPhase()`
  Converts all goods to credits for remaining players
  `int`
  `setCardContext(Player player, Map<String,Object> parameters)`
  Sets context for current card
  `void`
  `setCurrentCard(Card card)`
  Sets the current event card in play
  `void`
  `setCurrentPhase(Phase phase)`
  Sets current game phase
  `void`
  `setCurrentPlayer(Player currentPlayer)`
  `void`
  `setLevel(int level)`
  Sets the game difficulty level and initializes game components
  `void`
  `setMaxPlayers(int maxPlayers)`
  Sets allowed number of players (2-4)
  `void`
  `setPosition(Player player, int position)`
  Sets a player's rocket starting position based on their finishing order
  `void`
  `shuffleCards()`
  Shuffles all card decks together
  `void`
  `startFlightPhase()`
  `Ship`
  `viewShip(int id)`
  Returns the Ship object for given player id

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Game

    public Game()
    Constructs a new Game with default settings Initializes player list and default max players to 5

- ## Method Details

  - ### setLevel

    public void setLevel(int level)
    Sets the game difficulty level and initializes game components
    Parameters:
      `level` - Game difficulty level (1 or 2)

  - ### isGameStarted

    public boolean isGameStarted()
    Checks if the game has started by verifying maxPlayers has changed from default
    Returns:
      true if the game started; false otherwise

  - ### getRemainingPlayers

    public List<Player> getRemainingPlayers()
    Gets players still actively participating in the race
    Returns:
      list of players still active in the race

  - ### getEliminatedPlayers

    public List<Player> getEliminatedPlayers()
    Gets players eliminated from the current race
    Returns:
      list of players who have been eliminated

  - ### getLevel

    public int getLevel()
    Gets current game difficulty level
    Returns:
      Current game level (1 or 2)

  - ### setCurrentCard

    public void setCurrentCard(Card card)
    Sets the current event card in play
    Parameters:
      `card` - the card to set as current

  - ### changeRocketPosition

    public void changeRocketPosition(Player player, int change)
    Changes a player's rocket position with collision avoidance logic
    Parameters:
      `player` - the player whose rocket to move
      `change` - the amount to move the rocket (positive or negative)

  - ### setPosition

    public void setPosition(Player player, int position)
    Sets a player's rocket starting position based on their finishing order
    Parameters:
      `player` - the player to position
      `position` - the finishing position (1–4)
    Throws:
      `IllegalArgumentException` - if position is out of range
      `RuntimeException` - if the chosen position is already occupied

  - ### rollDice

    public int rollDice(int playerId)
    Rolls dice for card effect resolution
    Parameters:
      `playerId` - ID of rolling player
    Returns:
      Dice roll result (2-12)
    Throws:
      `IllegalStateException` - If not permitted to roll

  - ### getDiceResult

    public int getDiceResult()
    Gets last dice roll result
    Returns:
      Current dice result

  - ### resetDiceResult

    public void resetDiceResult()
    Resets dice result to 0.

  - ### setCurrentPhase

    public void setCurrentPhase(Phase phase)
    Sets current game phase
    Parameters:
      `phase` - New game phase

  - ### getGameboard

    public Gameboard getGameboard()
    Gets the game board
    Returns:
      Current game board

  - ### setMaxPlayers

    public void setMaxPlayers(int maxPlayers)
    Sets allowed number of players (2-4)
    Parameters:
      `maxPlayers` - number of players
    Throws:
      `IllegalArgumentException` - if out of range

  - ### getMaxPlayers

    public int getMaxPlayers()
    Gets maximum allowed players.
    Returns:
      Max player count

  - ### addPlayer

    public void addPlayer(int id, String name) throws NameErrorException
    Adds a player with unique id and nickname to the game
    Parameters:
      `id` - unique player id
      `name` - desired nickname
    Throws:
      `NameErrorException` - if name already taken
      `IllegalStateException` - if id already used
      `IllegalArgumentException` - if max players reached

  - ### getPlayers

    public List<Player> getPlayers()

  - ### getCurrentPlayer

    public Player getCurrentPlayer()

  - ### viewShip

    public Ship viewShip(int id)
    Returns the Ship object for given player id
    Parameters:
      `id` - player id
    Returns:
      Ship or null if not found

  - ### getPlayer

    public Player getPlayer(int id)
    Finds a player by id among active and eliminated
    Parameters:
      `id` - player id
    Returns:
      Player or null if none

  - ### initializeShip

    public void initializeShip()
    Initializes ships for all players and assigns initial crew

  - ### getReservedComponent

    public Component getReservedComponent(Player player, int index)
    Retrieves a reserved component from a player's ship
    Parameters:
      `player` - the Player
      `index` - the component index
    Returns:
      the Component reserved

  - ### getCurrentPhase

    public Phase getCurrentPhase()
    Gets the current phase of the game
    Returns:
      the Phase enum

  - ### drawComponent

    public Component drawComponent()
    Draws a random face-down component
    Returns:
      Drawn component

  - ### getComponent

    public Component getComponent(int id)
    Gets component by ID
    Parameters:
      `id` - Component ID
    Returns:
      Requested component

  - ### getFaceUpComponentList

    public List<Component> getFaceUpComponentList()
    Lists all face-up components
    Returns:
      List of face-up components

  - ### putComponentBack

    public void putComponentBack(Component component)
    Returns a component to the board face-up
    Parameters:
      `component` - Component to return

  - ### drawFaceUpComponent

    public void drawFaceUpComponent(Component component)
    Draws a specific face-up component
    Parameters:
      `component` - Component to draw

  - ### addComponentToShip

    public boolean addComponentToShip(int playerId, int line, int column, Component component)
    Adds component to player's ship
    Parameters:
      `playerId` - Player ID
      `line` - Grid row
      `column` - Grid column
      `component` - Component to add
    Returns:
      true if successful, false otherwise

  - ### removeComponentFromShip

    public boolean removeComponentFromShip(int playerId, int line, int column)
    Removes component from player's ship
    Parameters:
      `playerId` - Player ID
      `line` - Component row
      `column` - Component column
    Returns:
      true if successful, false otherwise

  - ### instantiateRocketForPlayer

    public void instantiateRocketForPlayer(int playerId)
    Creates rocket for player
    Parameters:
      `playerId` - Player ID

  - ### completeAssembly

    public void completeAssembly(int playerId)
    Completes ship assembly and starts flight if all ready
    Parameters:
      `playerId` - Player ID

  - ### getFinishingOrder

    public List<Player> getFinishingOrder()
    Gets ship completion order
    Returns:
      Players in build finish order

  - ### assignRocketPositionsToFinishedPlayers

    public void assignRocketPositionsToFinishedPlayers(List<Player> finishingOrder)
    Assigns starting positions based on build completion order
    Parameters:
      `finishingOrder` - Players in build completion order

  - ### drawSubDeck

    public List<Card> drawSubDeck(int index)
    Views a sub-deck of cards
    Parameters:
      `index` - Sub-deck index (0-2)
    Returns:
      Cards in sub-deck
    Throws:
      `RuntimeException` - If invalid index or wrong game level

  - ### canContinuePlaying

    public boolean canContinuePlaying(Player player)
    Checks if player can continue racing
    Parameters:
      `player` - Player to check
    Returns:
      true if player can continue, false otherwise

  - ### startFlightPhase

    public void startFlightPhase()

  - ### shuffleCards

    public void shuffleCards()
    Shuffles all card decks together

  - ### checkDeck

    public Map<Player,Integer> checkDeck()
    Checks if deck is empty (ends flight phase)
    Returns:
      Final scores if deck empty, null otherwise

  - ### initializeCrew

    public void initializeCrew(Player player)
    Initializes crew members for player
    Parameters:
      `player` - Player to initialize

  - ### handleCrewRequest

    public void handleCrewRequest(int playerId, int x, int y, AlienColor alienColor)
    Handles crew/alien placement request
    Parameters:
      `playerId` - Player ID
      `x` - Cabin row
      `y` - Cabin column
      `alienColor` - Alien color (NONE for crew)
    Throws:
      `IllegalArgumentException` - For invalid player or position
      `RuntimeException` - For invalid placement rules

  - ### removeAlien

    public void removeAlien(int playerId, int x, int y)
    Removes alien from cabin
    Parameters:
      `playerId` - Player ID
      `x` - Cabin row
      `y` - Cabin column
    Throws:
      `IllegalArgumentException` - For invalid cabin

  - ### orderPlayers

    public void orderPlayers()
    Orders players by rocket position (descending)

  - ### orderPlayersReverse

    public void orderPlayersReverse()
    Orders players by rocket position (ascending)

  - ### getLeader

    public int getLeader()
    Gets current race leader ID
    Returns:
      Leader player ID

  - ### drawCard

    public Card drawCard(int playerId)
    Draws next card for flight phase
    Parameters:
      `playerId` - Drawing player ID
    Returns:
      Drawn card
    Throws:
      `RuntimeException` - If not leader's turn

  - ### setCurrentPlayer

    public void setCurrentPlayer(Player currentPlayer)

  - ### passTurn

    public void passTurn()
    Passes turn to next player

  - ### applyCardEffect

    public int applyCardEffect(Player player, Map<String,Object> parameters)
    Applies current card's effect to player
    Parameters:
      `player` - Target player
      `parameters` - Effect parameters
    Returns:
      Application result code
    Throws:
      `IllegalStateException` - If no card or wrong turn

  - ### setCardContext

    public int setCardContext(Player player, Map<String,Object> parameters)
    Sets context for current card
    Parameters:
      `player` - Acting player
      `parameters` - Context parameters
    Returns:
      Validation result code

  - ### getCurrentCard

    public Card getCurrentCard()
    Gets current active card
    Returns:
      Current card

  - ### abandonRace

    public void abandonRace(int playerId)
    Removes player from current race
    Parameters:
      `playerId` - Player to abandon

  - ### sellGoodsPhase

    public void sellGoodsPhase()
    Converts all goods to credits for remaining players

  - ### endFlightPhase

    public Map<Player,Integer> endFlightPhase()
    Ends flight phase and calculates scores
    Returns:
      Final player scores

  - ### automaticLost

    public int automaticLost(int playerId)
    Automatically eliminates player if conditions met
    Parameters:
      `playerId` - Player to check
    Returns:
      Status code (0=no action, 2=no crew, 3=too far behind)

  - ### automaticEngineLost

    public boolean automaticEngineLost(int playerId)
    Checks and handles engine loss elimination
    Parameters:
      `playerId` - Player to check
    Returns:
      true if eliminated, false otherwise

  - ### getUpdatedPositions

    public Map<Integer,Integer> getUpdatedPositions()
    Gets current rocket positions
    Returns:
      Map of player IDs to positions