A command-line Chinese Chess / Xiangqi endgame AI project built in Python. This project implements core Xiangqi rules, legal move generation, game-state evaluation, and an AI opponent using Minimax search with Alpha-Beta pruning.
This project focuses on a simplified Xiangqi endgame environment where players can test different strategies against an AI-controlled opponent. The goal of the project is to explore how classical AI search algorithms can be used to make decisions in a turn-based board game.
The AI evaluates possible future board states and selects the move that leads to the strongest position. The project includes rule-based move validation, check/checkmate detection, random and greedy baseline players, and automated evaluation experiments.
The AI uses a Minimax-based search strategy. Since Xiangqi is a two-player competitive game, the AI assumes that both sides will try to make the best possible move.
At each turn, the AI:
The evaluation function scores a board state based on several factors:
In the current version, the evaluation score is calculated from Red’s perspective. When the AI controls Black, it chooses moves that minimize Red’s advantage.
The AI assigns different values to different pieces based on their importance in the endgame.
| Piece | Description | Value |
|---|---|---|
| K | General / King | 0 |
| A | Advisor / Guard | 40 |
| P | Pawn | 180 |
| C | Cannon | 300 |
| R | Rook | 500 |
The General is not evaluated with a normal material value because losing the General directly determines the outcome of the game.
.
├── main.py # Main entry point for running the game
├── game_state.py # Board representation, pieces, and game state logic
├── move_generator.py # Legal move generation and rule checking
├── evaluation.py # Board evaluation function
├── minimax.py # Minimax search and Alpha-Beta pruning
├── display.py # Command-line board display
├── random_endgame.py # Random endgame state generation
└── README.md
Run the main program:
python main.py
If your system uses Python 3 explicitly:
python3 main.py
To test the AI, I compared it against random and greedy baseline agents over 50 games.
| Red Player | Black Player | Games | Black Wins | Red Wins | Draws | Average Plies |
|---|---|---|---|---|---|---|
| Random | AI | 50 | 50 | 0 | 0 | 2.32 |
| Random | Random | 50 | 33 | 2 | 15 | 16.02 |
| Greedy | AI | 50 | 50 | 0 | 0 | 4.00 |
| Greedy | Random | 50 | 21 | 6 | 23 | 30.00 |
The AI performed strongly against both random and greedy baseline players. In the tested endgame scenarios, the AI won all games when playing as Black.
Compared with the random baseline, the AI was able to find winning moves much faster and more consistently. The average number of plies was also much lower when the AI played, showing that the search-based strategy was able to identify strong tactical decisions quickly.
These results suggest that even a depth-limited Minimax agent can perform effectively in a simplified Xiangqi endgame when combined with a reasonable evaluation function and legal move generation.
Through this project, I gained hands-on experience with:
Possible future improvements include:
Feiyi Chen
M.S. Computer Science, Northeastern University