Skip to content

About

Quant club selection coding task

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Sokoban Game Engine & Automated BFS Solver

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).

🚀 Project Architecture

The codebase is split into two primary components to separate the interactive gameplay from the heavy algorithmic processing:

  1. main.cpp (Interactive Engine): An Object-Oriented, cross-platform terminal game that allows a user to play Sokoban using standard keyboard inputs.
  2. 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.

🧠 Algorithmic Optimizations (The Solver)

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 as INVALID and aborts that branch, saving massive amounts of compute time.
  • Deterministic Visited Map: An unordered_map tracks 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.

🎮 How to Compile and Run

Because this project uses raw terminal manipulation, it must be compiled using a standard C++ compiler (like g++).

1. The Interactive Game (main.cpp)

Open your terminal or command prompt and compile the interactive engine:

g++ main.cpp -o game
./game     # On Linux/WSL/Mac
game.exe   # On Windows

Controls:

  • W - Move Up
  • S - Move Down
  • A - Move Left
  • D - Move Right
  • CTRL+C - Quit

2. The Automated Solver (engine.cpp)

Compile and run the algorithmic solver:

g++ engine.cpp -o solver
./solver     # On Linux/WSL/Mac
solver.exe   # On Windows

🧩 Custom Level Format

Both 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).

About

Quant club selection coding task

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages