# mohjb/sokobansolver architecture

> Architecture diagram of the GitHub repository mohjb/sokobansolver, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohjb/sokobansolver
- Repository: https://github.com/mohjb/sokobansolver

## Overview

##### Project type & purpose (from README + tree)
This is a **Java Sokoban puzzle solver** implementing **A\*** search (“A* application”). It’s not a distributed system; it’s a **single-process algorithmic tool** that:
- loads Sokoban levels (likely from `levels.levels`),
- represents game states,
- generates legal moves (actions),
- runs A* (priority queue / open set, closed set),
- outputs a solution path (sequence of moves / actions).

##### What the file structure implies architecturally
Relevant files:
- `githubMohJBSokobanSolver/src/SokobanSolver.java`
Likely the main solver entry and A* driver (open/closed sets, loop, termination).
- `githubMohJBSokobanSolver/src/OldSokobanSolver.java`
- `githubMohJBSokobanSolver/src/SokoAct.java`
Likely “Sokoban Action” (move representation), or action generator.
- `githubMohJBSokobanSolver/src/levels.levels`
Input dataset of levels.
- `SokoSo.java` (top-level, outside Eclipse project folder)
Possibly another entry point / wrapper / earlier version.

Also present: Eclipse metadata (`.project`, `.settings`) → indicates a local IDE project, not a packaged multi-module system.

##### Entry Point / Orchestration
- `SokobanSolver` (and possibly `SokoSo`)
- Responsibilities:
- select level
- initialize state
- call solver
- print solution / stats

##### Level Repository + Parser
- `levels.levels` (data)
- “LevelLoader/Parser” (may be embedded in `SokobanSolver`)
- Responsibilities:
- read level text
- produce initial board/state representation

##### Domain Model (Game State / Board Representation)
- Even if not explicitly named in tree, the solver must have:
- Board grid (walls, goals)
- Player position
- State hashing/equality (critical for closed set)
- If this is embedded in `SokobanSolver`, represent it as an internal component/class.

##### Action / Move Representation
- `SokoAct`
- Responsibilities:
- Moves (Up/Down/Left/Right)
- possibly also pushes vs moves
- apply action to state (transition function), or describe it

##### Successor Generator (Legal Moves)
- Often implemented using the domain model + action representation.
- Responsibilities:
- from a state, enumerate legal next states

##### A\* Search Engine
- Likely `SokobanSolver`
- **Open Set** (PriorityQueue ordered by `f = g + h`)
- **Closed Set** (HashSet/Map of visited states)
- **Node/Record** (state + parent + action + g + h + f)
- Responsibilities:
- loop: pop best node, goal test, expand successors, update open/closed
- reconstruct solution path via parent pointers

##### Heuristic Function
- deadlock detection (if present)
- Responsibilities:
- estimate remaining cost
- must be admissible/consistent if optimality is desired

##### Solution Reconstruction + Output
- Responsibilities:
- trace parent links
- format sequence of moves
- print/write stats (expanded nodes, runtime, solution length)

##### Legacy Solver (optional)
- `OldSokobanSolver` as “deprecated/previous algorithm”
- as an alternate implementation behind a “Solver Interface”, or

##### Architectural patterns/design principles used (explicitly call them out)
Even though it’s a small repo, it still has recognizable patterns:
- **Search Pipeline / Algorithmic Architecture**: parse → model → search → output
- **Separation of concerns** (should be depicted even if code is somewhat merged):
- state representation vs search strategy vs heuristic vs IO
- **Graph search pattern**:
- node expansion
- frontier management
- visited/closed tracking
- **Heuristic strategy**:
- heuristic is a pluggable concept; show it as a replaceable component (even if not coded that way)

