Update
This commit is contained in:
parent
c81a62c894
commit
3d2999a451
9 changed files with 144 additions and 24 deletions
125
game_of_life.md
125
game_of_life.md
|
@ -1,7 +1,34 @@
|
|||
# Game Of Life
|
||||
|
||||
*--> Reveal the secret of life with these four simple rules! <--*
|
||||
|
||||
Game of Life (sometimes just *Life*) is probably the most famous [cellular automaton](cellular_automaton.md) (mathematical simulation set on a grid of cells), which with only four simple rules gives rise to a world with patterns that seem to behave similarly to simple biological [life](life.md). It was invented in 1970 by [John Conway](john_conway.md), a famous mathematician who researched games, and has since gained popularity among programmers, mathematicians and other nerds as it's not only scientifically valuable, it is also [awesome](beauty.md) and [fun](fun.md) to play around with as a kind of [sandbox](sandbox.md) toy; it is very easy to program, play around with, modify and can be explored to great depth. The word *game* is used because what we have here is really a zero-player mathematical [game](game.md), which is furthermore completely [deterministic](determinism.md) (there is no randomness), so we only choose some starting state of the world and then watch the game "play itself". Game of Life is similar systems such as [rule 110](rule110.md) and [Langton's ant](langtons_ant.md).
|
||||
|
||||
```
|
||||
. [][]. . . . . . . . []. []. . . . . . . [][][]. . . . . . . []. []. . . . . .
|
||||
[]. . [][]. . . . . . . []. . . . . . . []. . . []. . . . . . . []. . . . . . .
|
||||
[][]. [][]. . . . . . . . . . . . . . . . . . . . []. . . . . . . . . . . . . []
|
||||
[][]. . []. . . . . . . . . . . . []. . []. . . []. . . . . . . . . . . . []. []
|
||||
. . [][]. . . . . . . . . . . . []. []. . [][][]. . . . . . . . . . . . []. [].
|
||||
. . . . . . . . . . . []. . . . . []. . . . . . . . . . . . . []. . . . . []. .
|
||||
. . . . . . . . . . []. []. . . . . . . . . . . . . . . . . []. []. . . . . . .
|
||||
. . . . . . . . . . []. []. . . . . . . . . . . . . . . . . []. []. . . . . . .
|
||||
. . . . . . . . . . . []. . . . . . . . . . . . . . . . . . . []. . . . . . . .
|
||||
. . . . . . . . . . . . . . [][]. . . . ___\ . . . . . . . . . . . . . . [][]. . . .
|
||||
. . . . . . . . . . . . . . [][]. . . . / . . . . . . . . . . . . . . [][]. . . .
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
. . . . . . . . []. []. . . . [][]. . . . . . . . . . . . . []. . . . [][]. . .
|
||||
. . . . . . . . . [][]. . . []. . []. . . . . . . . . . []. []. . . []. . []. .
|
||||
. . . . . . . . . []. . . . . [][]. . . . . . . . . . . . [][]. . . . [][]. . .
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
. . . . . . . . . . . [][]. . . . . . . . . . . . . . . . . . [][]. . . . . . .
|
||||
```
|
||||
|
||||
*Two consecutive frames of the game, notice the glider at bottom middle, which keeps moving bottom right, and still life forms in the right part, which just stay still.*
|
||||
|
||||
Study of the game goes extremely deep, people are discovering new patterns and concepts. See e.g. LifeWiki at https://conwaylife.com/wiki/Main_Page, Catalogue at https://catagolue.hatsya.com or Life Lexicon at https://conwaylife.com/ref/lexicon/lex.htm. The Catalogue claims to have discovered 478223 distinct types of objects so far, LifeWiki has 1651 documented patterns (many with just boring numeric names, other ones with funny names like *[Cthulhu](cthulhu.md)*, *En retard*, *Meatball*, *p58 toadsucker*, *Overweight spaceship*, *Why not*, even *[Unix](unix.md)* lol).
|
||||
|
||||
The following is a sum up of some basic properties of the game, many of which should show why it is so significant:
|
||||
|
@ -13,7 +40,7 @@ The following is a sum up of some basic properties of the game, many of which sh
|
|||
- It is [Turing complete](turing_complete.md), meaning the game can be used as a universal computer, with the same computational power as any other computer.
|
||||
- It is very fun to play around with. There are many creatures to discover, observe and document (making this kind of a Pokémon game lol?).
|
||||
|
||||
## Rules
|
||||
## Rules And Their Implications
|
||||
|
||||
We have an [infinite](infinity.md), regular two dimensional grid of cells, each of which may be in one of two states: alive or dead. Each cell considers its nearest eight cells its neighbors. The game runs in discrete steps, or rounds. Every round each cell counts its neighbors' states and will set its state for the next round according to these rules:
|
||||
|
||||
|
@ -22,13 +49,101 @@ We have an [infinite](infinity.md), regular two dimensional grid of cells, each
|
|||
| alive | 2 or 3 live neighbors | else (under/overpopulation) |
|
||||
| dead | 3 live neighbors | else |
|
||||
|
||||
## Code Example
|
||||
TODO: basic structures, chaos vs order etcetc.
|
||||
|
||||
TODO
|
||||
## Code/Programming
|
||||
|
||||
## Programming
|
||||
The following is a simple [C](c.md) implementation of a [wrapping](wrap.md) version of game of life (i.e. the world is not actually infinite):
|
||||
|
||||
TODO: techniques for optimization etc.
|
||||
```
|
||||
#include <stdio.h>
|
||||
|
||||
#define WORLD_SIZE 20
|
||||
|
||||
unsigned char world[WORLD_SIZE * WORLD_SIZE];
|
||||
|
||||
unsigned char getCell(int x, int y)
|
||||
{
|
||||
return world[y * WORLD_SIZE + x] & 0x01;
|
||||
}
|
||||
|
||||
void setCell(int x, int y)
|
||||
{
|
||||
world[y * WORLD_SIZE + x] |= 0x01;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned char random = 30;
|
||||
|
||||
for (int i = 0; i < WORLD_SIZE * WORLD_SIZE; ++i)
|
||||
{
|
||||
world[i] = random > 127;
|
||||
random = random * 13 + 22;
|
||||
}
|
||||
|
||||
char in = 0;
|
||||
int step = 0;
|
||||
|
||||
while (in != 'q')
|
||||
{
|
||||
unsigned char *cell = world;
|
||||
|
||||
for (int y = 0; y < WORLD_SIZE; ++y)
|
||||
{
|
||||
int yU = y == 0 ? (WORLD_SIZE - 1) : (y - 1),
|
||||
yD = (y + 1) % WORLD_SIZE,
|
||||
xL = WORLD_SIZE - 1,
|
||||
xR = 1;
|
||||
|
||||
for (int x = 0; x < WORLD_SIZE; ++x)
|
||||
{
|
||||
int neighbors =
|
||||
getCell(xL,yU) + getCell(x,yU) + getCell(xR,yU) +
|
||||
getCell(xL,y) + getCell(xR,y) +
|
||||
getCell(xL,yD) + getCell(x,yD) + getCell(xR,yD);
|
||||
|
||||
if ((*cell) & 0x01)
|
||||
{
|
||||
putchar('[');
|
||||
putchar(']');
|
||||
|
||||
if (neighbors == 2 || neighbors == 3)
|
||||
*cell |= 0x02;
|
||||
}
|
||||
else
|
||||
{
|
||||
putchar('.');
|
||||
putchar(' ');
|
||||
|
||||
if (neighbors == 3)
|
||||
*cell |= 0x02;
|
||||
}
|
||||
|
||||
xL = x;
|
||||
xR = (x + 2) % WORLD_SIZE;
|
||||
|
||||
cell++;
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
for (int i = 0; i < WORLD_SIZE * WORLD_SIZE; ++i)
|
||||
world[i] >>= 1;
|
||||
|
||||
printf("\nstep %d\n",step);
|
||||
puts("press RETURN to step, Q to quit");
|
||||
|
||||
step++;
|
||||
in = getchar();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
The world cells are kept in the `world` array -- each cell holds the current state in the lowest bit. We perform drawing and update of the world at the same time. Notice especially that while updating the cells, we mustn't overwrite the cell's current state until its neighbors have been updated as well! Not realizing this is a **common beginner error** and results in so called [naive](naive.md) implementation. We avoid this error by first storing each cell's next state in the second lowest bit (keeping its current state in the lowest bit), and then, after all cells have been updated, we iterate over all cells again and perform one bit shift to the left (making the computed next states into current states).
|
||||
|
||||
TODO: extensions, continuous, code, optimizations
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue