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

public class SpinHexModel extends Object implements TwoPhaseActionState<AxialPosition, Rotation>
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
The model keeps track of the current state of the board and provides methods to query and manipulate the state according to the game rules.
  • Field Details

    • board

      protected HexagonalGrid board
      The hexagonal grid representing the current state of the SpinHex board. It contains the colors of the hexes in the game.
    • ADJACENT_DIRECTIONS

      protected static final AxialPosition[] 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 new SpinHexModel with the initial board configuration.
    • SpinHexModel

      public SpinHexModel(byte[][] startingBoard, byte[][] targetBoard)
      Constructs a new SpinHexModel 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

      public HexagonalGrid getSolution()
      Gets the solved configuration of the SpinHex board.
      Returns:
      A deep copy of the HexagonalGrid representing the solved board configuration.
    • getBoard

      public HexagonalGrid 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

      public Byte getHex(AxialPosition position)
      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

      public List<Byte> getNeighbors(AxialPosition position)
      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

      public boolean isLegalToMoveFrom(AxialPosition from)
      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 interface TwoPhaseActionState<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 interface puzzle.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 interface puzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
      Returns:
      A set of legal moves.
    • isLegalMove

      public boolean isLegalMove(TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation> moveAction)
      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 not null.
      Specified by:
      isLegalMove in interface common.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
      Parameters:
      moveAction - The move action to check.
      Returns:
      true if the move action is legal, false otherwise.
    • makeMove

      public void makeMove(TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation> moveAction)
      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 interface common.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
      Parameters:
      moveAction - The move action to perform.
    • clone

      Creates a clone of the current SpinHexModel state.
      Specified by:
      clone in interface puzzle.State<TwoPhaseActionState.TwoPhaseAction<AxialPosition, Rotation>>
      Overrides:
      clone in class Object
      Returns:
      A new SpinHexModel instance with the same board configuration.
    • toString

      public String toString()
      Returns a string representation of the SpinHexModel board. Each hex is represented by its ordinal value, separated by tabs.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the board.
    • equals

      public boolean equals(Object o)
      Checks if this SpinHexModel is equal to another object. Two SpinHexModels are considered equal if their boards have the same configuration.
      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 value for this SpinHexModel. The hash code is computed based on the board configuration.
      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this SpinHexModel.