A high-performance constraint satisfaction solver and data analytics pipeline for the real-time board game Nine Tiles Panic. Developed as a Computer Engineering undergraduate thesis at the Federal University of Rio de Janeiro (UFRJ).
To find the optimal city layout for any given game scenario, the solver must navigate a search space of 48.7 trillion theoretical spatial arrangements (
- Interactive Web App: Nine Tiles Panic Solver Interface
- Raw Dataset (4.27 GB): Hugging Face Hub
The project is structured into two decoupled phases: the generation engine and the analytical pipeline.
The physical rules of the game are modeled as a Constraint Satisfaction Problem (CSP). The solver (main.py and solver.py) explores the state space using:
- Parallelized Backtracking: The search tree is partitioned by its root nodes and executed concurrently across multiple CPU cores.
- Heuristic Pruning: Uses Minimum Remaining Values (MRV) and Forward Checking to prune invalid branches early.
-
Union-Find: A disjoint-set data structure is dynamically updated during the search to ensure acyclicity (preventing closed road loops) in
$O(\alpha(n))$ time.
The engine's output is serialized into highly compressed Apache Parquet files. The analytics pipeline (scripts/analytics/) processes this massive dataset out-of-core using DuckDB:
- Percentile Normalization: Standardizes 24 different board metrics into a uniform probability scale.
- Pareto Frontier Extraction: Filters out mathematically inferior boards across all 2,625 possible combinations of scoring objectives.
- Monte Carlo Simulation: Simulates 100,000 matches to evaluate scalarization strategies. Results show that both Weighted Product and Weighted Sum perform with statistical equivalence, sharing the highest win rates. The Weighted Product is ultimately adopted as the core decision heuristic due to its non-compensatory game design properties, punishing asymmetrical boards and ensuring strategically balanced layouts.
There are two primary ways to interact with this repository: using the pre-compiled data via the web interface, or running the full generation and analytical pipeline from scratch.
If you only want to explore the optimal boards, the repository already contains the finalized Pareto-optimal data (docs/data/pareto_front.json). You can run the interactive web app locally without installing complex dependencies or running the heavy backend solver.
- Clone the repository:
git clone https://github.com/rolim520/Nine-Tiles-Panic-Solver.git
cd Nine-Tiles-Panic-Solver/docs- Start a local HTTP server:
python -m http.server 8000- Open your browser and navigate to
http://localhost:8000.
If you want to verify the methodology, re-generate the billions of solutions, and run the DuckDB analytical pipeline, follow these steps:
- Clone the repository and install the backend requirements:
git clone https://github.com/rolim520/Nine-Tiles-Panic-Solver.git
cd Nine-Tiles-Panic-Solver
pip install -r requirements.txt- Run the CSP generation engine to find all valid boards:
python main.pyWarning: Depending on your CPU, this process can take from 12 to 24+ hours to complete and will generate a ~4.27 GB Parquet file inside the
generated_solutions/directory.
- Execute the analytical pipeline sequentially to process the generated dataset:
python scripts/analytics/01_percentiles.py
python scripts/analytics/02_pareto.py- (Optional) Run the Monte Carlo simulation to evaluate scalarization strategies:
python scripts/analytics/03_montecarlo.pyNine-Tiles-Panic-Solver/
├── docs/ # Static web interface files (HTML, JS, CSS, Brython)
├── game/ # JSON definitions for game tiles, topologies, and cards
├── images/ # Visual assets and generated PDF/PNG plots
├── results/ # Aggregated analytical outputs (Gantt, Monte Carlo results)
├── scripts/
│ ├── analytics/ # DuckDB data pipelines (Percentiles, Pareto, Monte Carlo)
│ ├── plots/ # Matplotlib plotting scripts for thesis figures
│ └── misc/ # Minor verification scripts
├── main.py # CSP generation engine entry point
├── solver.py # Backtracking algorithm implementation
├── analysis.py # Board statistics and graph analysis functions
└── constants.py # Game topology and graph node mappings
Special thanks to Jean-Claude Pellin, Jens Merkl, and Oink Games for designing and publishing Nine Tiles Panic. This project is purely an academic tribute to their excellent and challenging game design. All intellectual property regarding the game belongs to its respective creators and publishers.
