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 flipped
  • You can only place a piece if it "captures" at least 1 other piece
  • The 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 that direction immediately. If it finds a piece that matches your color, then the pieces in between can be captured and it adds these "Terminal" pieces to an array and moves onto the next step. The program then passes the starting X/Y and the terminal X/Ys to another function that loops through the terminal pieces and works backwards to the starting piece, flipping all the pieces as it passes over them. This happens instantaneously to the player, but the two steps look like this:

This all looks very simple, but like anything in programming, it posed it's share of challenges. The code that checks the 8 different directions is all the same, with the only difference being adding and subtracting 1 from the X/Y value. This means I can either copy and paste the same code 8 times and just change the values, or I can devise a system that will switch the direction automatically. The former is Easier at first, but if I had to change something, I would have to change it 8 times, so instead I went with the latter.

It works by looping 8 times, each time selecting the next option from a list. Each of these options sets 2 variables: uD (for up/down) and lR (for left/right) to either -1, 0, or 1 depending on the direction. EX: Up would be uD = 1, lR = 0, and Diagonal down-right would be uD = -1, lR = 1. I have 8 of these choices, one for each of the 8 possible directions, and it simply gets added to the X/Y of the cell that is currently being checked.

Writing this as 2 functions also posed some challenges. It meant that I needed to pass all the needed information out of the function in an array, store it in some kind of intermediate variable, and then pass it back to the "flipPieces()" function to finally flip the pieces. I could have done this in 1 function with a lot less code, however it needs to be 2 separate functions due to the fact that when the AI is implemented, it will need to check if a space is possible as well as the number of pieces flipped by each space, without flipping any over. This way it can loop through the possible spaces and only after choosing the best one, start to flip pieces over.

Once that part was done, I implementing a few small features such as turn switching and all of a sudden the game fell into place and actually WORKED! The game currently won't ever end, even if there are no possible spaces left because there still isn't any code that checks ALL possible spaces, but that will be implemented with the AI. Right now 2 players can still play the game and just count the pieces at the end

Last thing to do is to program the AI. I have never programmed an AI before, so this should be interesting...


Popular posts from this blog

I need to switch to Linux... BAD (rant)

Live Streaming is... weird

Accountability(); Pt. 2 From failure to success