##### Loop
- pop min-f node
- goal test
- generate successors
- compute g/h/f
- update Open/Closed

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =========================
  %% Context Diagram (Top)
  %% =========================
  subgraph "System Context"
    U["User / Puzzle Runner"]:::external
    APP["Sokoban Solver (Java A* tool)"]:::app
    LVL[("Levels File\nlevels.levels")]:::data
    OUT[("Solution Output\n(moves + stats)")]:::data

    U -->|"runs"| APP
    APP -.->|"reads_levels"| LVL
    APP -->|"prints_solution"| OUT
  end

  %% =========================
  %% Single-Process Runtime (JVM)
  %% =========================
  subgraph "Runtime Boundary: Single JVM Process"
    direction TB

    subgraph "CLI / Entry Points"
      direction TB
      EP["Entry Point / Orchestration\n(SokobanSolver.main)"]:::module
      WRAP["Alternate / Wrapper Entry\n(SokoSo)"]:::module
    end

    subgraph "IO Subsystem"
      direction TB
      PARSER["Level Loader / Parser"]:::module
      FORMAT["Output Formatter"]:::module
    end

    subgraph "Core Solver Engine"
      direction TB
      ASTAR["A* Search Engine\n(main loop + node expansion)"]:::module

      subgraph "Core Data Structures"
        direction TB
        OPEN["Open Set\nPriorityQueue (min-f)"]:::ds
        CLOSED["Closed Set\nHashSet/HashMap (visited/gScore)"]:::ds
        NODE["Search Node / Record\n(state,parent,action,g,h,f)"]:::ds
      end

      HEU["Heuristic Function h(n)\n(e.g., Manhattan + deadlocks?)"]:::module
      SUCC["Successor Generator\n(legal moves enumeration)"]:::module
      DOMAIN["Domain Model\nGameState / Board\n(hash + equals + goal test)"]:::domain
      ACT["Action Representation\n(SokoAct: U/D/L/R, push/move)"]:::module
      RECON["Solution Reconstruction\n(trace parents → actions)"]:::module
    end

    LEG["Legacy / Reference Solver\n(OldSokobanSolver)"]:::legacy
  end

  %% =========================
  %% Key Interactions / Data Flows
  %% =========================
  APP -->|"invokes"| EP
  APP -->|"invokes"| WRAP

  EP -->|"load_level(id)"| PARSER
  WRAP -->|"delegate_run"| EP

  PARSER -.->|"reads"| LVL
  PARSER -->|"initial_GameState"| DOMAIN

  EP -->|"solve(initial_state)"| ASTAR

  ASTAR -->|"init_push Node(g=0,h,f)"| OPEN
  ASTAR -->|"pop_min_f"| OPEN
  ASTAR -->|"mark_visited / store_gScore"| CLOSED

  ASTAR -->|"expand(state)"| SUCC
  SUCC -->|"uses_rules"| DOMAIN
  SUCC -->|"uses_moves"| ACT
  SUCC -->|"generate next_states + actions"| ASTAR

  ASTAR -->|"compute_h(state)"| HEU
  HEU -->|"reads board/positions"| DOMAIN

  ASTAR -->|"create/update Node(parent,action,g,h,f)"| NODE
  ASTAR -->|"push/update"| OPEN
  ASTAR -->|"check_seen/relax"| CLOSED

  ASTAR -->|"goal_found(node)"| RECON
  RECON -->|"List<Action> (solution path)"| FORMAT
  FORMAT -->|"print/write"| OUT

  EP -.->|"optional_reference"| LEG

  %% =========================
  %% A* Flow (Algorithm View)
  %% =========================
  subgraph "A* Flow (Algorithm Pipeline)"
    direction TB
    F1["1) Load level → initial state"]:::flow
    F2["2) Open ← initial node"]:::flow
    F3["3) Loop: pop min-f"]:::flow
    F4["4) Goal test"]:::flow
    F5["5) Generate successors"]:::flow
    F6["6) g/h/f; update Open/Closed"]:::flow
    F7["7) Reconstruct + output solution"]:::flow

    F1 --> F2 --> F3 --> F4
    F4 -->|"not_goal"| F5 --> F6 --> F3
    F4 -->|"goal"| F7
  end

  %% Tie flow to components (light coupling)
  F1 -.-> PARSER
  F2 -.-> OPEN
  F3 -.-> OPEN
  F5 -.-> SUCC
  F6 -.-> CLOSED
  F6 -.-> HEU
  F7 -.-> RECON

  %% =========================
  %% Click Events (from mapping)
  %% =========================
  click EP "https://github.com/mohjb/sokobansolver/blob/master/githubMohJBSokobanSolver/src/SokobanSolver.java"
  click ASTAR "https://github.com/mohjb/sokobansolver/blob/master/githubMohJBSokobanSolver/src/SokobanSolver.java"
  click ACT "https://github.com/mohjb/sokobansolver/blob/master/githubMohJBSokobanSolver/src/SokoAct.java"
  click LVL "https://github.com/mohjb/sokobansolver/blob/master/githubMohJBSokobanSolver/src/levels.levels"
  click LEG "https://github.com/mohjb/sokobansolver/blob/master/githubMohJBSokobanSolver/src/OldSokobanSolver.java"
  click WRAP "https://github.com/mohjb/sokobansolver/blob/master/SokoSo.java"

  %% =========================
  %% Styles (Color + Shapes)
  %% =========================
  classDef external fill:#f4f6f8,stroke:#6b7280,stroke-width:1px,color:#111827;
  classDef app fill:#1f6feb,stroke:#0b3d91,stroke-width:1px,color:#ffffff;
  classDef module fill:#60a5fa,stroke:#1d4ed8,stroke-width:1px,color:#0b1020;
  classDef domain fill:#86efac,stroke:#16a34a,stroke-width:1px,color:#052e16;
  classDef ds fill:#fdba74,stroke:#c2410c,stroke-width:1px,color:#431407;
  classDef data fill:#e5e7eb,stroke:#6b7280,stroke-width:1px,color:#111827;
  classDef legacy fill:#fca5a5,stroke:#b91c1c,stroke-width:1px,color:#450a0a;
  classDef flow fill:#ddd6fe,stroke:#6d28d9,stroke-width:1px,color:#1f1147;
```
