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

    Constructors
    Constructor
    Description
    Creates a ThreeJugsState instance representing the initial state of the puzzle.
    Creates a deep copy of the specified ThreeJugsState object.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    boolean
     
    Set<common.TwoPhaseMoveState.TwoPhaseMove<Integer>>
    Returns the set of all moves that can be applied to the state.
    int
     
    boolean
    isLegalMove(common.TwoPhaseMoveState.TwoPhaseMove<Integer> move)
    Returns whether it is possible to pour a source jug into a target jug.
    boolean
    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.
    boolean
    Returns whether the puzzle is solved.
    void
    makeMove(common.TwoPhaseMoveState.TwoPhaseMove<Integer> move)
    Pours the source jug specified into the target jug specified.
     

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ThreeJugsState

      public ThreeJugsState()
      Creates a ThreeJugsState instance representing the initial state of the puzzle.
    • ThreeJugsState

      public ThreeJugsState(ThreeJugsState other)
      Creates a deep copy of the specified ThreeJugsState object.
      Parameters:
      other - the object to be copied
  • Method Details

    • isSolved

      public boolean isSolved()
      Returns whether the puzzle is solved.
      Specified by:
      isSolved in interface puzzle.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>
      Returns:
      whether the puzzle is solved
    • isLegalMove

      public boolean isLegalMove(common.TwoPhaseMoveState.TwoPhaseMove<Integer> move)
      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:
      isLegalMove in interface common.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

      public void makeMove(common.TwoPhaseMoveState.TwoPhaseMove<Integer> move)
      Pours the source jug specified into the target jug specified. The jugs are identified with the integers 0, 1, and 2, respectively.
      Specified by:
      makeMove in interface common.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>
      Parameters:
      move - wraps the numbers of the source and the target jugs
    • getLegalMoves

      public Set<common.TwoPhaseMoveState.TwoPhaseMove<Integer>> getLegalMoves()
      Returns the set of all moves that can be applied to the state.
      Specified by:
      getLegalMoves in interface common.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>
      Returns:
      the set of all moves that can be applied to the state
    • isLegalToMoveFrom

      public boolean isLegalToMoveFrom(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. The jugs are identified with the integers 0, 1, and 2, respectively.
      Specified by:
      isLegalToMoveFrom in interface common.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

      public ThreeJugsState copy()
      Specified by:
      copy in interface common.util.Copyable<ThreeJugsState>
      Specified by:
      copy in interface common.State<common.TwoPhaseMoveState.TwoPhaseMove<Integer>, ThreeJugsState>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object