
Writing a Reversi game in Python + PyQt4
They asked us somehow to write a small project - the game Reversi .
And since I'm learning Python now, I decided to write on it. Together with the PyQt4 graphics library.
Well then, what was the matter? We create SVN and go! (Moved to Github )
Here's a miracle I got:

According to Wikipedia (and I completely agree with her)
Python I know at a basic level. I wrote something under the Google App Engine (for example, a crutch for myself to get a convenient RSS feed from a YouTube user, but that's not the point). Something just like that. Something for the Euler project (a great site I must say). In general, I played :)
With Qt, I had no business with this. But to figure out, as it turned out, is not at all difficult.
The basis of the basics , in Russian. And also the documentation here and here . Together with the PyQT4 distribution there is a good base of examples, which also helped me figure it out.
It makes no sense to dwell on installing and configuring the interpreter and libraries, it’s been chewed a lot of times and I haven’t met any pitfalls.
As I already wrote, I have not worked with Qt before. And the first difficulty was to deal with the widget system, how the interaction with the user is generally built. But in principle, it’s not difficult to understand, referring to examples and documentation.
The next difficulty was to write a simple AI. The idea was spied somewhere in the wilds of the Internet and adapted for reverse.
The source, if interested, you can touch yourself. Who knows, there will be no problems with starting (you need the pyqt4 library).
http://reversi-free0u.googlecode.com/svn/trunk/main.py
UPDATE: Now the code is available on Github . I can not vouch for performance.
Especially for windows. Exe'shnik.
http://reversi-free0u.googlecode.com/files/Reversi.7z
Packed using py2exe. I ran into two problems.
First:
First, the images of the cell and the chips in the game were pictures that were inserted into the game. And the packaged application did not see them successfully.
The problem was solved simply - by rendering images using Qt tools (I was just too lazy to do this initially).
Second:
A successfully packaged application ran for me, but refused to work on another computer. After a bit of searching, it turned out that this problem is also easily solved :)
It turned out that the visual studio libraries are not enough.
The main disadvantage is the large size of the resulting application. But there is nothing to be done. All the same, python is an interpreted language.
What did this weekend spent on the game give me?
Moved to a collective blog. In this life, you need to try everything 8)
upd: slightly changed the names of variables in the
upd2 source code: for the afflicted, version of python 2.6.4
And since I'm learning Python now, I decided to write on it. Together with the PyQt4 graphics library.
Well then, what was the matter? We create SVN and go! (Moved to Github )
Here's a miracle I got:

