Class HexagonalGrid

java.lang.Object
spinhex.model.HexagonalGrid
All Implemented Interfaces:
Cloneable

public final class HexagonalGrid extends Object implements Cloneable
Represents a hexagonal grid for the SpinHex puzzle game.

This class provides an efficient implementation of a hexagonal grid using a flat array for internal storage. It handles the mapping between axial coordinates (q,s) and array indices, boundary checking, and common grid operations.

  • Constructor Summary

    Constructors
    Constructor
    Description
    HexagonalGrid(byte[][] board)
    Constructs a new hexagonal grid from a 2D byte array.
    HexagonalGrid(int size)
    Constructs a new hexagonal grid with the specified size.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a copy of this hexagonal grid.
    boolean
    Compares this hexagonal grid with another object for equality.
    byte
    get(int q, int s)
    Gets the value of a cell at the specified axial coordinates.
    byte
    Gets the value of a cell at the specified axial position.
    int
    Returns the radius of the hexagonal grid.
    int
    Returns the size of the hexagonal grid.
    int
    Returns a hash code for this hexagonal grid.
    boolean
    isInBounds(int q, int s)
    Checks if the specified axial coordinates are within the bounds of the grid.
    boolean
    Checks if an axial position is within the bounds of the grid.
    void
    set(int q, int s, byte value)
    Sets the value of a cell at the specified axial coordinates.
    void
    set(AxialPosition pos, byte value)
    Sets the value of a cell at the specified axial position.

    Methods inherited from class java.lang.Object

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

    • HexagonalGrid

      public HexagonalGrid(int size)
      Constructs a new hexagonal grid with the specified size.

      Creates an empty grid where all cells are initialized to 0.

      Parameters:
      size - The size of the grid (number of rows/columns)
    • HexagonalGrid

      public HexagonalGrid(byte[][] board)
      Constructs a new hexagonal grid from a 2D byte array.

      The grid is created with the same size as the input array, and values from the array are copied to the grid where they are within bounds.

      Parameters:
      board - A 2D byte array representing the initial state of the grid
  • Method Details

    • getSize

      public int getSize()
      Returns the size of the hexagonal grid.

      The size is defined as the number of rows/columns in the grid.

      Returns:
      The size of the hexagonal grid
    • getRadius

      public int getRadius()
      Returns the radius of the hexagonal grid.

      The radius is defined as half the size of the grid, rounded down.

      Returns:
      The radius of the hexagonal grid
    • set

      public void set(int q, int s, byte value)
      Sets the value of a cell at the specified axial coordinates.
      Parameters:
      q - The q-coordinate of the cell
      s - The s-coordinate of the cell
      value - The value to set for the cell
    • set

      public void set(AxialPosition pos, byte value)
      Sets the value of a cell at the specified axial position.
      Parameters:
      pos - The axial position of the cell
      value - The value to set for the cell
    • get

      public byte get(int q, int s)
      Gets the value of a cell at the specified axial coordinates.

      Returns 0 if the coordinates are outside the grid bounds.

      Parameters:
      q - The q-coordinate of the cell
      s - The s-coordinate of the cell
      Returns:
      The value of the cell, or 0 if out of bounds
    • get

      public byte get(AxialPosition pos)
      Gets the value of a cell at the specified axial position.

      Returns 0 if the position is outside the grid bounds.

      Parameters:
      pos - The axial position of the cell
      Returns:
      The value of the cell, or 0 if out of bounds
    • isInBounds

      public boolean isInBounds(AxialPosition position)
      Checks if an axial position is within the bounds of the grid.
      Parameters:
      position - The axial position to check
      Returns:
      true if the position is within bounds, false otherwise
    • isInBounds

      public boolean isInBounds(int q, int s)
      Checks if the specified axial coordinates are within the bounds of the grid.
      Parameters:
      q - The q-coordinate to check
      s - The s-coordinate to check
      Returns:
      true if the coordinates are within bounds, false otherwise
    • clone

      public HexagonalGrid clone()
      Creates a copy of this hexagonal grid.
      Overrides:
      clone in class Object
      Returns:
      A new hexagonal grid with the same content as this one
    • equals

      public boolean equals(Object o)
      Compares this hexagonal grid with another object for equality.

      Two hexagonal grids are equal if they have the same content.

      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare with
      Returns:
      true if the objects are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code for this hexagonal grid.
      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this object