Module AM37

Class GameBoard

java.lang.Object
it.polimi.ingsw.model.GameBoard

public class GameBoard
extends java.lang.Object
Representation of the game field

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

    Constructors 
    Constructor Description
    GameBoard()
    Default constructor Creates a matrix of 25 Cells
    GameBoard​(java.util.List<Cell> allCells)
    Jackson constructor
  • Method Summary

    Modifier and Type Method Description
    java.util.List<Cell> cloneAllCells()  
    GameBoard cloneGameBoard()
    Returns a clone of the current game board state
    boolean equals​(java.lang.Object o)
    Compares the argument to the receiver, and answers true if their representation as Arrays is the same
    java.util.List<Cell> getAdjacentCells​(Cell cell)
    Gets all the adjacent cells of a given one
    java.util.List<Cell> getAllCells()
    Returns all the cells of the game board
    Cell getCell​(int x, int y)
    Returns a cell by its coordinates
    Cell getCell​(Cell cell)
    "Proper" cell getter
    Cell getCellBehind​(Cell src, Cell dest)
    Calculates the Cell behind a given Cell
    int hashCode()  
    boolean isInsideGameBoard​(int x, int y)
    Checks if cell coordinates are legal

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GameBoard

      public GameBoard​(java.util.List<Cell> allCells)
      Jackson constructor
      Parameters:
      allCells - the gameBoard to restore, as array of cells
    • GameBoard

      public GameBoard()
      Default constructor Creates a matrix of 25 Cells
  • Method Details

    • getCell

      public Cell getCell​(int x, int y)
      Returns a cell by its coordinates

      If the indexes are out of bounds, returns null

      Parameters:
      x - the X coordinate of the cell to return
      y - the Y coordinate of the cell to return
      Returns:
      the cell corresponding to the coordinates if exists, null otherwise
    • getCell

      public Cell getCell​(Cell cell)
      "Proper" cell getter

      Given 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 legal

      The 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 check
      y - the Y coordinate of the cell to check
      Returns:
      true if the coordinates are legal, false otherwise
    • getAllCells

      public java.util.List<Cell> getAllCells()
      Returns all the cells of the game board

      This 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

      public java.util.List<Cell> cloneAllCells()
    • getCellBehind

      public Cell getCellBehind​(Cell src, Cell dest)
      Calculates the Cell behind a given Cell

      Given 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 line
      dest - the second point of the line
      Returns:
      the cell behind the dest Cell if exists, null otherwise
    • getAdjacentCells

      public java.util.List<Cell> getAdjacentCells​(Cell cell)
      Gets all the adjacent cells of a given one

      A 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
      Given these premises, this method will never return an empty ArrayList: the size of the list is
      • 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

      public GameBoard cloneGameBoard()
      Returns a clone of the current game board state

      In 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 class java.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 class java.lang.Object