I'm currently trying to create the game of othello (aka, reversi) in python 3.
I have huge problems with the section of the program where it shall evaluate if a move is valid or not.
What I want to create:
- Check if a position on the board is empty or not
- Check if there are any neighbours of the opposite color
- If there are such neighbours, continue in that direction and see if we can reach one of our own pieces without crossing an empty position.
I have tried many different functions but i cant get it right...
In the link down below is my latest attempt,
The first thing that comes to mind is to represent the board as a string, where every character in the string represents the state of that square, starting from 0 (top left) to 63 (bottom right).
For example, say you represented a square being occupied by a white by "o", for black, "x", and an empty square would be ".", for the starting state, it would be:
board = "...........................ox......xo..........................."Then if you wanted to check whether a square is empty or not, you would check whether that character is empty:
For this, you could pre-store tuples of directions that correspond to all of a square's neighbours (this will also be helpful for calculating possible moves):
Where the first item of the tuple is the amount to move horizontally, and the second item being the amount to move vertically. So if you wanted to find neighbors of the opposite color, you would get the index of the current square:
Then loop through the directions, and check what color the neighbor is using the logic from above.
Now since that is pretty basic code, it doesn't cover possibilities where the current tile is on an edge on the corner, where the index would go to an incorrect position, looping around. One possibility is to just hardcode those cases in, and ignore those directions, however another (possibly more elegant way) can be used as well. Instead of representing the board as a 64-character string, you can represent it as a 100-character string to include a border like so:
Then you can simply check if the "neighbor" is part of the border (in this case represented by a question mark, but can be anything else besides the ones you use to represent empty spaces and discs).
However, you will need to account for the fact that you have to convert indices from a 8x8 board to 10x10 board (and convert back) appropriately.
This is pretty simple once you implement the above logic, you will just need a while loop to check tiles in a certain direction, and whether they can be taken if we reach the original's color.