Back in 2009, shortly before or after my bachelor thesis, I have programmed a simple but moderately entertaining web game unimaginatively titled "the Magic Square Breeding Game", whose goal is to breed a perfect magic square of the 3x3 size using the methods evolutionary computing.

  • The game is fully playable on my legacy website
  • You can also download the source code under the GNU General Public License (in a bout of radical minimalism, I have managed to pack the entire game into a single PHP file)

For more details on the gameplay, here is an excerpt from the help "page":

Setup and notation

You start the game on a field of 6x4 tiles. Each tile can contain up to one square filled with numbers (see below). As you progress through the game, older squares will automatically move upwards until they disappear off the upper edge ("die of old age"). The bold numbers to the left of the tile rows indicate the generation that the squares in the corresponding row were born into: the higher the generation number, the younger is the square. Only four generations can co-exist at the same time on the field.

At the beginning of the game, you get six 3x3 squares filled with random numbers from 1 to 9. They are imperfect (the chances of you randomly getting a perfect magic square in the first generation are very, very slim) but by applying an breeding strategy, you will eventually produce a perfect magic square out of their "genetic material". To make your task of identifying the squares closest to that goal (the "fittest individuals") easier, each one comes with auxiliary fields on the right and on the bottom that indicate how much the sum of the corresponding row and column deviates from that of a perfect magic square (the sum of each row or column in a 3x3 magic square is always 15). The larger the deviation, the further away the square is from the ideal. The bottom-right field additionally contains the sum of all deviations, thus, providing a single-number representation of the square's "fitness". For very unfit individuals, these numbers will be additionally highlighted in red; the ones that come close to the goal, in yellow; and when you breed a perfect magic square, it will become green.

Basic gameplay

Your primary interface tools are found directly underneath each square:

  • The checkbox preceded by the word "Kill", when checked, makes the game "kill off" the square prematurely (before it dies of old age). This function is necessary because out of up to 12 squares present on the field at any time, only 6 can survive to see the next generation. The killing happens all children you have specified (see below) are born.
  • The dropdown menu preceded by "Chld." indicates how many children this particular individual will produce for the next generation. Up to six children can be born in each new generation. The children are produced by randomly swapping ("mutating") two numbers of the parent's square, therefore fitter parents statistically produce fitter children. A child is never identical to its parent.

After you have selected the individuals to be killed and the ones to parent children, press the "Next generation" button for changes to take effect. The game will then apply all changes and display the updated field. That's pretty much it for the basic rules. The real challenge, however, lies in finding an efficient breeding strategy, as you will soon notice that blindly following the "survival-of-the-fittest" principle not always earns the best results...

Winning and losing

You win the game by producing a magic square with the total fitness equal to 0.

You lose the game if all of your squares either die of old age or are completely wiped out.