Record Class Ship

java.lang.Object
java.lang.Record
com.alessiocascini.battleship.client.model.Ship
Record Components:
info - The ShipInfo containing name and size
cells - An array of Cell objects representing the ship's position

public record Ship(ShipInfo info, Cell[] cells) extends Record
Represents a placed ship on the game board. This record combines the ship's type information with its physical coordinates on the grid.
Author:
Alessio Cascini
  • Constructor Summary

    Constructors
    Constructor
    Description
    Ship(ShipInfo info, Cell[] cells)
    Creates an instance of a Ship record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the cells record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    int[][]
    Converts the ship's cell coordinates into a primitive 2D integer array.
    final int
    Returns a hash code value for this object.
    Returns the value of the info record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

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

    • Ship

      public Ship(ShipInfo info, Cell[] cells)
      Creates an instance of a Ship record class.
      Parameters:
      info - the value for the info record component
      cells - the value for the cells record component
  • Method Details

    • getCellsAsArray

      public int[][] getCellsAsArray()
      Converts the ship's cell coordinates into a primitive 2D integer array. This format is typically used for network transmission to the server.
      Returns:
      A 2D array where each element is a [row, col] pair
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • info

      public ShipInfo info()
      Returns the value of the info record component.
      Returns:
      the value of the info record component
    • cells

      public Cell[] cells()
      Returns the value of the cells record component.
      Returns:
      the value of the cells record component