DIY AI Powered Hexapawn in Python

Shaoqing Tan
3 min readOct 15, 2019

The code is adapted from Chapter 4 of Max Pumperla’s Deep Learning and the Game of Go. The book includes a game of Tic, Tac, Toe in Chapter 4’s examples. We are adapting it to Hexapawn with new game rules but we are keeping the software interface the same such that the existing minimax agent can still be used as our opponent.

The rule of Hexapawn can be summarised as follows

  • On a 3x3 board each player starts with 3 pawns on either end.
  • A pawn can be advanced forward if the target position is empty.
  • A pawn can be advanced diagonally if the target position is occupied by an opponent, in which case the opponent pawn is removed from the board.
  • A player wins if a pawn reaches the other end.
  • A player wins if the opponent is stuck with no moves available.
  • A player wins if the opponent is out of pawns. (this is a subset of the previous condition)

With proper moves, the second player to move always wins. Furthermore, since the search space of this game is extremely small, we expect the minimax powered AI player who plays second to always beat the human player who plays first.

The code for this project can be found on github. The implementation details will not be discussed here, for those interested, please refer to pull request #1.

To run the game, you can use the Jupyter Notebook version found here, or start by downloading the github repo (extract if necessary) and open a terminal.

1. Change directory into the `code` sub-directory

$ cd projects/deep_learning_and_the_game_of_go/code

2. Install package dependencies

$ python3 -m pip install .

3. Run the game

$ python3 play_hxp.py

4. Type in a number followed by enter to select your move

===================
C1 C2 C3
R1 x1 | x2 | x3
R2 | |
R3 o1 | o2 | o3
Human player's turn
===================
0 Move x1 to Point(row=2, col=1)
1 Move x2 to Point(row=2, col=2)
2 Move x3 to Point(row=2, col=3)
-- 0

5. Observation

Notice that the AI player is smart enough to beat you every time! An example transcript is as follows.

===================
C1 C2 C3
R1 x1 | x2 | x3
R2 | |
R3 o1 | o2 | o3
Human player's turn
===================
0 Move x1 to Point(row=2, col=1)
1 Move x2 to Point(row=2, col=2)
2 Move x3 to Point(row=2, col=3)
-- 0
===================
C1 C2 C3
R1 | x2 | x3
R2 x1 | |
R3 o1 | o2 | o3
AI player's turn
===================
===================
C1 C2 C3
R1 | x2 | x3
R2 o2 | |
R3 o1 | | o3
Human player's turn
===================
0 Move x2 to Point(row=2, col=1)
1 Move x2 to Point(row=2, col=2)
2 Move x3 to Point(row=2, col=3)
-- 0
===================
C1 C2 C3
R1 | | x3
R2 x2 | |
R3 o1 | | o3
AI player's turn
===================
C1 C2 C3
R1 | | x3
R2 x2 | | o3
R3 o1 | |
Winner: Player.o

Thank you for staying with me! If you enjoyed the game, please consider reading the source code to understand what it takes to produce the software interface and the minimax algorithm. I also encourage you to make the board larger to see if the AI player can keep up with the increased search space.

Notable files include the following.

--

--