# 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.

![](YYYdZZZ/yafour/yafour.png)

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](https://connect4.gamesolver.org)
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](bitboard).

My version of the game is available in the [downloads](downloads)
section. It runs in Linux, Windows and macOS.

## Technicalities

### 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](/contact).

### Anti aliasing
The graphics look somewhat primitive. This is caused by the fact that
at this moment (Feb 2022) 'tkinter' does not know about anti-aliasing.

### Turtles
It appears that turtles (the dishes in yafour) are moving slower after
some use. I have no idea what causes this. Suggestions are [welcome](/contact).

Since I simplified the code to erase a text, the performance degradation
is not happening anymore. Problem solved but why?

Answer: I printed the same texts over and over again, without clearing.
I suppose, this is repeated in the mainloop, thus deteriorating the performance.
Now, before printing, the text turtle is cleared.

## 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.

* [four-in-a-row](https://wiki.gnome.org/Apps/Four-in-a-row) Plays a decent game,
  is available in debian, and possibly other distros as well. Written in 
  [vala](https://en.wikipedia.org/wiki/Vala_(programming_language))

* [web version from coolmathgames](https://www.coolmathgames.com/0-four-in-a-row) No match
   for yafour.

* [web version from fourinarow.org](https://fourinarow.org/) Plays a decent game.

* [web version from puzzlemadness](https://puzzlemadness.co.uk/4inarow) No match for yafour.

* [web version from papergames](https://papergames.io/en/connect4) In higher levels it plays
  very decently. You have to use level=med or level=high in yafour to consistently beat it.

* [web version, you cannot win!](https://connect4.gamesolver.org/) 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. However, maybe some master in JavaScript
coded the thing in JavaScript.

* [web version from mathisfun](https://www.mathsisfun.com/games/connect4.html) Seems to play
   decent, but loses from yafour.

It should be noted, that running the algorithms in JavaScript results in a poor performance
compared with Fortran, so raw speed wins here from good code. On the other hand, my implementation
in Fortran uses a very simple representation of the status-quo, which results in an inefficient
evaluation function.