According to Wikipedia (and I completely agree with her)
The game uses a square board with a size of 8 × 8 cells (all cells can be the same color) and 64 special chips, painted on different sides in contrasting colors, for example, white and black. The cells of the board are numbered from the upper left corner: vertical - in Latin letters, horizontal - in numbers. One player plays white, the other black. Making a move, the player places the chip on the board cell with “his” color up.
At the beginning of the game, 4 chips are placed in the center of the board: black on d5 and e4, white on d4 and e5.
- Black makes the first move. Next, the players take turns.
- When making a move, the player must place his chip on one of the cells of the board so that between this put chip and one of the chips of his color already on the board there is a continuous row of opponent's chips, horizontal, vertical or diagonal (in other words, so that a continuous row of chips the opponent was “closed” by the player’s chips on both sides). All the opponent’s chips entering the “closed” row this turn are flipped to the other side (change color) and go to the player who walked.
- If as a result of one move more than one row of enemy chips is “closed” at the same time, then all the chips that are on all “closed” rows are flipped.
- The player has the right to choose any of the moves possible for him. If a player has possible moves, he cannot refuse a move. If the player does not have valid moves, then the move is passed to the opponent.
- The game ends when all the chips are placed on the board or when none of the players can make a move. At the end of the game, counting the chips of each color is carried out, and the player whose chips are more on the board is declared the winner. In case of equal number of chips, a tie is counted.
Python I know at a basic level. I wrote something under the Google App Engine (for example, a crutch for myself to get a convenient RSS feed from a YouTube user, but that's not the point). Something just like that. Something for the Euler project (a great site I must say). In general, I played :)
With Qt, I had no business with this. But to figure out, as it turned out, is not at all difficult.
The basis of the basics , in Russian. And also the documentation here and here . Together with the PyQT4 distribution there is a good base of examples, which also helped me figure it out.
It makes no sense to dwell on installing and configuring the interpreter and libraries, it’s been chewed a lot of times and I haven’t met any pitfalls.
As I already wrote, I have not worked with Qt before. And the first difficulty was to deal with the widget system, how the interaction with the user is generally built. But in principle, it’s not difficult to understand, referring to examples and documentation.
The next difficulty was to write a simple AI. The idea was spied somewhere in the wilds of the Internet and adapted for reverse.
Copy Source | Copy HTML- def compStep(player):
- table = [
- [1, 8, 2, 2, 2, 2, 8, 1],
- [8, 8, 6, 5, 5, 6, 8, 8],
- [2, 6, 4, 3, 3, 4, 6, 2],
- [2, 5, 3, 1, 1, 3, 5, 2],
- [2, 5, 3, 1, 1, 3, 5, 2],
- [2, 6, 4, 3, 3, 4, 6, 2],
- [8, 8, 6, 5, 5, 6, 8, 8],
- [1, 8, 2, 2, 2, 2, 8, 1]
- ]
- pX = [ 0] * 61
- pY = [ 0] * 61
- minE = 9
- maxE = 0
- NP = 0
- for row in range(8):
- for col in range(8):
- E = eated(row, col, player)
- if (minE > table[row][col]) & (E < 255) & (E > 0):
- minE = table[row][col]
- NP = 0
- maxE = 0
- if (minE == table[row][col]) & (E < 255):
- if E > maxE:
- maxE = E
- NP = 1
- pX[NP] = row
- pY[NP] = col
- elif E == maxE:
- NP = NP + 1
- pX[NP] = row
- pY[NP] = col
- E = 1
- makeStep(player, pX[E], pY[E])
- area[pX[E]][pY[E]] = player
The source, if interested, you can touch yourself. Who knows, there will be no problems with starting (you need the pyqt4 library).
http://reversi-free0u.googlecode.com/svn/trunk/main.py
UPDATE: Now the code is available on Github . I can not vouch for performance.
Especially for windows. Exe'shnik.
http://reversi-free0u.googlecode.com/files/Reversi.7z
Packed using py2exe. I ran into two problems.
First:
First, the images of the cell and the chips in the game were pictures that were inserted into the game. And the packaged application did not see them successfully.
The problem was solved simply - by rendering images using Qt tools (I was just too lazy to do this initially).
Second:
A successfully packaged application ran for me, but refused to work on another computer. After a bit of searching, it turned out that this problem is also easily solved :)
It turned out that the visual studio libraries are not enough.
I have not seriously used Python 2.6 with py2exe, also I have no experienceThat is, the most difficult thing is to put a library and a manifest file in the program.
with this new manifest stuff, but a little experiment showed that this approach
seems to work for simple cases (I tested only on XP machines, not Vista!):
I deinstalled python 2.6 (since I had installed it 'for all users') and
installed it again 'for me only'. This installation copied the msvcr90.dll
and Microsoft.VC90.CRT.manifest files into the c: \ python26 folder.
Then I ran py2exe over a very simple script ('print "Hi"') which created
an executable. This executable worked fine on a machine where msvcr90.dll
was installed in Windows \ SxS (or how it's called), but did NOT run on another
machine where msvcr90.dll is not installed in Windows \ SxS.
Then I copied the msvcr90.dll and Microsoft.VC90.CRT.manifest files into the dist
folder where py2exe had created my executable. Now the exe works on both machines.
When I tried to do the same for a simple wxPython script py2exe crashed because
it tried to load msvcp90.dll (IIRC), but didn't find it (it seems only to be installed
in the Windows \ SxS folder). This may be a bug in py2exe.
The main disadvantage is the large size of the resulting application. But there is nothing to be done. All the same, python is an interpreted language.
What did this weekend spent on the game give me?
- The pleasure of learning and writing a gui application in python
- The desire to share this with you)
For this topic I received an invite
Moved to a collective blog. In this life, you need to try everything 8)
upd: slightly changed the names of variables in the
upd2 source code: for the afflicted, version of python 2.6.4