Class HexagonalGrid
java.lang.Object
spinhex.model.HexagonalGrid
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionHexagonalGrid
(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 TypeMethodDescriptionclone()
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
get
(AxialPosition pos) Gets the value of a cell at the specified axial position.int
Returns the radius of the hexagonal grid.int
getSize()
Returns the size of the hexagonal grid.int
hashCode()
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
isInBounds
(AxialPosition position) 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.
-
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 cells
- The s-coordinate of the cellvalue
- The value to set for the cell
-
set
Sets the value of a cell at the specified axial position.- Parameters:
pos
- The axial position of the cellvalue
- 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 cells
- The s-coordinate of the cell- Returns:
- The value of the cell, or 0 if out of bounds
-
get
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
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 checks
- The s-coordinate to check- Returns:
- true if the coordinates are within bounds, false otherwise
-
clone
Creates a copy of this hexagonal grid. -
equals
-
hashCode
-