Cellular Automation - Modified Game of Life - C++ + SDL

cellular automata c++

In this little simulation demo I created four simple rules, of which can be activated by uncommenting them out in the source code. Though nothing too complex emerges, I still liked some the resulting behavior. Particularly, rule1(), results in colors slowly grouping together in various forms. It may be hard to immediately noticed so watch carefully. This was motivated by John Conway's Game of Life. The program was written in C/C++, all graphics are done using SDL The whole source can be found here main.cpp

Compile

g++ main.cpp -lSDL
Run
./a.out

Game of Life Patterns

Game of Life Patterns Game of Life Patterns Game of Life Patterns

Rule1()

for(int x = 0; x < worldWidth; x++) {
    for(int y = 0; y < worldHeight; y++) {
        int r = world[x][y].r;
        int g = world[x][y].g;
        int b = world[x][y].b;
        /*
          o o o
          o x o
          o o o
        */
        // RULES
        if(x > 0 && y > 0) { // top left
            if(world[x - 1][y - 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x - 1][y - 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x - 1][y - 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(y > 0) { // top
            if(world[x][y - 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x][y - 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x][y - 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(x < worldWidth - 1 && y > 0) { // top right
            if(world[x + 1][y - 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x + 1][y - 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x + 1][y - 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(x > 0) { //  left
            if(world[x - 1][y].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x - 1][y].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x - 1][y].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(x < worldWidth - 1) { // right
            if(world[x + 1][y].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x + 1][y].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x + 1][y].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(x > 0 && y < worldHeight - 1) { // bottom left
            if(world[x - 1][y + 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x - 1][y + 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x - 1][y + 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(y < worldHeight - 1) { // bottom
            if(world[x][y + 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x][y + 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x][y + 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(x < worldWidth - 1 && y < worldHeight - 1) { // bottom right
            if(world[x + 1][y + 1].r > 200) {
                r+=2; g--; b--;
            }
            if(world[x + 1][y + 1].g > 200) {
                r--; g+=2; b--;
            }
            if(world[x + 1][y + 1].b > 200) {
                r--; g--; b+=2;
            }
        }
        if(r < 0) {
            r = 0;
        }
        if(g < 0) {
            g = 0;
        }
        if(b < 0) {
            b = 0;
        }
        if(r > 255) {
            r = 255;
        }
        if(g > 255) {
            g = 255;
        }
        if(b > 255) {
            b = 255;
        }
        world[x][y].r = r;
        world[x][y].g = g;
        world[x][y].b = b;
    }
}


comments powered by Disqus