Class TwoPhaseActionSelector<T,U>

java.lang.Object
jfxutils.TwoPhaseActionSelector<T,U>
Type Parameters:
T - the type representing the source position (from)
U - the type representing the action to apply
Direct Known Subclasses:
JFXTwoPhaseActionSelector

public class TwoPhaseActionSelector<T,U> extends Object
Utility class to determine the next action in two-phase action selection puzzles. It manages the state transitions between selecting a source position and selecting an action to apply at that position.
  • Field Details

  • Constructor Details

    • TwoPhaseActionSelector

      public TwoPhaseActionSelector(TwoPhaseActionState<T,U> state)
      Creates a TwoPhaseActionSelector to manage the action selection process for the given state.
      Parameters:
      state - the state on which actions will be performed
  • Method Details

    • getPhase

      public final TwoPhaseActionSelector.Phase getPhase()
      Returns the current selection phase.
      Returns:
      the current selection phase
    • setPhase

      protected void setPhase(TwoPhaseActionSelector.Phase phase)
      Sets the current selection phase. The method is provided to be overridden by subclasses.
      Parameters:
      phase - the current selection phase
    • isReadyToMove

      public final boolean isReadyToMove()
      Returns whether the move is ready to be made.
      Returns:
      true if the selection is in the READY_TO_MOVE phase, false otherwise.
    • selectFrom

      public void selectFrom(T from)
      Selects a source position from which an action will be performed. If the position is valid for making a move, the selection process advances to the action selection phase.
      Parameters:
      from - the position to select as the source
    • selectAction

      public void selectAction(U action)
      Selects an action to be performed at the previously selected source position. If the action is valid for the selected source, the selection process advances to the ready-to-move phase.
      Parameters:
      action - the action to perform at the selected source
    • getFrom

      public final T getFrom()
      Returns the source position selected.
      Returns:
      the source position selected
      Throws:
      IllegalStateException - if the selection is still in the SELECT_FROM phase, meaning no source has been selected yet.
    • getAction

      public final U getAction()
      Returns the action selected.
      Returns:
      the action selected
      Throws:
      IllegalStateException - if the selection is not in the READY_TO_MOVE phase.
    • isInvalidSelection

      public final boolean isInvalidSelection()
      Returns whether the last selection (i.e, for the source or the target respectively) was invalid.
      Returns:
      true if the last selection was invalid, false otherwise.
    • makeMove

      public final void makeMove()
      Makes the move selected. If the move is not yet ready to be made, then an IllegalStateException is thrown.
    • reset

      public final void reset()
      Resets the selection state, clearing both the source position and action selections.