Showing posts from May, 2016

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()"…

Othello() Part 2: Simple logic

At this point I have all of the drawing code implemented. The pieces and UI are drawn to the board, as well as being able to place pieces randomly on the board (which you shouldn't be able to do, but oh well)

The rules of Othello are simple:

You can capture your opponent's pieces by trapping them in between two of your own. This can be up, down, left, right, or diagonal. You can capture more than one piece if possible.Only pieces directly affected by the current turn are flippedYou can only place a piece if it "captures" at least 1 other pieceThe game is over when no other moves can be made. In order for any of those rules to be implemented, the first rule has to be somewhat working, so lets start with that.

I came up with a basic algorithm in my head, and it ended up working pretty good. It takes the X/Y of the cell the mouse has clicked in, and checks all 8 directions around it for a piece matching your color. If it encounters an empty space, then it stops checking …