java.lang.Object
it.polimi.ingsw.am13.model.player.Player
All Implemented Interfaces:
PlayerIF, Serializable

public class Player extends Object implements Serializable, PlayerIF
This class manages a player of match, storing and managing his nickname, token, field, hand cards, personal objective, starter card, points, whether he is connected or not.
See Also:
  • Field Details

    • playerLobby

      private final PlayerLobby playerLobby
      Player decontextualized from game. It stores nickname and token chosen by the player
    • field

      private final Field field
      Player's field which represents the positioning of his played cards.
    • handCards

      private final List<CardPlayable> handCards
      Player's hand of cards, which could be Gold cards or Resource cards.
    • personalObjective

      private CardObjective personalObjective
      Player's personal objective. It affects only player's points when the game ends.
    • possiblePersonalObjectives

      private final List<CardObjective> possiblePersonalObjectives
      The two objective cards among which the player can choose his personal objective. They can't change once they have been set.
    • startCard

      private CardStarter startCard
      Player's starter card which has been drawn to him. It can't change once it has been set.
    • points

      private int points
      Player's points counter. Initialized with 0
    • objectivePoints

      private int objectivePoints
      The points of the player that are given by objective cards. They are calculated at the end and are only used to determine the winner when they both have the same total points.
    • isConnected

      private boolean isConnected
      Whether a player is currently connected or not (for the corresponding Advanced Function)
  • Constructor Details

    • Player

      public Player(String nick, Token token)
      Constructor for the Player class. It initializes nickname, token, points and starter card (it has to be drawn before creating a Player object). It also initializes an empty field for the player and the space to store the player's hand.
      Parameters:
      nick - Nickname chosen by the player.
      token - Token color chosen for the player.
    • Player

      public Player(PlayerLobby playerLobby)
      Constructor for the Player class. It initializes nickname, token, points and starter card (it has to be drawn before creating a Player object). It also initializes an empty field for the player and the space to store the player's hand.
      Parameters:
      playerLobby - Player decontextualized from game. It has nickname and token already set.
  • Method Details

    • disconnectPlayer

      public void disconnectPlayer() throws ConnectionException
      Sets the isConnected variable to false
      Throws:
      ConnectionException - if the isConnected variable was already false (repeated disconnection)
    • reconnectPlayer

      public void reconnectPlayer() throws ConnectionException
      Sets the isConnected variable to true
      Throws:
      ConnectionException - if the isConnected variable was already true (repeated reconnection)
    • isConnected

      public boolean isConnected()
      Specified by:
      isConnected in interface PlayerIF
      Returns:
      whether a player is currently connected or not
    • initStarter

      public void initStarter(CardStarter cardStr) throws VariableAlreadySetException
      Sets the drawn starter card for this player. Once set, it can't be modified!
      Parameters:
      cardStr - Starter card already drawn.
      Throws:
      VariableAlreadySetException - Exception is thrown if this method has been called before.
    • initPossiblePersonalObjectives

      public void initPossiblePersonalObjectives(CardObjective possiblePersonalObjective0, CardObjective possiblePersonalObjective1) throws VariableAlreadySetException
      Sets the two objective cards among which the player can choose his personal objective. Once set, it can't be modified
      Parameters:
      possiblePersonalObjective0 - first possible personal objective cards
      possiblePersonalObjective1 - first possible personal objective cards
      Throws:
      VariableAlreadySetException - if this method has been called before
    • initObjective

      public void initObjective(CardObjectiveIF cardObj) throws VariableAlreadySetException, InvalidChoiceException
      Sets the player's personal objective. Once set, it can't be modified!
      Parameters:
      cardObj - Objective card which will be set as personal objective.
      Throws:
      VariableAlreadySetException - Exception is thrown if this method has been called before.
      InvalidChoiceException - if the objective card does not belong to the list of the possible objective cards
    • playStarter

      public void playStarter(Side side) throws InvalidPlayCardException, InvalidChoiceException
      Play the corresponding given side of the starter card on the player's field.
      Parameters:
      side - Starter card's side chosen by the player
      Throws:
      InvalidPlayCardException - Positioning error of the card at coordinates (0,0), or startCard is not yet initialized.
      InvalidChoiceException
    • playCard

      public void playCard(CardSidePlayableIF sideToPlay, Coordinates coordToPlay) throws InvalidPlayCardException, RequirementsNotMetException
      Play a card side of the player's hand on the field at given coordinates. If the card can't be played an exception is thrown.
      Parameters:
      sideToPlay - Card side to be played on the field.
      coordToPlay - Coordinates where the card side has to be played on the field.
      Throws:
      InvalidPlayCardException - Positioning error of the card at given coordinates, or startCard is not yet initialized.
      RequirementsNotMetException - At least one Requirement is not satisfied for the given card.
    • addCardToHand

      public void addCardToHand(CardPlayable card) throws PlayerHandException
      Add a card to player's hand. An exception is thrown if the hand is full.
      Parameters:
      card - Card to be added to the hand.
      Throws:
      PlayerHandException - Exception thrown when the hand is already full (it should never happen!)
    • addPoints

      public void addPoints(int increment)
      Increment player's points counter by the given amount.
      Parameters:
      increment - Number of points to add.
    • addObjectivePoints

      public void addObjectivePoints(CardObjective common1, CardObjective common2)
      Calculate and add points for the personal and common objectives.
      Parameters:
      common1 - First common objective.
      common2 - Second common objective.
    • getPlayerLobby

      public PlayerLobby getPlayerLobby()
      Specified by:
      getPlayerLobby in interface PlayerIF
      Returns:
      Player decontextualized from game. It stores nickname and token chosen by the player
    • getStarter

      public CardStarter getStarter()
      Specified by:
      getStarter in interface PlayerIF
      Returns:
      Starter card of the player. Null if player has not been assigned a starter card yet
    • getPoints

      public int getPoints()
      Specified by:
      getPoints in interface PlayerIF
      Returns:
      Player's points counter.
    • getHandCards

      public List<CardPlayable> getHandCards()
      Specified by:
      getHandCards in interface PlayerIF
      Returns:
      Player's hand of cards, which could be Gold cards or Resource cards.
    • getPersonalObjective

      public CardObjective getPersonalObjective()
      Specified by:
      getPersonalObjective in interface PlayerIF
      Returns:
      Player's personal objective. It affects only player's points when the game ends. Null if the personal objective hasn't been initialized yet
    • getPossiblePersonalObjectives

      public List<CardObjective> getPossiblePersonalObjectives()
      Specified by:
      getPossiblePersonalObjectives in interface PlayerIF
      Returns:
      the two objective cards among which the player can choose his personal objective. Null if they have not been set yet
    • getAvailableCoord

      public List<Coordinates> getAvailableCoord()
      Returns:
      The list of coordinates in which new cards can be played. If game has not started yet, the list is empty
    • getField

      public FieldIF getField()
      Specified by:
      getField in interface PlayerIF
      Returns:
      Player's field
    • getObjectivePoints

      public int getObjectivePoints()
      Returns:
      the points of the player that are given by objective cards