It randomly generates computer programs, runs them, and you can see what they do. On the left is a visualisation of the memory the program is running in, on the right, you see the code that makes up the program.
we see the memory the program runs in. There are 300 cells, each can hold a value from 0
to 255
. Three of those cells make up one pixel, the first cell determines the amount of red, the second the amount of green and the third the amount of blue that is used. So if all cells are 0
, the pixel is black, if the first one is 255
, it's bright red, if all three are 255
, the pixel is white.
This makes a total of 100 pixels, arranged from left to right, top to bottom.
we see our program. The language we use is brainfuck, a rather esoteric language that isn't really used to build applications but has some useful properties. When pressing the shuffle button, a new program is generated. When the program is running, a box shows which instruction is currently executed. Each character on its own is a separate instruction.
holds the memory cells. In theory, an infinite amount. In our case, just 300 cells. It is arranged in a loop, so if we reach the end, we just end up at the beginning again.
holds just one byte in our case, that is a value from 0 to 255. The cells themselves behave in a similar way to the tape: if we reach 255 and try to add 1, we just end up at 0. Subtracting 1 works the same way, wrapping from 0 to 255.
points to a single cell on the tape. It's the only way for the program to interact with the tape: It can only read or modify the cell that is pointed to. The pointer can be moved around to the left or right, one cell at a time.
are pretty minimal, there are just 8 different instructions, each one of them one character long.
Instruction | Explanation |
---|---|
+ | Add 1 to the value of the current cell |
- | Subtract 1 from the value of the current cell |
< | Move the pointer one cell to the left |
> | Move the pointer one cell to the right |
[ | If the value of the current cell is 0 , jump to the corresponding ] . If the value is not 0 , continue. |
] | If the value of the current cell is 0 , continue. If the value is not 0 , jump back to the corresponding [ |
. | Print the value of the current cell as an ascii character. Not implemented in colorfuck. |
, | Read a value from the input and place its ascii value into the current cell. Not implemented in colorfuck. |
It would be possible to do a similar thing with other programming languages. In fact, feel free to fork this and try around! I chose brainfuck for a few reasons: It's quite simple to build an interpreter, generating valid programs is rather simple and with all this, it's still turing complete.
There are a few shortcuts and creative freedoms we're taking. They do not impact the turing completenes of the brainfuck interpreter. Here's a list:
,
instruction is not implemented, it just acts as a noop. This is slightly illegal, however it ensures the same code always has the same output. Feel free to fork this and add an input field, make it return a random value or characters from the program itself.[
and ]
. (This isn't really a limitation)[]
. This combination of instruction does nothing in the best case and is an endless loop in the worst case, which usually makes a program quite boring. You can use this if you are writing your own brainfuck code and want it to terminate. There are still a lot of other combinations which essentialy do the same thing, so you will still get programs that eventually get stuck.