Package mazealgo.model.algorithms.search
Class ASearchingAlgorithm
java.lang.Object
mazealgo.model.algorithms.search.ASearchingAlgorithm
- All Implemented Interfaces:
ISearchingAlgorithm
- Direct Known Subclasses:
BreadthFirstSearch,DepthFirstSearch
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final SolutionbuildSolution(AState lastState) Helper used by subclasses to construct aSolutionwith 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.intprotected voidonNodeEvaluated(AState state) Called by subclasses each time a node is committed to the visited set.protected voidreset()Resets per-solve state.voidsetNodeEvaluatedListener(Consumer<AState> listener) Registers a listener invoked once per node the algorithm visits (synchronously, on the thread runningISearchingAlgorithm.solve(ISearchable)).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface mazealgo.model.algorithms.search.ISearchingAlgorithm
getName, solve
-
Field Details
-
numberOfNodesEvaluated
protected int numberOfNodesEvaluated
-
-
Constructor Details
-
ASearchingAlgorithm
public ASearchingAlgorithm()
-
-
Method Details
-
setNodeEvaluatedListener
Registers a listener invoked once per node the algorithm visits (synchronously, on the thread runningISearchingAlgorithm.solve(ISearchable)). Passnullto clear. Used by the UI's "Watch Search" mode to paint visited cells as the search proceeds. -
onNodeEvaluated
Called by subclasses each time a node is committed to the visited set. -
buildSolution
Helper used by subclasses to construct aSolutionwith 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,nullfor 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 callsuper.reset(). -
getNumberOfNodesEvaluated
public int getNumberOfNodesEvaluated()- Specified by:
getNumberOfNodesEvaluatedin interfaceISearchingAlgorithm
-