An intelligent Rush Hour puzzle game implementation featuring AI search algorithms and an intuitive graphical interface built with Pygame. This project demonstrates various artificial intelligence search strategies to solve the classic sliding puzzle game where the goal is to move a red car (marked as 'X') out of a traffic jam.
- ๐ฏ Overview
- โจ Features
- ๐๏ธ Project Structure
- ๐ง Installation
- ๐ Usage
- ๐ง Algorithms
- ๐บ๏ธ Map Format
- โ๏ธ Configuration
- ๐ฎ Game Controls
- ๐ Performance Metrics
- ๐ค Contributing
- ๐ License
- ๐จโ๐ป Author
The Rush Hour Puzzle Solver is an educational AI project that implements and compares different search algorithms to solve Rush Hour puzzles. The game features:
- Interactive GUI: Built with Pygame for smooth user experience
- Multiple AI Algorithms: BFS, DFS, UCS, and A* search implementations
- Performance Analysis: Real-time statistics including execution time, memory usage, and nodes expanded
- Customizable Maps: Support for multiple predefined maps and random generation
- Educational Focus: Perfect for learning AI search algorithms and their performance characteristics
- Intuitive Graphical Interface: Clean, modern UI built with Pygame
- Multiple Puzzle Maps: 10+ predefined maps with varying difficulty levels
- Random Map Generation: Procedurally generated puzzles for endless gameplay
- Interactive Controls: Mouse and keyboard support for manual play
- Visual Solution Playback: Step-by-step animation of AI-generated solutions
- Four Search Algorithms: BFS, DFS, UCS, and A* implementations
- Real-time Performance Metrics:
- Execution time measurement
- Memory usage tracking
- Number of expanded nodes
- Solution path length
- Algorithm Comparison: Side-by-side performance analysis
- Optimized Implementations: Efficient state representation and search strategies
- Scene Management: Home screen, game screen, and help screen
- Dropdown Menus: Easy algorithm and map selection
- Statistics Display: Real-time performance data visualization
- Responsive Design: Adapts to different screen sizes
- Visual Feedback: Clear indication of moves and solutions
AI-Search-Problem/
โโโ ๐ README.md # Project documentation
โโโ ๐ LICENSE # MIT License
โโโ ๐ requirements.txt # Python dependencies
โโโ ๐๏ธ src/ # Source code directory
โ โโโ ๐ main.py # Main application entry point
โ โโโ ๐๏ธ assets/ # Game assets (images, sounds)
โ โ โโโ ๐ผ๏ธ logo.png # Game logo
โ โโโ ๐๏ธ config/ # Configuration files
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ config.json # Game configuration settings
โ โ โโโ ๐ settings.py # Settings loader
โ โโโ ๐๏ธ core/ # Core game logic
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ board.py # Game board implementation
โ โ โโโ ๐ map_loader.py # Map loading utilities
โ โ โโโ ๐ vehicle.py # Vehicle class definition
โ โโโ ๐๏ธ entities/ # UI entities
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ button.py # Button UI component
โ โ โโโ ๐ dropdown.py # Dropdown UI component
โ โโโ ๐๏ธ maps/ # Puzzle map definitions
โ โ โโโ ๐ map1.json # Beginner level map
โ โ โโโ ๐ map2.json # Easy level map
โ โ โโโ ๐ map3.json # Medium level map
โ โ โโโ ๐ map4.json # Hard level map
โ โ โโโ ๐ map5.json # Expert level map
โ โ โโโ ๐ ... # Additional maps (6-10)
โ โโโ ๐๏ธ rushhour/ # Game state management
โ โ โโโ ๐ state.py # Game state representation
โ โโโ ๐๏ธ scenes/ # Game scenes
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ home.py # Main menu screen
โ โ โโโ ๐ playing.py # Game playing screen
โ โ โโโ ๐ help.py # Help/instructions screen
โ โโโ ๐๏ธ solvers/ # AI search algorithms
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ base_solver.py # Base solver class
โ โ โโโ ๐ bfs_solver.py # Breadth-First Search
โ โ โโโ ๐ dfs_solver.py # Depth-First Search
โ โ โโโ ๐ ucs_solver.py # Uniform Cost Search
โ โ โโโ ๐ astar_solver.py# A* Search with heuristics
โ โโโ ๐๏ธ utils/ # Utility functions
โ โโโ ๐ __init__.py # Package initialization
โ โโโ ๐ helper.py # Helper functions
โโโ ๐๏ธ tests/ # Test files
โโโ ๐ test_map.py # Map loading tests
- Python 3.7 or higher
- pip (Python package installer)
-
Clone the repository
git clone https://github.com/NhanPhamThanh-IT/AI-Search-Problem.git cd AI-Search-Problem -
Create a virtual environment (recommended)
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Verify installation
python src/main.py
- pygame (2.5.2): Game development framework
- numpy: Numerical computations
- Additional packages: See
requirements.txtfor complete list
-
Navigate to the project directory
cd AI-Search-Problem -
Start the game
python src/main.py
-
Game Flow
- Main menu appears with options to play, view help, or quit
- Select "Play" to enter the game screen
- Choose a map from the dropdown menu (1-10 or Random)
- Select an AI algorithm (BFS, DFS, UCS, or A*)
- Click "Solve" to watch the AI solve the puzzle
- View performance statistics in real-time
- Click and drag vehicles to move them
- Only valid moves are allowed (no collisions)
- Try to move the red car (X) to the exit on the right side
- Select your preferred algorithm
- Click "Solve" to start automatic solving
- Watch the solution animation
- Compare algorithm performance metrics
- Strategy: Explores all nodes at current depth before going deeper
- Completeness: Complete (finds solution if one exists)
- Optimality: Optimal for unweighted graphs
- Time Complexity: O(b^d)
- Space Complexity: O(b^d)
- Best for: Finding shortest solution path
- Strategy: Explores as far as possible before backtracking
- Completeness: Complete in finite spaces
- Optimality: Not optimal
- Time Complexity: O(b^m)
- Space Complexity: O(bm)
- Best for: Memory-constrained environments
- Strategy: Expands node with lowest path cost first
- Completeness: Complete
- Optimality: Optimal
- Time Complexity: O(b^(C*/ฮต))
- Space Complexity: O(b^(C*/ฮต))
- Best for: Weighted graphs with varying costs
- Strategy: Uses heuristic function to guide search
- Completeness: Complete
- Optimality: Optimal with admissible heuristic
- Time Complexity: O(b^d)
- Space Complexity: O(b^d)
- Best for: Optimal solutions with good heuristics
- Heuristic: Manhattan distance to goal position
Maps are defined in JSON format with the following structure:
{
"size": [6, 6], // Board dimensions [rows, cols]
"vehicles": [
// Array of vehicles
{
"name": "X", // Vehicle identifier (X = main car)
"row": 2, // Starting row position
"col": 0, // Starting column position
"length": 2, // Vehicle length
"orientation": "H" // H = Horizontal, V = Vertical
},
{
"name": "A", // Other vehicles
"row": 0,
"col": 3,
"length": 2,
"orientation": "H"
}
// ... more vehicles
]
}- Follow the JSON structure above
- Ensure no vehicle overlaps
- Place the main car (X) that needs to reach the exit
- Save as
mapN.jsonin thesrc/maps/directory
Game settings are stored in src/config/config.json:
{
"TITLE": "Rush Hour Game - Introduction to AI",
"WINDOW_SIZE": [900, 600],
"BG_COLOR": [30, 30, 30],
"SCENES": {
"MENU": "menu",
"PLAY": "play",
"HELP": "help",
"QUIT": "quit"
},
"MAPS": ["1", "2", "3", "4", "5", "Random"],
"ALGORITHMS": ["BFS", "DFS", "UCS", "A*"],
"CELL_SIZE": 60,
"MARGIN": 20,
"FPS": 60,
"TIME_LIMIT": 30
}- Window size: Adjust game window dimensions
- Colors: Modify background and UI colors
- Cell size: Change grid cell dimensions
- FPS: Set frame rate for smooth animation
- Time limit: Set maximum solving time
- Mouse: Click buttons and dropdown menus
- ESC: Return to main menu or quit
- Mouse:
- Click and drag vehicles to move
- Click dropdowns to select maps/algorithms
- Click "Solve" button to start AI solving
- Keyboard:
- ESC: Return to main menu
- R: Reset current puzzle
- Space: Pause/resume solution animation
- Mouse: Click "Back" to return to menu
- ESC: Return to main menu
The game displays real-time performance statistics:
- Execution Time: Total time for algorithm to find solution
- Solution Length: Number of moves in optimal path
- Search Efficiency: Nodes expanded vs. total possible states
- Memory Usage: Peak memory consumption during search
- Nodes Expanded: Total number of states explored
- Nodes in Memory: Maximum nodes stored simultaneously
- Side-by-side algorithm comparison
- Historical performance tracking
- Export results to CSV (planned feature)
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- New Algorithms: Implement additional search strategies
- UI Improvements: Enhance graphics and user experience
- Performance Optimization: Improve algorithm efficiency
- New Features: Add game modes, statistics, or tools
- Documentation: Improve docs and add tutorials
- Testing: Add unit tests and integration tests
- Follow PEP 8 style guidelines
- Add docstrings to functions and classes
- Include type hints where appropriate
- Write clear, descriptive commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2025 Nhan Pham Thanh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files...
Nhan Pham Thanh
- GitHub: @NhanPhamThanh-IT
- Project: AI-Search-Problem
If you find this project helpful, please consider:
- โญ Starring the repository
- ๐ด Forking for your own experiments
- ๐ Reporting issues or suggesting improvements
- ๐ข Sharing with others interested in AI and game development
This project is perfect for:
- Computer Science Students: Learning AI search algorithms
- Educators: Teaching search strategies and game theory
- Researchers: Benchmarking new algorithms
- Developers: Understanding Pygame and Python game development
Made with โค๏ธ for the AI and game development community