Yafour: yet another four-in-a-row or connect-4 game
Yes, another implementation of the well-known four-in-a-row game, a.k.a. connect-4.
Implementation of this game was an exercise in
- turtle graphics in Python
- implementing a finite-state algorithm
- implementation of the alpha-beta algorithm in Fortran
- coupling Python and Fortran
The user interface is not documented at all, simply click around, and everything will become clear. I guess.
Later on, I found excellent webpages about the connect-4 game containing a very efficient implementation in C++, and explanation of the alpha-beta algorithm and the coding of the connect-4 game in 12 parts. My implementation of the game is about at the level of Part 5 of that story, and is in no way comparable in efficiency and beauty with the version of Part 12.
In Part 6 ("Bitboard"), the author tells which bit magic is used, but does not explain clearly how the magic works. Therefore, I made a page which tries to explain this.
My version of the game is available in the downloads section. It runs in Linux, Windows and macOS.
Coupling Python and Fortran
The user interface is written in Python, the code to compute the best move is written in Fortran.
I could not find how to incorporate a Fortran code, using setup.py and 'setuptools', in the same way as C or C++ code. Therefore, the compilation of the Fortran code (ninarow.f90 (in principle ready to take any size of the board and play any length of a winning line)) is done explicitly, yielding a .so or a .dll file (Unix vs. Windows). The program yafour.py then searches for this file using the content of 'sys.path'. The Fortran program is equipped with a C-like interface in the function cninarow(). This function is called in the Python program using CDLL (Unix) or WinDLL (Windows).
Using f2py works more or less, but I read somewhere that the support will be discontinued soon, so I abandoned this path. Some inactivated f2py code is still present in yafour.py.
If somebody know a better, or even 'the proper way' to do this, let me know.
The graphics look somewhat primitive. This is caused by the fact that at this moment (feb 2022) 'tkinter' does not know about anti-aliasing.
It appears that turtles (the dishes in yafour) are moving slower after some use. I have no idea what causes this. Suggestions are welcome.
Since I simplified the code to erase a text, the performance degradation is not happening anymore. Problem solved but why?
Other implementations of four in a row
Here a not comprehensive list of some implementations of four in a row. I compared the strength by playing against yafour, level medium. If there was a choice in strength, I chose the strongest one for yafour's opponent.
web version from coolmathgames No match for yafour.
web version from fourinarow.org Plays a decent game.
web version from puzzlemadness No match for yafour.
web version, you cannot win! Indeed, unbeatable! It seems that the algorithm runs on a server, is probably written in C++ and uses a database for starting positions. I guess.
web version from mathisfun Seems to play decent, but loses from yafour.