Othello(); Pt. 3 The AI
This is it. The moment I have been waiting for! With the actual game part complete, all that is left is to program the AI (the reason I started this whole project).
My theory on how the AI will work is pretty simple:
1) Check all valid positions
2) Pick the position with the highest outcome of flipped pieces.
The first step is to eliminate all the spaces which are not possible. We know that a space is only valid if it can flip at least one piece, so that means that one of the bordering spaces of an empty piece has to contain at least one piece of the opposite color.
In order to check for this, I run a 3x3 grid over every space, and if the center is empty, then it checks the surrounding spaces for one of the opposite color. If there is one of the opposite color, then it marks that space as "possible." Even though this will still mark spaces that are not technically possible, it narrows it down enough to be useful. The result is this:
From here, I can the "tryFlip()" function on each of those red spaced, and see how many pieces each one will return. All the spaces that flip pieces are placed in an ordered list from least amount of pieces flipped, to most amount of pieces flipped, allowing me to select the lowest for an easy difficulty, or the highest for a harder difficulty. If there are no more possible spaces, then the game ends.
All in all the AI part was really simple, and kind of a letdown considering how much work had to go into the game before I could start working on it, but I still learned a lot.
The finished game can be downloaded and played here: