Game AI .*;
Reversi is also known by its trade name, Othello. It's a fairly simple game that's been around forever, and it's often posed to high school and college students to design a challenging computer opponent. Over the years I've played around with a few ways to implement AI for this game.
In Reversi, game pieces are disk-shaped, with a black side and a white side. A game for two players, each chooses to play as either black or white. The game board is a simple 8-by-8 grid. Each player takes turns placing a piece, with their color facing up, and flipping some of the opponent's pieces to their own color. The game ends when the board is entirely filled with pieces, or neither player can make a move. Winning the game means having the most pieces with your color face-up at the end.
For a player to make a move, they need to place a piece in such a way as to catch one or more of the opponent's pieces between their own. For example, if there's a white piece and a black piece side-by-side on the board, and you're playing white, you'd place your piece on the opposite side of the black piece from your own, so the black piece is caught between the two. You complete your turn by flipping that black piece over to its white side, increasing the number of your pieces on the board.
If multiple opponent pieces are lined up in between during your move, you get to flip all those pieces to your color. Moves can be made horizontally, vertically or diagonally. If your move catches opponent pieces lined up between multiple pieces of your own (i.e.; vertically and diagonally, etc.), you get to flip all those pieces to your color.
The examples posted here include a playable implementation in Java.
The Examples Are Contained In Jar Files
Each of the examples contain a lot of GUI code and other Java classes which don't change between examples, so I think it's easier to put them in java .jar files for download.
- If you're familiar with coding in java, chances are you're also familiar with .jar files. When you click on the link to each jar file, hopefully your browser will give you the option of either opening the file with your java runtime, or saving the file to disk.
- If you're not familiar with .jar files, they're basically .zip archives you can run at the command line with something like: "java -jar filename.jar". To see the source code files, you can easily open a jar archive with any utility that opens .zip files. I've included both the .class files and the .java source code files.
I'll still show the important stuff: the AI.