Class Match

java.lang.Object
it.polimi.ingsw.am13.model.Match

public class Match extends Object
This class manages a match The flow of the match is handled by game phase. The methods to control the flow of the game are
  • Match(List): sets the game phase to null (match created, decks instantiated, but game not yet started)
  • startGame(): starts initialization phase (mainly draws first cards for each player), setting game phase to INIT.
  • playStarter(PlayerLobby, Side) and choosePersonalObjective(PlayerLobby, CardObjectiveIF): every time a player plays their starter card and chooses one of the 2 personal objective cards, these methods check if initialization has been completed, and eventually sets game phase to IN_GAME
  • nextTurn(): makes necessary checks for passing to FINAL_PHASE and ultimately to CALC_POINTS During this game phase, the only method which can change the internal state are pickCard(...) and playCard(...)
  • addObjectivePoints(): Calculates extra points given by objective cards for each player, and moves the game on to phase ENDED
  • calcWinner(): Only in this ultimate phase this method can be called, calculating which player has won
After game phase becomes ENDED, the internal state of Match and the model in general cannot change any longer.
  • Field Details

    • deckResources

      private final DeckHandler<CardResource> deckResources
      Deck handler of the resource cards deck
    • deckGold

      private final DeckHandler<CardGold> deckGold
      Deck handler of the gold cards deck
    • deckObjective

      private final DeckHandler<CardObjective> deckObjective
      Deck handler of the objective cards deck
    • deckStarter

      private final Deck<CardStarter> deckStarter
      Deck handler of the starter cards deck
    • players

      private final List<Player> players
      The players of the match. The list is created by building the entire class, and cannot change The size is >=2 and <=4, the colors of players are all different It includes all the players, even if they are disconnected
    • playersMap

      private final Map<PlayerLobby,Player> playersMap
      Map associating player decontextualized from game to the actual playing player
    • firstPlayer

      private final Player firstPlayer
      The first player of the match
    • firstPlayerIndex

      private final int firstPlayerIndex
      The index of the first player in the players list
    • currentPlayer

      private Player currentPlayer
      The player who is playing in the current turn. Null if the gameStatus is different from IN_GAME or FINAL_PHASE (the 2 phases divided in turns)
    • gameStatus

      private GameStatus gameStatus
      The status of the game
    • turnsToEnd

      private int turnsToEnd
      This variable keeps track of how many turns are left during the final phase of the game
    • countSetup

      private int countSetup
      This variable keeps track of how players have chosen their objective card and played their starter card
    • turnActionsCounter

      private int turnActionsCounter
      Count of the actions done by a player during their turn. It has a relevant value only during phase IN_GAME, FINAL_PHASE The possible values are referred to current player and are:
      • 0: Turn has started, player did nothing yet
      • 1: Player has played a card on their field, but has not picked any card
      • 2: Player picked a card after playing, now turn is ended and game can move on to another player
  • Constructor Details

    • Match

      public Match(List<Player> players) throws InvalidPlayersNumberException
      Initializes the match. It sets the game status to null (game not already really started), chooses the first player at random from the list of players, instantiates the four decks and sets the players list to the list it receives as a parameter.
      Parameters:
      players - the players of the match
      Throws:
      InvalidPlayersNumberException - If the number of players is not between 2 and 4, or the players have tokens of same colors
  • Method Details

    • disconnectPlayer

      public void disconnectPlayer(PlayerLobby player) throws ConnectionException, InvalidPlayerException
      Disconnects the given player. If this method is called before the player chooses the side in which he should play his starter card, or his secret objective, it assigns them to him.
      If this method is called when a player has played his card but not picked one yet, it assigns the first non-null pickable card to the player. If instead it is called at the beginning of the player's turn, the ghost turn after the player is disconnected is handled by this method. This way, at the end of the disconnection for the current player in turn-based phase, it is always possible to make the game move on via nextTurn()
      This method should not be called before game has actually starter Then it calls the corresponding method in the Player class. Note that the method only sets the player as "disconnected", but it does not remove the player themself from the game (that is the list of players for this match still includes the disconnected players).
      Throws:
      ConnectionException - if the player was already not connected when this method was called
      InvalidPlayerException - if player is not among this match's players
    • reconnectPlayer

      public void reconnectPlayer(PlayerLobby player) throws ConnectionException, InvalidPlayerException
      Reconnects the given player by calling the corresponding method in the Player class
      Throws:
      ConnectionException - if the player was already connected when this method was called
      InvalidPlayerException - if player is not among this match's players
    • fetchIsConnected

      public boolean fetchIsConnected(PlayerLobby player) throws InvalidPlayerException
      Parameters:
      player - one of the players of the match
      Returns:
      whether a player is currently connected or not
      Throws:
      InvalidPlayerException - if player is not among this match's players
    • countConnected

      public int countConnected()
      Returns:
      the number of players that are currently connected
    • fetchPickables

      public List<CardPlayable> fetchPickables()
      List of all visible cards (that are pickable during turn phases). The list is of size 6, with order: top of deck (with getVisibleSide()==Side.SIDEBACK), and 2 visible cards (with getVisibleSide()==Side.SIDEFRONT), and repetion of this. Elements can be null. If a deck is empty but both its cards are present, only the first element of the set of 3 will be null. Besides this first element of the set, also one or both of the other ones can be null (if it remains only one or no cards of this type to be picked)
      Returns:
      a new list containing the 6 cards on the table that can be picked by players The order: top of resource deck, 2 visible resource cards, top of gold deck, 2 visible gold cards
    • fetchCommonObjectives

      public List<CardObjective> fetchCommonObjectives()
      Returns the visible cards for the common objectives. Note that the deck of objective cards is always visible, with top card visible from its back side. Hence this information is not included in the list
      Returns:
      the list of the 2 common objectives.
    • fetchAvailableCoord

      public List<Coordinates> fetchAvailableCoord(PlayerLobby player) throws InvalidPlayerException
      Returns:
      the List of coordinates in which new cards can be played If game has not started yet, the list is empty
      Throws:
      InvalidPlayerException - If player is not among this match's players
    • fetchHandPlayable

      public List<CardPlayable> fetchHandPlayable(PlayerLobby player) throws InvalidPlayerException
      Parameters:
      player - one of the players of the match
      Returns:
      The cards in the hand of the player
      Throws:
      InvalidPlayerException - if the player is not one of the players of the match
    • fetchHandObjective

      public CardObjective fetchHandObjective(PlayerLobby player) throws InvalidPlayerException
      Parameters:
      player - one of the players of the match
      Returns:
      the personal objective of the player (null if it hasn't been initialized yet)
      Throws:
      InvalidPlayerException - if the passed player is not one of the players of the match
    • setStarter

      private void setStarter(Player player)
      Assigns the starter card to the player
      Parameters:
      player - to which the starter card is assigned
    • fetchStarter

      public CardStarter fetchStarter(PlayerLobby player) throws InvalidPlayerException
      Parameters:
      player - One of the players of the match
      Returns:
      The starter card assigned to player
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
    • playStarter

      public void playStarter(PlayerLobby playerLobby, Side side) throws GameStatusException, InvalidPlayerException, InvalidPlayCardException
      This method plays the starting of the given player on the passed side
      Parameters:
      playerLobby - Player who has chosen which side of the starting card he wants to play
      side - The side of the starting card
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
      GameStatusException - If game phase is not INIT
      InvalidPlayCardException - Positioning error of the card at coordinates (0,0).
    • setPossiblePersonalObjectives

      private void setPossiblePersonalObjectives(Player player)
      Assign to the given player the two objectives he can choose from
      Parameters:
      player - one of the players of the match
    • fetchPersonalObjectives

      public List<CardObjective> fetchPersonalObjectives(PlayerLobby player) throws InvalidPlayerException
      Parameters:
      player - who should be contained in players
      Returns:
      a list containing the two objective cards the player can choose from
      Throws:
      InvalidPlayerException - if the player is not one of the players of this match
    • choosePersonalObjective

      public void choosePersonalObjective(PlayerLobby player, CardObjectiveIF cardObjective) throws GameStatusException, InvalidPlayerException, InvalidChoiceException, VariableAlreadySetException
      Sets the personal objective of the player according to his choice
      Parameters:
      player - one of the players of the match
      cardObjective - the objective chosen by the player
      Throws:
      GameStatusException - if this method isn't called in the INIT phase
      InvalidPlayerException - if the player is not one of the players of this match
      InvalidChoiceException - if the objective card does not belong to the list of the possible objective cards for the player
      VariableAlreadySetException - if this method has been called before for the player
    • checkInGamePhase

      private void checkInGamePhase()
      This method checks if the initial phase (game setup) has been completed, and if so, sets the game status to IN_GAME
    • startGame

      public void startGame() throws GameStatusException
      Sets the starter Card, sets the possible objective cards, gives the initial cards to the players. Can be called only if the match has not started yet (gamePhase==null) and sets game phase to INIT.
      Throws:
      GameStatusException - if this method is called when game has already started (gamePhase!=null)
    • playCard

      Plays a given card side on the field of a given player, at the given coordinates It only increases the turnActionsCounter if the player is not connected
      Parameters:
      cardIF - Card which is being played. It must be in player's hand
      side - indicates whether the card is going to be played on the front or on the back
      coordinates - in the field of the player where the card is going to be positioned
      Returns:
      The played card, null if the player is not connected
      Throws:
      GameStatusException - if the actual phase is different from IN_GAME or FINAL_PHASE, or it's not the moment in turn for playing the card on field
      RequirementsNotMetException - If the requirements for playing the specified card in player's field are not met
      InvalidPlayCardException - If the player doesn't have the specified card, or generic positioning error
    • pickCard

      public void pickCard(CardPlayableIF cardIF) throws GameStatusException, InvalidDrawCardException
      Picks one of the 6 cards on the table If the player isn't connected or there is no card to pick, it only increases the turnActionsCounter
      Parameters:
      cardIF - A playable card that should be in the field
      Throws:
      InvalidDrawCardException - if the passed card is not on the table
      GameStatusException - if this method is called not in game phase IN_GAME or FINAL_PHASE, or if it's not the right moment for picking the card from common field
    • nextTurn

      public boolean nextTurn() throws GameStatusException
      This method makes the match proceed from the turn that has just been played to the next one, by changing the currentPlayer and, if necessary, changing the GameStatus
      Returns:
      False if there is no other turn to play after the one that has just been played, true otherwise
      Throws:
      GameStatusException - if this method is called not in game phase IN_GAME or FINAL_PHASE, or * if it's not the right moment for changing turn (player still has to play and/or pick)
    • checkFinalPhase

      private boolean checkFinalPhase()
      Returns:
      true if the conditions triggering the end of the game (a player has reached 20 points or both the Resources and Gold decks are empty) are satisfied, false otherwise
    • addObjectivePoints

      public void addObjectivePoints() throws GameStatusException
      This method adds the points given by Objective cards to each player. Make the game phase go on to ENDED
      Throws:
      GameStatusException - if this method is called in a phase which is not the CALC_POINTS phase
    • calcWinner

      public List<PlayerLobby> calcWinner() throws GameStatusException
      This method finds the winner among the connected players, ie the one with the most points
      Returns:
      the winner of the match
      Throws:
      GameStatusException - if this method is called in a phase which is not the ENDED phase, unless there is only one player left
    • getGameStatus

      public GameStatus getGameStatus()
      Returns:
      the status of the game
    • getCurrentPlayer

      public Player getCurrentPlayer()
      Returns:
      the player who is playing in the current turn. Null if the gameStatus is different from IN_GAME or FINAL_PHASE (the 2 phases divided in turns)
    • getFirstPlayer

      public Player getFirstPlayer()
      Returns:
      The first player of the match
    • getPlayers

      public List<Player> getPlayers()
      Returns:
      The players of the match The size is >=2 and <=4, and the colors of players are all different
    • getFieldByPlayer

      public FieldIF getFieldByPlayer(PlayerLobby playerLobby) throws InvalidPlayerException
      Parameters:
      playerLobby - Player whose field is returned
      Returns:
      Field (interface) of the given player
      Throws:
      InvalidPlayerException - If the player is not among the playing players in the match
    • getTurnsToEnd

      public int getTurnsToEnd()
      Returns:
      The number of turn to be completed to reach the end of the turn-based phase. -1 if the phase FINAL_PHASE has not been reached yet.