smithers.logicpuzzles
Class AbstractPuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>

java.lang.Object
  extended by smithers.logicpuzzles.AbstractPuzzleSolver<P,Q>
All Implemented Interfaces:
PuzzleSolver<P,Q>
Direct Known Subclasses:
MosaicSolver, SlitherlinkSolver

public abstract class AbstractPuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
extends java.lang.Object
implements PuzzleSolver<P,Q>

Class to aid implementations of PuzzleSolver.


Field Summary
protected  P puzzle
           
protected  Q state
           
 
Constructor Summary
AbstractPuzzleSolver(P puzzle)
          Creates a new solver for the specified puzzle.
AbstractPuzzleSolver(Q state)
          Creates a new solver to work from the specified state.
 
Method Summary
 P getPuzzle()
          Returns the puzzle which is being solved.
 Q getState()
          Returns the state of the solution attempt.
abstract  Q makeState(P puzzle)
          Makes a new state for the specified puzzle.
abstract  boolean nextStep()
          Advances to the next step of solving the puzzle.
 boolean pass()
          Performs a single pass through the puzzle.
 boolean solve()
          Attempts to completely solve the puzzle.
abstract  int solveStep()
          Attempts to solve the current step of the puzzle.
 boolean step()
          Performs a single step in the solving process.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface smithers.logicpuzzles.PuzzleSolver
start
 

Field Detail

puzzle

protected P extends Puzzle puzzle

state

protected Q extends PuzzleState<P> state
Constructor Detail

AbstractPuzzleSolver

public AbstractPuzzleSolver(P puzzle)
Creates a new solver for the specified puzzle.

Parameters:
puzzle - the puzzle to solve

AbstractPuzzleSolver

public AbstractPuzzleSolver(Q state)
Creates a new solver to work from the specified state.

Parameters:
state - the puzzle state to work from
Method Detail

getPuzzle

public P getPuzzle()
Returns the puzzle which is being solved.

Specified by:
getPuzzle in interface PuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
Returns:
the Puzzle which this PuzzleSolver relates to.

getState

public Q getState()
Returns the state of the solution attempt. This method should always return the same object, with updates being made to it as the solution progresses.

Specified by:
getState in interface PuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
Returns:
the Puzzle which this State relates to.

makeState

public abstract Q makeState(P puzzle)
Makes a new state for the specified puzzle. Usually this will simply call a constructor which takes puzzle as an argument.

Parameters:
puzzle - the puzzle to create a state for
Returns:
a state for the given puzzle

solveStep

public abstract int solveStep()
Attempts to solve the current step of the puzzle. This method must return 0 if it made no changes, 1 if it made a change to its internal data, but no change to the state of the puzzle, or any number greater than 1 if a change has been made to the state.

Returns:
the return value described above

nextStep

public abstract boolean nextStep()
Advances to the next step of solving the puzzle.

Returns:
true iff the solver has started a new 'pass'

step

public boolean step()
Performs a single step in the solving process. This method should do the smallest amount of work which results in a change being made to puzzle.

Specified by:
step in interface PuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
Returns:
true iff a change was made

pass

public boolean pass()
Performs a single pass through the puzzle. This method should perform some small amount of the solution to the puzzle. Typically, this method should perform roughly a single pass through the clues of the puzzle, or through one subcollection of the clues (such as the row or column clues). The work done by this method should ideally lie somewhere between step and solve. If no intermediate is available, this should perform the same function as step. Note that unlike the other solving methods, this method may return false even when this solver can still make further deductions.

Specified by:
pass in interface PuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
Returns:
true iff a change was made

solve

public boolean solve()
Attempts to completely solve the puzzle. If the puzzle cannot be solved, this ahould solve as much as is possible. Calling this method should be equivalent to while (this.step()); and to while (this.pass());

Specified by:
solve in interface PuzzleSolver<P extends Puzzle,Q extends PuzzleState<P>>
Returns:
true iff a change was made