Module AM37

Class Cell

java.lang.Object
it.polimi.ingsw.model.Cell
All Implemented Interfaces:
java.io.Serializable

public class Cell
extends java.lang.Object
implements java.io.Serializable
The base unit of the game board

class contains all information about a cell inside a gameBoard, such as:

  • Position, via coordX and coordY (see GameBoard;
  • Building state, via block and hasDome (see Block);
  • Occupant, via occupiedBy (see Worker);
This class uses JacksonAnnotations to export the internal status to an external JSON file, in order to save the current state and recover it in case of a server failure.
See Also:
Serialized Form
  • Constructor Summary

    Constructors 
    Constructor Description
    Cell​(int coordX, int coordY)
    Default constructor
    Cell​(int coordX, int coordY, boolean hasDome, Worker occupiedBy, Block block)
    Jackson Constructor
  • Method Summary

    Modifier and Type Method Description
    Cell cloneCell()
    Creates a clone of the cell
    boolean equals​(java.lang.Object o)
    Compares the argument to the receiver, and answers true if their coordinates and blocks built (including dome) are equals
    Block getBlock()
    block getter
    int getCoordX()
    coordX getter
    int getCoordY()
    coordY getter
    Worker getOccupiedBy()
    occupiedBy getter
    boolean hasDome()
    hasDome getter
    int hashCode()  
    int heightDifference​(Cell cell)
    Provides the height difference between two cells
    void setBlock​(Block block)
    block setter
    void setHasDome​(boolean hasDome)
    hasDome setter
    void setOccupiedBy​(Worker occupiedBy)
    occupiedBy setter
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

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

    • Cell

      public Cell​(int coordX, int coordY)
      Default constructor

      Creates a new Cell, assigning its coordinates. At the time of their creation, the Cells are empty; this means that there are no buildings nor workers.

      Parameters:
      coordX - the X coordinate of the cell to create
      coordY - the Y coordinate of the cell to create
    • Cell

      public Cell​(int coordX, int coordY, boolean hasDome, Worker occupiedBy, Block block)
      Jackson Constructor

      Loads a previously saved Cell state; the Cell is "reassembled" in this constructor (after reading the saved state from file) instead of being built during the file parsing to avoid reference problems.

      Parameters:
      coordX - the X coordinate of the cell to restore
      coordY - the Y coordinate of the cell to restore
      hasDome - the hasDome flag of the cell to restore
      occupiedBy - the Worker occupying the cell
      block - the building block on the cell
  • Method Details

    • getCoordX

      public int getCoordX()
      coordX getter
      Returns:
      the X coordinate of the cell
    • getCoordY

      public int getCoordY()
      coordY getter
      Returns:
      the Y coordinate of the cell
    • hasDome

      public boolean hasDome()
      hasDome getter
      Returns:
      true if there is a dome on the cell, false otherwise
    • setHasDome

      public void setHasDome​(boolean hasDome)
      hasDome setter

      Following the classic game flow, this method is used only to set the hasDome attribute to true upon a dome construction; since there might be additional gods that allows a dome to be removed, we decided to leave it as a setter. At the current stage of development, this method is always called passing true as a parameter.

      Parameters:
      hasDome - boolean flag to notify the dome presence
    • getOccupiedBy

      public Worker getOccupiedBy()
      occupiedBy getter
      Returns:
      the Worker on the cell if present, null otherwise
    • setOccupiedBy

      public void setOccupiedBy​(Worker occupiedBy)
      occupiedBy setter
      Parameters:
      occupiedBy - the Worker to be placed on the cell
    • getBlock

      public Block getBlock()
      block getter
      Returns:
      the last Block built on the cell
    • setBlock

      public void setBlock​(Block block)
      block setter
      Parameters:
      block - the Block to build on the cell
    • heightDifference

      public int heightDifference​(Cell cell)
      Provides the height difference between two cells

      Returns the height difference as (parameterCell.height - thisCell.height).
      The height difference between two cells is defined as the difference between the Blocks built on the two cells.

      Parameters:
      cell - the Cell to compare
      Returns:
      the height difference
    • cloneCell

      public Cell cloneCell()
      Creates a clone of the cell

      Using the private constructor to create a new Cell instance having the same attributes; used to clone and send the gameBoard over the network.

      Returns:
      a clone of the cell
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object o)
      Compares the argument to the receiver, and answers true if their coordinates and blocks built (including dome) are equals
      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