🧩 Puzzle State Example
State in action: Not every puzzle is just solved or unsolved — some have phases. A player might start it, make progress, then finally crack it.
Why it matters: Instead of messy if checks, each state gets its own class, making puzzle logic clean, extensible, and easy to follow.
Scenario
Section titled “Scenario”A puzzle in the escape room isn’t just “solved or unsolved.”
It might have progression:
-
Unsolved → Player hasn’t interacted yet.
-
In Progress → Player made progress (entered part of the code, found part of the clue).
-
Solved → Puzzle is complete.
Instead of tracking this with a boolean or lots of if statements, we give each state its own class.
PuzzleState Interface
Section titled “PuzzleState Interface”public interface PuzzleState { void interact(Puzzle puzzle);}Concrete States
Section titled “Concrete States”// Unsolved statepublic class UnsolvedState implements PuzzleState { @Override public void interact(Puzzle puzzle) { System.out.println("You start working on the puzzle..."); puzzle.setState(new InProgressState()); }}
// In progress statepublic class InProgressState implements PuzzleState { @Override public void interact(Puzzle puzzle) { System.out.println("You're making progress on the puzzle..."); puzzle.setState(new SolvedState()); }}
// Solved statepublic class SolvedState implements PuzzleState { @Override public void interact(Puzzle puzzle) { System.out.println("The puzzle is already solved!"); }}Puzzle Context
Section titled “Puzzle Context”public class Puzzle { private PuzzleState state;
public Puzzle() { this.state = new UnsolvedState(); }
public void setState(PuzzleState state) { this.state = state; }
public void interact() { state.interact(this); }}Using the Puzzle State
Section titled “Using the Puzzle State”public class EscapeRoomGame { public static void main(String[] args) { Puzzle puzzle = new Puzzle();
puzzle.interact(); // "You start working on the puzzle..." puzzle.interact(); // "You're making progress on the puzzle..." puzzle.interact(); // "The puzzle is already solved!" }}Sample Output
Section titled “Sample Output”You start working on the puzzle...You're making progress on the puzzle...The puzzle is already solved!