The game of life, ChatGPT version

Organic life is based on chemical reactions. Given a initial state of some compound we can try to guess how it will evolve emulating the interactions of molecules (think for example to proteins unfolding). Tha game of life is a mathematical model that wants to use this principle: an initial state and single rules to make it evolves.

Funny enough, it is Turing Complete, so we can design a game simulating a Turing machine. Show to show you how impressive it is I propose you this video showing one Universal Turing Machine emulated by the game of life.

The game of life

The game want to reproduce cells life:

  • in an environment with too many cells they lack of resources and die;
  • in an environment with too few cells they can not reproduce themself and die;
  • with the right number of individuals the population reproduce and survive.

In the game cells are squares of a grid, each grid can contain a cell (the square is full) or to be empty (the square is empty). The status of the grid in a particular moment is called generation and it determines the next generation thank to these rules:

  • if a square with a cell as more than 3 neighbors with a cell there is overpopulation and the square dies
  • if a square has 2 or 3 living neighbors it survives
  • if it has 0 or 1 neighbors it dies for under-population
  • an empty cell with 3 living neighbors becomes full.

Each generation is slightly different from the previous, and we can study the evolution of a particular state. There are some complex cell organizations that produce spectacular evolutions, like multicellular entities (some are famous, like the glider, the symbol of hacker subculture) in movement in this space, generators of multicellular entities, starships etc.

The fact that there are complexes that generate particles in movements, and complexes that change behavior when hitted by such particles allow us to build logic gates and to create a pattern that behaves like a finite state machines. It is possible to show that this simple model has the same computational power than the universal Turing Machine.

Emulators and implementations

The fact that it is Turing Complete fascinated me and something like 15 years ago I developed a Java program with GUI to play this game. There are very good emulator programs that can run huge initial states for many generation in very few time, like Golly, which has a lot of presets of spaceships, finite state machines and others, moreover it supports several sets of rules, not only the version described in this article.

Why am I talking about this topic? Because I thought that it was a good Easter Egg for the 404 page of the blog, but I would need some hour to develop it in a decent way. In this period I’m experimenting with ChatGPT so I try to get two pigeons with a bean (it’s a literal translation of an Italian idiomatic statement).

I started making a free account at https://chat.openai.com/ and here my experience on it.

Steps

With my great surprise ChatGPT could write a good emulator within minutes, with only small errors. My requests were the following:

  • make the game of life in a html canvas with a grid of line, where i can set the initial status clicking the squares of the grid and start the emulation with a button external to the canvas
  • add a stop button to stop the emulation and a reset button to clean the grid (here the canvas context had a different name respect to the first point and give a easy-to-solve error)
  • add a textbox to allow user to set the game interval (here ChatGPT called the function to read the textbox setInterval() making the whole program not working until I warned it that it redefined a standard js function).
  • in the click event listener of the canvas, blank the canvas before draw the grid and the cells (the original code redraw grid and cells without blanking the previous generation, so it cumulated all the generations making it a mess)
  • can you add a button that initialize the grid with cells representing the string “404”? (I was already creating the 404, from this point I noticed that there were functions and constants not previously defined but the chat gave them to me just asking for them)
  • them I ask for an object wrapping all the game but it was to large for a single output of the chat and from here I start to change things manually.
  • I also asked to change the click listener in a on mouse move listener to draw the initial state like in Paint, but only at the publication I noticed that x and y are inverted respect to the initial orientation in this listener and if you draw a horizontal line when the emulation start it will be vertical.

Conclusions

ChatGPT surprised me, at the beginning it was very effective, than increasing the size of the program it started to miss some point, but was always a good guideline for a programmer. I think that for a normal user would be difficult to create a program, but a programmer also with few experience can benefit a lot from it. From now on I think I’ll pay much more attention to AI and it’s capabilities.

Here the result I obtained (press Init to start):



Parameters:

Here as usual the code: https://github.com/zagonico86/my-snippets/tree/main/js/game-of-life

PS. Using ChatGPT for a while, it’s very effective to have a quick and good introduction to a new topic (it is similar to ask the key concepts to a colleague that already knows an argument!), to ask strategy suggestions or get useful samples of code, but it lacks of precision and problem solving capability in problems that are more difficult of average or “not main-stream”, for example it did get me a working Pi greek calculator function or an ascii art camera.

More important, while in a search engine we have many options and we can have privacy, here we simply can’t, so I find it limiting if I’m learning topics like hacking (any programmer should know the basic of it to write better code). This class of programs will change our way of working, will be hard competitors for search engine, but I see some limit.

This AI chat generation won’t replace us at work, but I’m not sure this won’t happen with the next generation! 🙂