NIMJA (Nim Game AI) is a sophisticated implementation of the classic Nim game featuring intelligent AI opponents and a comprehensive strategy helper. The game implements multiple AI algorithms including Nim-sum calculations, minimax with alpha-beta pruning, and advanced endgame strategies.
- Player vs Player (PvP): Two human players take turns
- Player vs AI (PvAI): Challenge the AI opponent
- Normal Nim: The player who takes the last object WINS
- Misรจre Nim: The player who takes the last object LOSES
- Nim-sum Calculation: Uses XOR operations based on Sprague-Grundy theorem
- Minimax with Alpha-Beta Pruning: Optimal move search with pruning optimization
- Position Evaluation: Strategic scoring of game states
- Pattern Matching: Recognition of winning/forcing positions
- Endgame Strategy: Specialized tactics for Misรจre Nim endgames
- Real-time Analysis: Get optimal move suggestions
- Move Prediction: See potential move sequences and counter-moves
- Turn Management: Practice with both first and second move orders
- Position Evaluation: Understand the mathematical principles behind optimal play
- Python 3.7 or higher
- Tkinter (usually included with Python installations)
# Check Python version
python --version
# Check if Tkinter is available
python -c "import tkinter; print('Tkinter is available')"
- Download or clone the repository
- Navigate to the project directory
- Run the game directly
git clone https://github.com/nylla8444/NIMJA.git
cd "NIMJA"
python main.py
- Choose between Player vs Player or Player vs AI
- Access the Strategy Helper for analysis and practice
- Normal Nim: Last player to move wins
- Misรจre Nim: Last player to move loses
- Game Board: Visual representation using 'O' characters
- Input Fields:
- Row (1-4): Select which row to take from
- Amount: Number of objects to remove
- Submit Move: Apply your move to the game
- Game Mode Toggle: Switch between Normal and Misรจre Nim
- Move Order: Practice playing first or second
- Position Analysis: Get real-time optimal move suggestions
- Move Predictions: See AI's top moves and counter-strategies
- Turn Management: Input moves and see analysis
NIMJA/
โโโ main.py # Entry point - initializes the game
โโโ nim_game.py # Main game logic and GUI management
โโโ nim_ai.py # AI algorithms and strategy implementation
โโโ nim_helper.py # Strategy helper window and analysis tools
โโโ .gitignore # Git ignore patterns
โโโ README.md # This documentation file
main.py
โโโ nim_game.py
โโโ nim_ai.py
โโโ nim_helper.py
โโโ nim_ai.py
The AI uses the mathematical concept of Nim-sum (XOR of all pile sizes):
- Winning Position: Nim-sum โ 0 (can force opponent into losing position)
- Losing Position: Nim-sum = 0 (opponent has winning strategy)
The AI switches strategy based on game state:
- Early/Mid Game: Play like Normal Nim
- Endgame: Special rules when few objects remain
- With all single piles: Leave odd number for opponent
- With one pile > 1: Reduce to create favorable single pile count
- Nim-sum Calculation: O(n) where n = number of rows
- Move Generation: O(nรm) where m = max objects in a row
- Position Evaluation: O(1) constant time
- Pattern Matching: O(n log n) due to sorting
- Game starts with 4 rows containing 1, 3, 5, and 7 objects respectively
- Players alternate turns
- On each turn, remove any number of objects from a single row
- The goal depends on the game variant:
- Normal: Take the last object to win
- Misรจre: Force your opponent to take the last object
- Try to leave positions where Nim-sum = 0
- Calculate: Row1 XOR Row2 XOR Row3 XOR Row4
- If result โ 0, you can win with optimal play
- Play like Normal Nim until endgame
- In endgame, count piles with >1 object
- Leave odd number of single-object piles for opponent
# On Ubuntu/Debian
sudo apt-get install python3-tk
# On CentOS/RHEL
sudo yum install tkinter
# or
sudo dnf install python3-tkinter
# On macOS with Homebrew
brew install python-tk
- Ensure you're running Python 3.7+
- Check if display is properly configured (for SSH/remote connections)
- Try running:
python -m tkinter
to test Tkinter installation
- The AI calculations are optimized and should run smoothly
- If experiencing lag, ensure Python is updated to the latest version
- โ Windows 10/11
- โ macOS 10.14+
- โ Linux (Ubuntu 18.04+, Debian 10+, CentOS 7+)
This project demonstrates several computer science concepts:
- Game Trees: Minimax algorithm implementation
- Optimization: Alpha-beta pruning for efficient search
- Bitwise Operations: XOR for Nim-sum calculations
- Pattern Recognition: Strategic position matching
- Combinatorial Game Theory: Nim-sum and winning/losing positions
- Sprague-Grundy Theorem: Foundation of optimal Nim play
- Game State Evaluation: Heuristic functions for position assessment
- Modular Design: Separation of concerns across multiple files
- Object-Oriented Programming: Classes for game components
- GUI Development: Tkinter interface design
- Error Handling: Input validation and exception management
This is an educational project. If you'd like to extend it:
- Difficulty Levels: Adjustable AI strength
- Game Variants: Different starting configurations
- Statistics Tracking: Win/loss records
- Network Play: Online multiplayer functionality
- Sound Effects: Audio feedback for moves
- Animation: Smooth visual transitions
- Follow PEP 8 Python style guidelines
- Add docstrings for new functions
- Include algorithm complexity analysis
- Maintain modular structure
This project is created for educational purposes. Feel free to use, modify, and distribute for learning and non-commercial purposes.
- Charles Bouton: Original mathematical analysis of Nim (1901)
- Sprague-Grundy Theorem: Foundation for combinatorial game theory
- Nim Game Theory: Mathematical optimization in game strategy
Project: NIMJA
Purpose: Educational demonstration of AI algorithms in game theory
Language: Python 3.7+
GUI Framework: Tkinter
Enjoy playing and learning with NIMJA! ๐ฎ๐ง