Thumbnail created by vectorsmarket15 - Flaticon
Link to Repository
Project Overview
As a child, I really enjoyed playing Pentago. With the 6x6 board that came with the game, I liked to come up with new games to play with my friends. One such game I coined Hexago, a simple chess-inspired game where both players start with 3 rows of pawns and aim to be the first to get a pawn to the opponent’s last row.
In the early stages of learning Python, I decided to give building this game a shot. Given that it was a novel game, I wanted to build an AI that I could play against, and perhaps use it to find new strategies of play. In the end, I managed to build the game, but struggled to come up with a meaningful CPU algorithm to play against. In this project, I wanted to revisit and rewrite my noob code, while adding a Monte Carlo Tree Search (MCTS) algorithm AI adapted from ai-boson. The MCTS explanation by John Levine was a great help in building the AI.
How it works
I built the script to run within the terminal/IDE, with the board changing perspective whenever it was either white (represented by “O”) or black’s (represented by “X”) turn to move. Users move by inputting an algebraic notation, where the notation format follows chess exactly. The game offers both multiplayer and cpu, which can be controlled as a terminal parameter or within main.py. I also exposed certain parameters of the MCTS algorithm within main.py for users to adjust the exploitation-exploration ratio of the AI or to visualise the AI’s score for each move.
An example of the first two turns in a versus game:
=== white's turn ===
a b c d e f
6 X X X X X X
5 X X X X X X
4 X X X X X X
3 O O O O O O
2 O O O O O O
1 O O O O O O
Available moves: ['axb4', 'bxa4', 'bxc4', 'cxb4', 'cxd4', 'dxc4', 'dxe4', 'exd4', 'exf4', 'fxe4']
Choose a move: axb4
=== black's turn ===
f e d c b a
1 O O O O O O
2 O O O O O O
3 O O O O O .
4 X X X X O X
5 X X X X X X
6 X X X X X X
Available moves: ['axb4', 'cxb4', 'a3', 'axb3', 'cxb3', 'cxd3', 'dxc3', 'dxe3', 'exd3', 'exf3', 'fxe3']
Choose a move:
And an example of white winning, where white wins with a pawn in a6:
=== white's turn ===
a b c d e f
6 X X X X X X
5 X O . X X X
4 . X X . . X
3 . . O O X X
2 O O O O O O
1 O O O O O O
Available moves: ['bxa6#', 'bxc6#', 'cxb4', 'd4', 'dxc4', 'a3', 'b3', 'dxe3', 'exf3', 'fxe3']
Choose a move: bxa6#
f e d c b a
1 O O O O O O
2 O O O O O O
3 X X O O . .
4 X . . X X .
5 X X X . . X
6 X X X X X O
White wins!
What I Learnt
Successfully implementing the AI felt like a huge milestone in my coding journey; I could finally see major improvements in my ability to read, understand and adapt code, and could write some semblance of a neat class: no more haphazard mess of functions like back in the day. Further, as one of my first production level terminal scripts, I was also able to sharpen my error handling and terminal paramater managements skills.
Future Improvements
In the actual Pentago board game, the board is actually constructed out of 4 revolving 3x3 boards, where turning the board was part of each move. This would be something interesting to implement in future iterations of my game!