public class GameBoard
extends java.lang.Object
The GameBoard class contains the actual representation of the board via a square Cell
matrix (the single
cells contain each further information about the buildings and, if present, the occupant).
During the development, we decided to allow the board to be transformed, via a dedicated method, into a
ArrayList
, since it can be easier to manipulate (e.g. during tests or to be sent over the network).
This class uses the JsonCreator
, JsonProperty
and JsonIdentityInfo
in order to
serialize and save the board on a file (as a part of the Game
class), to be able to restore the game
status upon a server failure.
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description java.util.List<Cell>
cloneAllCells()
GameBoard
cloneGameBoard()
Returns a clone of the current game board stateboolean
equals(java.lang.Object o)
Compares the argument to the receiver, and answers true if their representation as Arrays is the samejava.util.List<Cell>
getAdjacentCells(Cell cell)
Gets all the adjacent cells of a given onejava.util.List<Cell>
getAllCells()
Returns all the cells of the game boardCell
getCell(int x, int y)
Returns a cell by its coordinatesCell
getCell(Cell cell)
"Proper" cell getterCell
getCellBehind(Cell src, Cell dest)
Calculates the Cell behind a given Cellint
hashCode()
boolean
isInsideGameBoard(int x, int y)
Checks if cell coordinates are legal
-
Constructor Details
-
Method Details
-
getCell
Returns a cell by its coordinatesIf the indexes are out of bounds, returns null
- Parameters:
x
- the X coordinate of the cell to returny
- the Y coordinate of the cell to return- Returns:
- the cell corresponding to the coordinates if exists, null otherwise
-
getCell
"Proper" cell getterGiven a generic Cell object, returns the corresponding Cell object contained in the game board, based on its coordinates
- Parameters:
cell
- the cell to get the coordinates from- Returns:
- the corresponding cell from the game board
-
isInsideGameBoard
public boolean isInsideGameBoard(int x, int y)Checks if cell coordinates are legalThe cell coordinates are considered legal if both of them are between 0 and the board DIMENSION, minus one
- Parameters:
x
- the X coordinate of the cell to checky
- the Y coordinate of the cell to check- Returns:
- true if the coordinates are legal, false otherwise
-
getAllCells
Returns all the cells of the game boardThis method creates an ArrayList of Cells, containing all the cells in the GameBoard, sorted by row, low to high
- Returns:
- an ArrayList containing all the cells of the board
-
cloneAllCells
-
getCellBehind
Calculates the Cell behind a given CellGiven two Cells (src, dest), the Cell behind dest, if exists, is the cell at distance 1 from the dest cell, at distance (dist(src, dest) + 1) from src lying on the line passing by src and dest. If the resulting coordinates are outside the board, the cell behind does not exists. This method DOES NOT check for height differences in any way.
NOTE: this method is supposed to be used with cells which distance between each other is one; it still works with more distant cells, if the cells are aligned on an axis (same X or Y coordinate) or on a diagonal (the modulus of the difference between the X coordinates is equal to the modulus of the difference of the Y coordinates of the two Cells. In any other case, for the sake of the project at this state, the behavior of the method is not granted.- Parameters:
src
- the first point of the linedest
- the second point of the line- Returns:
- the cell behind the dest Cell if exists, null otherwise
-
getAdjacentCells
Gets all the adjacent cells of a given oneA cell is defined adjacent to another cell if the distance between them is exactly one (diametrically opposed cells, e.g. (1,0) and (1,4), are not considered adjacent). cular, the adjacency relation is:
- symmetric: if A is adjacent to B, B is adjacent to A
- non-transitive: if A is adjacent to B and B is adjacent to C, A and C are not adjacent
- non-reflexive: A is not adjacent to itself
- 3, if cell is one of the four corners
- 5, if cell is a border cell
- 8, in any other case
- Parameters:
cell
- the cell to analyze- Returns:
- an ArrayList containing the cells adjacent to cell
-
cloneGameBoard
Returns a clone of the current game board stateIn order to save the state of the entire game, it is needed to save the game board and its cells to preserve the references. To do so, we create a new GameBoard instance with the special constructor (GameBoard(GameBoard)), which creates an exact replica of the cells of the current game board in the new board.
- Returns:
- a clone of the game board
-
equals
public boolean equals(java.lang.Object o)Compares the argument to the receiver, and answers true if their representation as Arrays is the same- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the object to be compared with this- Returns:
- true if the object is the same as the cell, false otherwise
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-