This is a Python program for playing Othello (or Reversi) against an AI player.
This project is developed on Ubuntu 16.04 LTS.
-
Download all the files from GitHub.
-
Unzip.
-
Open Terminal,
cd
into this directory and type:python3.6 reversi_gui.py
Please note that a command-line version of the game is also available. To run the command-line version, type:
python3.6 reversi_cmd_line.py
Each reversi piece has a black side and a white side. On your turn, you place one piece on the board with your color facing up. You must place the piece so that an opponent's piece, or a row of opponent's pieces, is flanked by your pieces. All of the opponent's pieces between your pieces are then turned over to become your color.
The object of the game is to own more pieces than your opponent when the game is over. The game is over when neither player has a move. Usually, this means the board is full.
The game is started in the position shown below on a reversi board consisting of 64 squares in an 8x8 grid.
A move consists of placing one piece on an empty square.
You can capture vertical, horizontal, and diagonal rows of pieces. Also, you can capture more than one row at once.
The game ends when:
- One player wins, by making his color dominant on the board.The players agree to finish the game (as a resignation, or a draw).
- One player wins, by making his color dominant on the board.The players agree to finish the game (as a resignation, or a draw).
Minimax procedure with alpha-beta pruning.
The AI employs the alpha-beta pruning algorithm described in Chapter 5 of the book Artificial Intelligence: A Modern Approach. The detailed pseudocode is listed below:
To limit the time the AI spends on searching, a cutoff test is introduced. So when it is appropriate to cut off the search, it'll call the heuristic function to choose a move.
The specific cutoff test for this application is easy: the depth of the search is limited to 5 levels. In other words, the number of look ahead steps is 5 by default. If the depth from the current state to the terminal state is less than 5, then the cutoff test is simply the terminal test.
Note the depth limit can be modified at line 66 in reversi_logic.py
.
Apparently, for a player, the more coins he has (and the fewer coins his opponent has), the better.
But note that the weight assigned to this heuristic should not be heavy, because the difference in coins fluctuates intensely and frequently, due to the nature of the game, and only in the very end does the difference in coins matters.
This heuristic aims to increase one's flexibility in terms of choices and constrain the opponent's choices of valid moves.
From the experience of playing the game, we learned that corners are crucial because once a corner is captured, it cannot be flipped. Furthermore, a player can build up stable coins around the corner. This is how one usually wins. Thus, this heuristic should receive a heavy weighting.
If the game has already ended, then returns +100 if the max player (That's you! Congratulations!🎉) has won, or returns -100 if the AI has won (Aww you lost…😭).
- Add more heuristics (e.g. stability heuristic)
- Prettify GUI
- Use database to store the states of the game at the beginning and the end to speed up AI decision.
Flyordie.com. (n.d.). Reversi Rules. [online] Available at: http://www.flyordie.com/games/help/reversi/en/games_rules_reversi.html [Accessed 3 Dec. 2017].
Kukreja, K. (2013). Heuristic/Evaluation Function for Reversi/Othello. [online] Everything Under The Sun. Available at: https://kartikkukreja.wordpress.com/2013/03/30/heuristic-function-for-reversiothello/ [Accessed 3 Dec. 2017].
Sannidhanam, V., & Annamalai, M. (2015). An Analysis of Heuristics in Othello.
Stuart, R. and Norvig, P. (2010). Artificial Intelligence: A Modern Approach. 3rd ed. Englewood Cliffs: Prentice-Hall, pp.167-174.