java.lang.Object
it.polimi.ingsw.am13.model.player.Field
- All Implemented Interfaces:
FieldIF
,Serializable
Class which represents the status of the game field of a specific player. It keeps track of played cards and visible
resources. It performs actions and queries related to the player's field.
-> Card positions are referenced with Coordinates (see Coordinates class for a detailed description of the system)
-> Resource counters are stored in a Map in the form of (Resource, count) key-value pair
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map
<Coordinates, CardSidePlayable> Description of the player's card positions using a sparse matrix representation.Resource counters stored in a Map in the form of (Resource, count) key-value pair. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addResource
(Resource res) Increase by 1 the counter of the given resource.private boolean
cardIsPlaceableAtCoord
(Coordinates coords) Checks if a card is placeable in a certain position of the Field applying the rules described in the rulebook.Retrieves the List of coordinates in which new cards can be placed.getCardSideAtCoord
(Coordinates coords) Retrieves the placed card in the Field at given coordinates.private int
getResourceCount
(Resource res) Retrieves the number of visible resources in the Field.Getter for visible resource counters of the Field.getRoot()
Retrieves the root of the field's cards (the starter card in position (0,0)).boolean
isCoordPlaced
(Coordinates coord) Checks if the coordinated passed as parameters has a card placed on itvoid
playCardSide
(CardSidePlayable card, Coordinates coords) Play a card side at given coordinates on the Field.private void
removeResource
(Resource res) Decrease by 1 the counter of the given resource.toString()
-
Field Details
-
field
Description of the player's card positions using a sparse matrix representation. They are stored in a Map in the form of (Coordinates, CardSidePlayable) key-value pair. -
resources
Resource counters stored in a Map in the form of (Resource, count) key-value pair. Please note that 'NO_RESOURCE' resources are tracked too.
-
-
Constructor Details
-
Field
public Field()Constructor of the Field. It initializes all resource counters to 0.
-
-
Method Details
-
getCoordinatesPlaced
- Specified by:
getCoordinatesPlaced
in interfaceFieldIF
- Returns:
- A list of all coordinated where a card has been already positioned. The list is empty if no cards has been placed yet. For how the game works, the coordinate (0,0), is the list is not empty, must be present
-
isCoordPlaced
Checks if the coordinated passed as parameters has a card placed on it- Specified by:
isCoordPlaced
in interfaceFieldIF
- Parameters:
coord
- Coordinates to be checked if a card is on it- Returns:
- True if a card is on the indicated coordinated, false otherwise
-
getCardSideAtCoord
Retrieves the placed card in the Field at given coordinates.- Specified by:
getCardSideAtCoord
in interfaceFieldIF
- Parameters:
coords
- Coordinates of the card.- Returns:
- If present, the card side at given coordinates. If not present, null.
-
getRoot
Retrieves the root of the field's cards (the starter card in position (0,0)).- Returns:
- Starter card side placed on the Field.
- Throws:
InvalidCoordinatesException
- If called too early in the game, starter card could not have been placed yet.
-
addResource
Increase by 1 the counter of the given resource.- Parameters:
res
- Resource to be incremented.
-
removeResource
Decrease by 1 the counter of the given resource.- Parameters:
res
- Resource to be decremented.
-
getResourceCount
Retrieves the number of visible resources in the Field.- Parameters:
res
- Resource of which count needs to be retrieved.
-
cardIsPlaceableAtCoord
Checks if a card is placeable in a certain position of the Field applying the rules described in the rulebook. A card can be placed at given coordinates if and only if the following requirements are satisfied: 1) Coordinates are valid (checked in Coordinates class) 2) There's no card placed at given coordinates yet 3) There is at least a card in one of the surrounding coordinates 4) All the existing surrounding corners are placeable- Parameters:
coords
- Coordinates to be checked- Returns:
- true if a new card can be placed in the spot, false otherwise
-
getAvailableCoords
Retrieves the List of coordinates in which new cards can be placed. It returns all and only the coordinates for which cardIsPlaceableAtCoord() returns true for the actual Field situation.- Specified by:
getAvailableCoords
in interfaceFieldIF
- Returns:
- A list of coordinates in which a card can be placed.
-
playCardSide
public void playCardSide(CardSidePlayable card, Coordinates coords) throws InvalidPlayCardException, RequirementsNotMetException Play a card side at given coordinates on the Field. Automatically updates the counters of visible resources. If the card can't be placed because of invalid Coordinates, or startCard is not properly initialized, InvalidPlayCardException is thrown. If the card can't be placed because of requirements not met, RequirementsNotMetException is thrown.- Parameters:
card
- The card side to be placed on the Field.coords
- Coordinates where to place the new card.- Throws:
InvalidPlayCardException
- Positioning error of the card at given coordinates, or startCard is not yet initialized, or first side played as (0,0) is not a side of startCard.RequirementsNotMetException
- At least one Requirement is not satisfied for the given card.
-
getResourcesInField
Getter for visible resource counters of the Field. The resource counters are stored in a Map in the form of (Resource, count) key-value pair.- Specified by:
getResourcesInField
in interfaceFieldIF
- Returns:
- Map containing the counters of visible resources
-
toString
-