java.lang.Object
it.polimi.ingsw.am13.model.Match
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)
andchoosePersonalObjective(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_GAMEnextTurn()
: 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 arepickCard(...)
andplayCard(...)
addObjectivePoints()
: Calculates extra points given by objective cards for each player, and moves the game on to phase ENDEDcalcWinner()
: Only in this ultimate phase this method can be called, calculating which player has won
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
This variable keeps track of how players have chosen their objective card and played their starter cardprivate Player
The player who is playing in the current turn.private final DeckHandler
<CardGold> Deck handler of the gold cards deckprivate final DeckHandler
<CardObjective> Deck handler of the objective cards deckprivate final DeckHandler
<CardResource> Deck handler of the resource cards deckprivate final Deck
<CardStarter> Deck handler of the starter cards deckprivate final Player
The first player of the matchprivate final int
The index of the first player in the players listprivate GameStatus
The status of the gameThe players of the match.private final Map
<PlayerLobby, Player> Map associating player decontextualized from game to the actual playing playerprivate int
Count of the actions done by a player during their turn.private int
This variable keeps track of how many turns are left during the final phase of the game -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method adds the points given by Objective cards to each player.This method finds the winner among the connected players, ie the one with the most pointsprivate boolean
private void
This method checks if the initial phase (game setup) has been completed, and if so, sets the game status to IN_GAMEvoid
choosePersonalObjective
(PlayerLobby player, CardObjectiveIF cardObjective) Sets the personal objective of the player according to his choiceint
void
disconnectPlayer
(PlayerLobby player) Disconnects the given player.fetchAvailableCoord
(PlayerLobby player) Returns the visible cards for the common objectives.fetchHandObjective
(PlayerLobby player) fetchHandPlayable
(PlayerLobby player) boolean
fetchIsConnected
(PlayerLobby player) fetchPersonalObjectives
(PlayerLobby player) List of all visible cards (that are pickable during turn phases).fetchStarter
(PlayerLobby player) getFieldByPlayer
(PlayerLobby playerLobby) int
boolean
nextTurn()
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 GameStatusvoid
pickCard
(CardPlayableIF cardIF) 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 turnActionsCounterplayCard
(CardPlayableIF cardIF, Side side, Coordinates coordinates) 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 connectedvoid
playStarter
(PlayerLobby playerLobby, Side side) This method plays the starting of the given player on the passed sidevoid
reconnectPlayer
(PlayerLobby player) Reconnects the given player by calling the corresponding method in the Player classprivate void
setPossiblePersonalObjectives
(Player player) Assign to the given player the two objectives he can choose fromprivate void
setStarter
(Player player) Assigns the starter card to the playervoid
Sets the starter Card, sets the possible objective cards, gives the initial cards to the players.
-
Field Details
-
deckResources
Deck handler of the resource cards deck -
deckGold
Deck handler of the gold cards deck -
deckObjective
Deck handler of the objective cards deck -
deckStarter
Deck handler of the starter cards deck -
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
Map associating player decontextualized from game to the actual playing player -
firstPlayer
The first player of the match -
firstPlayerIndex
private final int firstPlayerIndexThe index of the first player in the players list -
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
The status of the game -
turnsToEnd
private int turnsToEndThis variable keeps track of how many turns are left during the final phase of the game -
countSetup
private int countSetupThis variable keeps track of how players have chosen their objective card and played their starter card -
turnActionsCounter
private int turnActionsCounterCount 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
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
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 vianextTurn()
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 calledInvalidPlayerException
- if player is not among this match's players
-
reconnectPlayer
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 calledInvalidPlayerException
- if player is not among this match's players
-
fetchIsConnected
- 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
List of all visible cards (that are pickable during turn phases). The list is of size 6, with order: top of deck (withgetVisibleSide()==Side.SIDEBACK
), and 2 visible cards (withgetVisibleSide()==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
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
- 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
- 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
- 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
Assigns the starter card to the player- Parameters:
player
- to which the starter card is assigned
-
fetchStarter
- 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 playside
- The side of the starting card- Throws:
InvalidPlayerException
- If the player is not among the playing players in the matchGameStatusException
- If game phase is not INITInvalidPlayCardException
- Positioning error of the card at coordinates (0,0).
-
setPossiblePersonalObjectives
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 matchcardObjective
- the objective chosen by the player- Throws:
GameStatusException
- if this method isn't called in the INIT phaseInvalidPlayerException
- if the player is not one of the players of this matchInvalidChoiceException
- if the objective card does not belong to the list of the possible objective cards for the playerVariableAlreadySetException
- 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
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
public CardPlayableIF playCard(CardPlayableIF cardIF, Side side, Coordinates coordinates) throws GameStatusException, RequirementsNotMetException, InvalidPlayCardException 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 handside
- indicates whether the card is going to be played on the front or on the backcoordinates
- 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 fieldRequirementsNotMetException
- If the requirements for playing the specified card in player's field are not metInvalidPlayCardException
- If the player doesn't have the specified card, or generic positioning error
-
pickCard
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 tableGameStatusException
- 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
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
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
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
- Returns:
- the status of the game
-
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
- Returns:
- The first player of the match
-
getPlayers
- Returns:
- The players of the match The size is >=2 and <=4, and the colors of players are all different
-
getFieldByPlayer
- 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.
-