Quant Club Selection - Task 2 By: Ankit Agrawal 25IM10037
This project is a terminal-based implementation of the classic puzzle game Sokoban, developed entirely in C++. It features two distinct executables: a fully interactive playable game engine, and an automated algorithmic solver that computes the optimal path using an optimized Breadth-First Search (BFS).
The codebase is split into two primary components to separate the interactive gameplay from the heavy algorithmic processing:
main.cpp(Interactive Engine): An Object-Oriented, cross-platform terminal game that allows a user to play Sokoban using standard keyboard inputs.engine.cpp(Automated Solver): A headless algorithmic engine that takes a level string, searches the state-space for the optimal solution, and visually plays back the resulting LURD (Left, Up, Right, Down) sequence.
Sokoban is a PSPACE-complete problem. To prevent memory exhaustion and infinite loops during the BFS state-space search, several optimizations were implemented in engine.cpp:
- State Compression: Storing full 2D grids in the BFS queue is highly inefficient. The engine compresses the board state into a lightweight string format (
Px,Py|Bx,By|Bx,By) by extracting the coordinates of the player and sorting the coordinates of the boxes. This drastically reduces the memory footprint of the BFS queue. - Corner Deadlock Detection (
isDeadlock): The solver actively prunes the search tree. If a move pushes a box into a corner (surrounded by two adjacent walls) where it cannot reach a target, the engine immediately flags the state asINVALIDand aborts that branch, saving massive amounts of compute time. - Deterministic Visited Map: An
unordered_maptracks previously visited states to prevent cyclic loops, storing the parent state and the specific move (U,D,L,R) that led there. This allows the engine to backtrack and extract the final shortest path once the win state is found.
Because this project uses raw terminal manipulation, it must be compiled using a standard C++ compiler (like g++).
Open your terminal or command prompt and compile the interactive engine:
g++ main.cpp -o game
./game # On Linux/WSL/Mac
game.exe # On WindowsControls:
- W - Move Up
- S - Move Down
- A - Move Left
- D - Move Right
- CTRL+C - Quit
Compile and run the algorithmic solver:
g++ engine.cpp -o solver
./solver # On Linux/WSL/Mac
solver.exe # On WindowsBoth engines support dynamically loading custom levels via terminal input. The engine parses a custom string format designed to flatten a 2D grid into a 1D string.
Format: [Width]:[LevelData]
Legend:
#= Wall@= Player+= Player on Target$= Box*= Box on Target.= Target= Floor (Space)
Example Custom String (Default Level):
13: ######### # # # # # # ## #### #.# $@$ #.# ## # ## # # ###### ### ### ###### ## ### ####
(Note: The engine automatically calculates padding and grid dimensions based on the provided Width integer).