java.lang.Object
it.polimi.ingsw.am13.model.GameModel
- All Implemented Interfaces:
GameModelIF
Model of the entire game.
It can be used as an interface to handle the entire game, starting from its creation with a full room, till the
natural end of the game.
It handles also disconnections and reconnections, and is responsible for triggering all the notifies about game
actions to the listeners
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
Unique number indicating the actual game model (hence the game/match to its all entirety)private final Room
This is used to notify the view when a change occurs in the GameModel after a game event happens.private final Match
Class match with all the information regarding the match itself and how to precess it -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Method callable once reached phase CALC_POINTS.Method callable only once reached phase ENDED It finds the winner, ie the player(s) with the most pointsvoid
choosePersonalObjective
(PlayerLobby player, CardObjectiveIF cardObj) Method callable only during INIT phase.int
void
disconnectPlayer
(PlayerLobby player) Disconnects the given player, by calling the corresponding method in match, removing the listener and notifying the listenersfetchAvailableCoord
(PlayerLobby player) Returns the visible cards for the common objectives.The first player of the match is the one that will do the first play of the card in phase IN_GAMEfetchHandObjective
(PlayerLobby player) fetchHandPlayable
(PlayerLobby player) boolean
fetchIsConnected
(PlayerLobby player) fetchPersonalObjectives
(PlayerLobby player) Method callable only during INIT phase.List of all visible cards (that are pickable during turn phases).fetchPlayerField
(PlayerLobby player) fetchStarter
(PlayerLobby player) int
boolean
nextTurn()
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 card) Picks one of the 6 cards on the tablevoid
playCard
(CardPlayableIF card, Side side, Coordinates coord) Plays a given card side on the field, at the given coordinatesvoid
playStarter
(PlayerLobby player, Side side) Method callable only once per player during INIT phase.void
reconnectPlayer
(GameListener gameListener, GameController controller) Reconnects the given player.void
startGame
(GameController controller) Sets the starter Card, sets the possible objective cards, gives the initial cards to the players.void
transmitChatMessage
(PlayerLobby sender, List<PlayerLobby> receivers, String text) Transmits a chat message (while this message will not cause any change in the model, nor will it be stored, it follows the same path as all the other messages)
-
Field Details
-
gameId
private final int gameIdUnique number indicating the actual game model (hence the game/match to its all entirety) -
match
Class match with all the information regarding the match itself and how to precess it -
listenerHandler
This is used to notify the view when a change occurs in the GameModel after a game event happens.
-
-
Constructor Details
-
GameModel
Creates a new instance ofGameModel
with the specified players. The players used here to create the model are the definitive players, and nobody can be added in a second time.- Parameters:
listenerHandler
- Room with the players who will be the definite players of the game, corresponding to the handler of their listeners- Throws:
InvalidPlayersNumberException
- If the list of players has size <2 or >4, the room did not reach the set number of players (the game for the room is not set as started), or there are duplicate chosen colors
-
-
Method Details
-
getListeners
- Returns:
- Returns the list of GameListener handled by this class.
-
disconnectPlayer
Disconnects the given player, by calling the corresponding method in match, removing the listener and notifying the listeners- Parameters:
player
- Player to disconnect- Throws:
InvalidPlayerException
- if the player associated to the GameListener is not a player of the matchConnectionException
- if the player was already disconnected when this method was called
-
reconnectPlayer
public void reconnectPlayer(GameListener gameListener, GameController controller) throws InvalidPlayerException, ConnectionException Reconnects the given player. It also notifies the players of this- Parameters:
gameListener
- New listener of the player who want to reconnectcontroller
- Controller of the game- Throws:
InvalidPlayerException
- if the player associated to the GameListener is not a player of the matchConnectionException
- if the player was already connected when this method was called
-
fetchIsConnected
- Throws:
InvalidPlayerException
-
countConnected
public int countConnected() -
getGameId
public int getGameId()- Specified by:
getGameId
in interfaceGameModelIF
- Returns:
- Unique number indicating the actual game model (hence the game/match to its all entirety)
-
fetchPlayersLobby
- Specified by:
fetchPlayersLobby
in interfaceGameModelIF
- Returns:
- List of players, decontextualized from game (their nicknames/tokens, nothing else)
-
fetchPlayers
- Specified by:
fetchPlayers
in interfaceGameModelIF
- Returns:
- List of players The order is the one followed in a round, from the first player to the last one
-
fetchFirstPlayer
The first player of the match is the one that will do the first play of the card in phase IN_GAME- Specified by:
fetchFirstPlayer
in interfaceGameModelIF
- Returns:
- The first player of the match
-
fetchCurrentPlayer
- Specified by:
fetchCurrentPlayer
in interfaceGameModelIF
- Returns:
- the player who is playing in the current turn. Null if the game phase is different from IN_GAME or FINAL_PHASE (the 2 phases divided in turns)
-
fetchPoints
- Specified by:
fetchPoints
in interfaceGameModelIF
- Returns:
- A map of points accumalated till this point for each player. If the turn-bases phases haven't started yet, the points will be 0
-
fetchGameStatus
- Specified by:
fetchGameStatus
in interfaceGameModelIF
- Returns:
- the status of the game. See class
GameStatus
for more details
-
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)- Specified by:
fetchPickables
in interfaceGameModelIF
- 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- Specified by:
fetchCommonObjectives
in interfaceGameModelIF
- Returns:
- the list of the 2 common objectives.
-
fetchStarter
- Specified by:
fetchStarter
in interfaceGameModelIF
- Parameters:
player
- One of the players of the match- Returns:
- The starter card assigned to player. Null if player has not been assigned a starter card yet
- Throws:
InvalidPlayerException
- If the player is not among the playing players in the match
-
fetchHandPlayable
- Specified by:
fetchHandPlayable
in interfaceGameModelIF
- Parameters:
player
- one of the players of the match- Returns:
- the cards in the hand of the player. The list if empty if player has no cards yet
- Throws:
InvalidPlayerException
- if the player is not one of the players of the match
-
fetchHandObjective
- Specified by:
fetchHandObjective
in interfaceGameModelIF
- 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
-
fetchAvailableCoord
- Specified by:
fetchAvailableCoord
in interfaceGameModelIF
- 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
-
fetchPlayerField
- Specified by:
fetchPlayerField
in interfaceGameModelIF
- Parameters:
player
- 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
-
fetchWinner
- Specified by:
fetchWinner
in interfaceGameModelIF
- Returns:
- Winner of the game if the phase is ENDED, null otherwise
-
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
)
-
playStarter
public void playStarter(PlayerLobby player, Side side) throws InvalidPlayerException, GameStatusException, InvalidPlayCardException Method callable only once per player during INIT phase. It plays the starter card of the given player on the passed side- Parameters:
player
- 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).
-
fetchPersonalObjectives
public List<CardObjectiveIF> fetchPersonalObjectives(PlayerLobby player) throws InvalidPlayerException, GameStatusException Method callable only during INIT phase. Returns the 2 possible objective cards the player can choose- Specified by:
fetchPersonalObjectives
in interfaceGameModelIF
- Parameters:
player
- who should be contained in players- Returns:
- a list containing the two objective cards the player can choose from
- Throws:
GameStatusException
- if this method isn't called in the INIT phaseInvalidPlayerException
- if the player is not one of the players of this match
-
choosePersonalObjective
public void choosePersonalObjective(PlayerLobby player, CardObjectiveIF cardObj) throws InvalidPlayerException, InvalidChoiceException, VariableAlreadySetException, GameStatusException Method callable only during INIT phase. Sets the personal objective of the player according to his choice. The objective card must be between the 2 possible cards given to the player. They can be retrieved withfetchPersonalObjectives(player)
- Parameters:
player
- one of the players of the matchcardObj
- 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
-
nextTurn
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 in the INIT or CALC POINTS phase
-
playCard
public void playCard(CardPlayableIF card, Side side, Coordinates coord) throws RequirementsNotMetException, InvalidPlayCardException, GameStatusException Plays a given card side on the field, at the given coordinates- Parameters:
card
- 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 backcoord
- Coordinates in the field of the player where the card is going to be positioned- Throws:
RequirementsNotMetException
- 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 errorGameStatusException
- If this method is called in the INIT or CALC POINTS phase
-
pickCard
Picks one of the 6 cards on the table- Parameters:
card
- The playable card to pick (that should be in the common field)- Throws:
InvalidDrawCardException
- if the passed card is not on the tableGameStatusException
- if this method is called in the INIT or CALC POINTS phase
-
addObjectivePoints
Method callable once reached phase CALC_POINTS. 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
Method callable only once reached phase ENDED It finds the winner, ie the player(s) 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
-
transmitChatMessage
Transmits a chat message (while this message will not cause any change in the model, nor will it be stored, it follows the same path as all the other messages)- Parameters:
sender
- of the messagereceivers
- of the messagetext
- content of the message
-