|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.pacbell.jfai.toh.domain.Solver
public class Solver
There is a base with three pins to accept disks. Each disk has a certain size. At the beginning all disks are stacked on one pin. The goal is to move the stack to another empty pin.
Legal moves:
An instance of this class implements the solution of the problem of moving the initial tower from the source pin to the target pin.
Before invoking solve()
, you may configure an instance by
setting the number of disks, the source pin and the target pin.
Bound Bean Properties:
Nested Class Summary | |
---|---|
class |
Solver.AbstractState
Abstract superclass for all state classes of Solver. |
class |
Solver.Idle
State while Solver is being configured. |
static interface |
Solver.IStateListener
An object that wants to be notified about specific state changes can implement this interface. |
class |
Solver.Paused
State while the solver is paused. |
class |
Solver.Solving
State while Solver is running to solve the puzzle. |
Field Summary | |
---|---|
(package private) static Log |
LOG
The trace file. |
static String |
PROPERTY_MOVE_COUNT
Name of bound property: number of disks. |
static String |
PROPERTY_NUMBER_OF_DISKS
Name of bound property: number of disks. |
static String |
PROPERTY_SOURCE
Name of bound property: source pin. |
static String |
PROPERTY_STATE
Name of bound property: solver's state. |
static String |
PROPERTY_TARGET
Name of bound property: target pin. |
Constructor Summary | |
---|---|
Solver()
Creates an idle instance with default number of pins and disks. |
Method Summary | |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a listener for changes to any property. |
void |
addPropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Adds a listener for changes to the given property. |
void |
addStateListener(Solver.IStateListener listener)
Adds the listener to receive a notification if the state changes. |
Base |
getBase()
Gets the base. |
List<Disk> |
getDisks()
Answer a list of all the disks in the puzzle, sorted by descending size. |
long |
getMoveCount()
Answers the number of moves that the solver has executed. |
protected long |
getMoveCounter()
Gets the number of executed moves. |
int |
getNumberOfDisks()
Gets the number of disks. |
long |
getRemainingMoves()
Answers the number of remaining moves that the solver has to execute. |
protected Date |
getSolutionStartTime()
Gets the start time. |
Pin |
getSource()
Gets the source pin. |
Date |
getStartTime()
If the solver is idle, then the result is null. |
Solver.AbstractState |
getState()
Gets the state. |
Pin |
getTarget()
Gets the target pin. |
long |
getTotalMoves()
Answers the total number of moves necessary to solve the puzzle. |
static long |
getTotalMoves(int numberOfDisks)
Let toh(n) be the number of moves for a puzzle with n disks. |
boolean |
hasListeners(String propertyName)
Checks if there are any listeners for changes to the given property. |
void |
pause()
Pauses the solver. |
void |
removePropertyChangeListener(PropertyChangeListener listener)
The given listener does no longer receive property change events. |
void |
removePropertyChangeListener(String propertyName,
PropertyChangeListener listener)
The given listener does no longer receive property change events for the given property. |
void |
removeStateListener(Solver.IStateListener listener)
The listener does no longer receive notifications about state changes. |
void |
resume()
Resumes the solver. |
protected void |
setMoveCounter(long moveCounter)
Sets the number of executed moves. |
void |
setNumberOfDisks(int newValue)
Requests to change the number of disks at the source pin. |
protected void |
setSolutionStartTime(Date solutionStartTime)
Sets the time when the solution started. |
void |
setSource(Pin newSource)
Requests to change the source pin, which effectively moves all disks to this pin. |
protected void |
setState(Solver.AbstractState newState)
Sets the state. |
void |
setTarget(Pin newTarget)
Requests to change the target pin. |
void |
solve()
This is the entry point to solve the problem of moving the initial tower from the source pin to the target pin. |
(package private) void |
stackDisksAt(Pin pin)
Position all disks at the given pin. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String PROPERTY_MOVE_COUNT
public static final String PROPERTY_NUMBER_OF_DISKS
public static final String PROPERTY_SOURCE
public static final String PROPERTY_STATE
public static final String PROPERTY_TARGET
static final Log LOG
Constructor Detail |
---|
public Solver()
Method Detail |
---|
public static final long getTotalMoves(int numberOfDisks)
toh(1) = 1
To move a tower with n disks, move the upper (n-1) disks to the temporary spot, then move the lowest disk, then move the (n-1) disk tower from the temporary spot to the destination.toh(n) = toh(n-1) + 1 + toh(n-1) = 2 * toh(n-1) + 1 [ A ]
toh(2) = 1 + 1 + 1 = 3Hypothesis: toh(n) = 2 n - 1 [ B ]
Proof:
numberOfDisks
- the number of disks
public void addPropertyChangeListener(PropertyChangeListener listener)
listener
- listener who wants to receive property change eventspublic void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
propertyName
- the name of the property of interestlistener
- listener who wants to receive property change eventspublic void addStateListener(Solver.IStateListener listener)
listener
- the listenerpublic Base getBase()
public List<Disk> getDisks()
public long getMoveCount()
If the solver is idle, then the result is 0. Otherwise the result is the number of moves made so far.
public int getNumberOfDisks()
public long getRemainingMoves()
If the solver is idle the result is the total number of moves to solve the puzzle. Otherwise, the result is the number of remaining moves to solve the puzzle.
public Pin getSource()
public Date getStartTime()
public Solver.AbstractState getState()
public Pin getTarget()
public long getTotalMoves()
public boolean hasListeners(String propertyName)
propertyName
- the name of a property
public void pause()
public void removePropertyChangeListener(PropertyChangeListener listener)
listener
- a property change listenerpublic void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
propertyName
- a property namelistener
- a property change listenerpublic void removeStateListener(Solver.IStateListener listener)
listener
- the listenerpublic void resume()
public void setNumberOfDisks(int newValue)
newValue
- new number of diskspublic void setSource(Pin newSource)
newSource
- new source pinpublic void setTarget(Pin newTarget)
newTarget
- new target pinpublic void solve() throws ConfigurationException, InvalidMoveException, InterruptedException
ConfigurationException
- if an invalid configuration is detected
InvalidMoveException
- if an invalid move is detected
InterruptedException
- If the solving thread is interruptedprotected long getMoveCounter()
protected Date getSolutionStartTime()
protected void setMoveCounter(long moveCounter)
moveCounter
- the number of executed movesprotected void setSolutionStartTime(Date solutionStartTime)
solutionStartTime
- the start timeprotected void setState(Solver.AbstractState newState)
newState
- the new statevoid stackDisksAt(Pin pin)
pin
- a Pin
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |