Class ASearchingAlgorithm

java.lang.Object
mazealgo.model.algorithms.search.ASearchingAlgorithm
All Implemented Interfaces:
ISearchingAlgorithm
Direct Known Subclasses:
BreadthFirstSearch, DepthFirstSearch

public abstract class ASearchingAlgorithm extends Object implements ISearchingAlgorithm
Base class for search algorithms. Owns the node-evaluation counter, a reset() hook that subclasses must call at the top of ISearchingAlgorithm.solve(ISearchable), and an optional listener that lets a UI watch the search happen in real time.

Resetting matters for the server-client layer: a SolveMazeStrategy may pool searcher instances across many client requests, and a leftover counter (or, for BFS, a non-empty open list) from a previous solve would silently corrupt the next response.

The listener is fired once per state the algorithm pops off its open structure and commits to (i.e. each time numberOfNodesEvaluated is incremented). Defaults to a no-op so the server path pays nothing.

  • Field Details

    • numberOfNodesEvaluated

      protected int numberOfNodesEvaluated
  • Constructor Details

    • ASearchingAlgorithm

      public ASearchingAlgorithm()
  • Method Details

    • setNodeEvaluatedListener

      public void setNodeEvaluatedListener(Consumer<AState> listener)
      Registers a listener invoked once per node the algorithm visits (synchronously, on the thread running ISearchingAlgorithm.solve(ISearchable)). Pass null to clear. Used by the UI's "Watch Search" mode to paint visited cells as the search proceeds.
    • onNodeEvaluated

      protected void onNodeEvaluated(AState state)
      Called by subclasses each time a node is committed to the visited set.
    • buildSolution

      protected final Solution buildSolution(AState lastState)
      Helper used by subclasses to construct a Solution with the current node-evaluation count already stamped on it — so the count survives the wire when a server returns a Solution to a remote client.
      Parameters:
      lastState - the goal state if found, null for an unsolvable maze (yields an empty path)
    • reset

      protected void reset()
      Resets per-solve state. Subclasses with their own mutable state (e.g. an open list) should override this and call super.reset().
    • getNumberOfNodesEvaluated

      public int getNumberOfNodesEvaluated()
      Specified by:
      getNumberOfNodesEvaluated in interface ISearchingAlgorithm