Class ThreeJugsState
java.lang.Object
puzzles.jugs.ThreeJugsState
- All Implemented Interfaces:
common.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>, common.TwoPhaseMoveState<Integer, ThreeJugsState>, common.util.Copyable<ThreeJugsState>, puzzle.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>, puzzle.TwoPhaseMoveState<Integer, ThreeJugsState>
public class ThreeJugsState
extends Object
implements puzzle.TwoPhaseMoveState<Integer, ThreeJugsState>
Represents the states of the three jugs problem, also known as a water
pouring puzzle. Consider three jugs with capacities 3, 5, and 8 liters,
respectively. Initially, the two smaller jugs are full of water, and the
largest jug is empty. The goal of the puzzle is to have 4 liters of water
in each of the larger two jugs. In a move, water can be poured from a
non-empty jug into another that is not fully loaded.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface common.TwoPhaseMoveState
common.TwoPhaseMoveState.TwoPhaseMove<T> -
Constructor Summary
ConstructorsConstructorDescriptionCreates aThreeJugsStateinstance representing the initial state of the puzzle.ThreeJugsState(ThreeJugsState other) Creates a deep copy of the specifiedThreeJugsStateobject. -
Method Summary
Modifier and TypeMethodDescriptioncopy()booleanReturns the set of all moves that can be applied to the state.inthashCode()booleanisLegalMove(common.TwoPhaseMoveState.TwoPhaseMove<Integer> move) Returns whether it is possible to pour a source jug into a target jug.booleanisLegalToMoveFrom(Integer from) Returns whether is it possible to pour the source jug specified into any of the other jugs, i.e., whether the source jug is non-empty.booleanisSolved()Returns whether the puzzle is solved.voidPours the source jug specified into the target jug specified.toString()
-
Constructor Details
-
ThreeJugsState
public ThreeJugsState()Creates aThreeJugsStateinstance representing the initial state of the puzzle. -
ThreeJugsState
Creates a deep copy of the specifiedThreeJugsStateobject.- Parameters:
other- the object to be copied
-
-
Method Details
-
isSolved
public boolean isSolved()Returns whether the puzzle is solved.- Specified by:
isSolvedin interfacepuzzle.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>- Returns:
- whether the puzzle is solved
-
isLegalMove
Returns whether it is possible to pour a source jug into a target jug. The jugs are identified with the integers 0, 1, and 2, respectively.- Specified by:
isLegalMovein interfacecommon.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>- Parameters:
move- wraps the numbers of the source and the target jugs- Returns:
- whether it is possible to pour a source jug into a target jug
-
makeMove
Pours the source jug specified into the target jug specified. The jugs are identified with the integers 0, 1, and 2, respectively.- Specified by:
makeMovein interfacecommon.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>- Parameters:
move- wraps the numbers of the source and the target jugs
-
getLegalMoves
Returns the set of all moves that can be applied to the state.- Specified by:
getLegalMovesin interfacecommon.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>- Returns:
- the set of all moves that can be applied to the state
-
isLegalToMoveFrom
Returns whether is it possible to pour the source jug specified into any of the other jugs, i.e., whether the source jug is non-empty. The jugs are identified with the integers 0, 1, and 2, respectively.- Specified by:
isLegalToMoveFromin interfacecommon.TwoPhaseMoveState<Integer, ThreeJugsState>- Parameters:
from- the number of the source jug- Returns:
- whether is it possible to pour the source jug specified into any of the other jugs, i.e., whether the source jug is non-empty
-
copy
- Specified by:
copyin interfacecommon.util.Copyable<ThreeJugsState>- Specified by:
copyin interfacecommon.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>
-
equals
-
hashCode
-
toString
-