Class SpinHexModel
java.lang.Object
spinhex.model.SpinHexModel
- All Implemented Interfaces:
common.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
,Cloneable
,puzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
,TwoPhaseActionState<AxialPosition, Rotation>
- Direct Known Subclasses:
ReadOnlySpinHexModelWrapper
A model for the SpinHex puzzle game, which implements the
TwoPhaseActionState
interface.
The game is played on a hexagonal grid where each cell contains a colored
hex.
The objective is to rotate a group of hexes to match the solved
configuration.
The puzzle has the following key features:
- A NxN board with colored hexagonal cells
- Moves involve selecting a hex and rotating its neighbors either clockwise or counterclockwise
- A hex can only be selected if it is colored and all of its neighbors are non-empty
-
Nested Class Summary
Nested classes/interfaces inherited from interface spinhex.model.TwoPhaseActionState
TwoPhaseActionState.TwoPhaseAction<T,
U> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final AxialPosition[]
The relative positions of the six adjacent hexes in a hexagonal grid.protected HexagonalGrid
The hexagonal grid representing the current state of the SpinHex board. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newSpinHexModel
with the initial board configuration.SpinHexModel
(byte[][] startingBoard, byte[][] targetBoard) Constructs a newSpinHexModel
with a specified starting board and target board. -
Method Summary
Modifier and TypeMethodDescriptionpuzzle.State
<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>> clone()
Creates a clone of the currentSpinHexModel
state.boolean
Checks if thisSpinHexModel
is equal to another object.getBoard()
Gets the current state of the SpinHex board.int
Gets the size of the SpinHex board.getHex
(AxialPosition position) Gets the color of the hex at the specified position.Gets the set of legal moves available from the current state.getNeighbors
(AxialPosition position) Gets the neighbors of the hex at the specified position.Gets the solved configuration of the SpinHex board.int
hashCode()
Returns a hash code value for thisSpinHexModel
.boolean
isLegalMove
(TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation> moveAction) Checks if the specified move action is legal.boolean
Checks if it is legal to move from the specified position.boolean
isSolved()
Checks if the puzzle is solved.void
makeMove
(TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation> moveAction) Makes a move based on the specified action.toString()
Returns a string representation of theSpinHexModel
board.
-
Field Details
-
board
The hexagonal grid representing the current state of the SpinHex board. It contains the colors of the hexes in the game. -
ADJACENT_DIRECTIONS
The relative positions of the six adjacent hexes in a hexagonal grid. These positions are used to determine the neighbors of a hex and to perform rotations.
-
-
Constructor Details
-
SpinHexModel
public SpinHexModel()Constructs a newSpinHexModel
with the initial board configuration. -
SpinHexModel
public SpinHexModel(byte[][] startingBoard, byte[][] targetBoard) Constructs a newSpinHexModel
with a specified starting board and target board.- Parameters:
startingBoard
- The initial configuration of the board.targetBoard
- The solved configuration of the board.
-
-
Method Details
-
getSolution
Gets the solved configuration of the SpinHex board.- Returns:
- A deep copy of the HexagonalGrid representing the solved board configuration.
-
getBoard
Gets the current state of the SpinHex board.- Returns:
- A deep copy of the HexagonalGrid representing the current board state.
-
getBoardSize
public int getBoardSize()Gets the size of the SpinHex board.- Returns:
- The size of the board (N for an NxN board).
-
getHex
Gets the color of the hex at the specified position.- Parameters:
position
- The position of the hex.- Returns:
- The color of the hex at the specified position.
- Throws:
IllegalArgumentException
- if the position is out of bounds.
-
getNeighbors
Gets the neighbors of the hex at the specified position.- Parameters:
position
- The position of the hex.- Returns:
- A list of colors of the neighboring hexes.
-
isLegalToMoveFrom
Checks if it is legal to move from the specified position. A move is legal if the position is within bounds, the hex at that position is not empty, and all its neighbors are non-empty.- Specified by:
isLegalToMoveFrom
in interfaceTwoPhaseActionState<AxialPosition, Rotation>
- Parameters:
from
- The position to check.- Returns:
true
if it is legal to move from the specified position,false
otherwise.
-
isSolved
public boolean isSolved()Checks if the puzzle is solved. The puzzle is considered solved if all hexes match the solved configuration.- Specified by:
isSolved
in interfacepuzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
- Returns:
true
if the puzzle is solved,false
otherwise.
-
getLegalMoves
Gets the set of legal moves available from the current state. This method computes the legal moves based on the current board size and caches the result for efficiency.- Specified by:
getLegalMoves
in interfacepuzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
- Returns:
- A set of legal moves.
-
isLegalMove
Checks if the specified move action is legal. A move action is legal if it is possible to move from the specified position and the action is notnull
.- Specified by:
isLegalMove
in interfacecommon.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
- Parameters:
moveAction
- The move action to check.- Returns:
true
if the move action is legal,false
otherwise.
-
makeMove
Makes a move based on the specified action. This method performs the rotation of the hexes around the specified position in the direction specified by the action.- Specified by:
makeMove
in interfacecommon.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
- Parameters:
moveAction
- The move action to perform.
-
clone
Creates a clone of the currentSpinHexModel
state.- Specified by:
clone
in interfacepuzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
- Overrides:
clone
in classObject
- Returns:
- A new
SpinHexModel
instance with the same board configuration.
-
toString
-
equals
Checks if thisSpinHexModel
is equal to another object. TwoSpinHexModels
are considered equal if their boards have the same configuration. -
hashCode
-