Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need instructions for Reversi game

Tags:

python

reversi

I am trying to write Reversi game in Python. Can anyone give me some basic ideas and strategy which are simple, good and easy to use?

I would appreciate for any help because I've gone to a little far but is stucked between codes and it became more complex too. I think I overdid in some part that should be fairly simple. So....


2 Answers

Reversi is an elegantly simple game. I'm going to use a psuedo C#/Java langauge to explain some concepts, but you can transpose them to Python.

To break it down into its most simple compnents, you have two basic things:

A 2 dimensional array that represents the game board:

gameBoard[10,10]

And some form of enumaration that stores the state of each tile in the gameboard:

enum tile
{
    none,
    white,
    black
}

To render the board, you loop through the gameBoard array, increasing by an offset of the piece size:

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        // The Piece to draw would be at gameBoard[i,j];
        // Pixel locations are calculated by multiplying the array location by an offset.
        DrawPiece(gameBoard[i,j],i * Width of Tile, j * width of tile);
    }
}

Likewise, resolving a mouse click back to a location in the array would be similar, use the mouse location and offset to calculate the actual tile you are on.

Each time a tile is placed, you scan the entire array, and apply a simple rules based engine on what the new colors should be. (This is the real challenge, and I'll leave it up to you.)

The AI can take advantage of doing this array scan with hypothetical moves, have it scan 10 or so possible moves, then choose the one that produces the best outcome. Try to not make it to smart, as its easy to make an unbeatable AI when you let it play out the entire game in its head.

When there are no longer any free locations in the array, You end the game.

like image 99
FlySwat Avatar answered Dec 20 '25 07:12

FlySwat


The wikipedia page has all of the rules and some decent strategy advice for reversi/othello. Basically, you need some sort of data structure to represent board state, that is to say the position of all the pieces on the board at any point in the game. As suggested by others, a 2d array is probably a decent choice, but it doesn't really matter so long as it is a representation that makes sense to you. Some of the hard stuff is figuring out which spaces are valid moves and then which pieces to flip over but, again, the wikipedia page has all of the details so it shouldn't be too hard to implement.

If you want to create an AI for your game, then I would suggest look at some sort of minimax type algorithm with Alpha-Beta pruning. There are a ton of resources on the web for these and an ai that uses minimax with a decent evaluation function will be able to beat most human players pretty easily, as it can look at least 8 or 9 moves ahead in very little time. There are some other fancier variants on minimax, like negamax or negascout that can do even better than basic minimax, but I'd start with the simpler ones. Wikipedia has pages on all of these algorithms and there is a ton of information on all of them as many AI courses use them for Othello or something similar. One page that is particularly useful is this Java Applet. It allows you to step through the steps of minimax and negamax on a sample state tree with and without alpha-beta pruning.

If none of this makes sense, let me know.

like image 21
Paul Wicks Avatar answered Dec 20 '25 07:12

Paul Wicks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!