• Publishers are finally taking notice to complaints over translations
    don't let this slip by and take initiative.
    Help here or complain directly to them (or both!)

Game of Life

Halo

varishangout.com
Regular
I was bored at work so I made a game of life.
The square placement struggles a bit and generally just places them in the outside line, but still makes neat patterns.

Code/Git


C:
#include <stdio.h>
#include <stdlib.h>
#define GRIDSIZE 10
#define MAXCELLS GRIDSIZE * GRIDSIZE
#define MINALIVENEIGHBOUR 2
#define MAXALIVENEIGHBOUR 3
typedef enum {DEAD,ALIVE} STATE;

typedef struct Location {
        int x,y;
};
typedef struct Cell {
    STATE state;
    struct Location pos;
    int neighbours;
};
typedef struct Grid {
    struct Cell cells[MAXCELLS];
};

void InitGrid(struct Grid *,int,int); //create grid of appropriate size
void PrintGrid(struct Grid); //print the grid
void Update(struct Grid *); //cycle through and update cells
int CheckNeighbour(struct Grid, int,int); //check a cells neighbours
STATE UpdateState(int, STATE);
int main()
{
    struct Grid playGrid;

    InitGrid(&playGrid,GRIDSIZE,GRIDSIZE);
    PrintGrid(playGrid);
    while(1) {
        system("clear");
        Update(&playGrid);
        PrintGrid(playGrid);
        sleep(1);
    }
    return 0;
}

void InitGrid(struct Grid *playGrid,int xMax,int yMax) {
    int cellCount = 0;
    time_t t;

    srand((unsigned) time(&t));
    //Create the height of the grid
     for(int y = 0; y < yMax; y++) {
            //Create the width of the grid
            for(int x = 0;x < xMax;x++) {
                srand(rand()%10);//using time wasn't making random numbers
                playGrid->cells[cellCount].pos.x = x;
                playGrid->cells[cellCount].pos.y = y;
                playGrid->cells[cellCount].state = (rand()%2);
                cellCount++;
            }
     }
}
void PrintGrid(struct Grid playGrid) {
    int currentCell = 0;
    for(int y = 0; y < GRIDSIZE; y++) {
        for(int x = 0; x < GRIDSIZE; x++) {
            switch(playGrid.cells[currentCell].state) {
                case ALIVE:
                    printf("X");
                    break;
                case DEAD:
                    printf("O");
                    break;
                default:
                    printf(" ");
                    break;
            }
            printf("|");
            currentCell++;
        }
        printf("\n");
    }
}
void Update(struct Grid *playGrid) {
    int cellCount = 0;
    while(cellCount < MAXCELLS) {
        playGrid->cells[cellCount].neighbours = CheckNeighbour(*playGrid,playGrid->cells[cellCount].pos.x,playGrid->cells[cellCount].pos.y);
        cellCount++;
    }
    cellCount = 0;
    while(cellCount < MAXCELLS) {
        playGrid->cells[cellCount].state = UpdateState(playGrid->cells[cellCount].neighbours,playGrid->cells[cellCount].state);
            cellCount++;
    }
}
int CheckNeighbour(struct Grid playGrid, int xPos,int yPos) {
    int aliveNeighbours = 0;

    int cellCount = 0;
    //Check Corner Cells
    //Gotta figure out how to do this better lol
    //0,0 AKA Top Left
    if(xPos == 0 && yPos == 0) {
    //0,Y AKA Top Right
        for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    else if (xPos == 0 && yPos == (GRIDSIZE-1)) {
        for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //X,0 AKA Bottom Left
    else if( xPos == (GRIDSIZE-1) && yPos == 0 ) {
        for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //X,Y AKA Bottom Right
    else if(xPos == (GRIDSIZE-1) && yPos == (GRIDSIZE-1)) {
        for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //Top Row
    else if(xPos == 0 ) {
         for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //Bottom Row
    else if(xPos == (GRIDSIZE-1)) {
         for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //Left Row
    else if(yPos == 0) {
         for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //Right Row
    else if(yPos == (GRIDSIZE-1)) {
         for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }
    }
    //Central Squares
    else {
          for(int y = 0; y < GRIDSIZE; y++) {
            for(int x = 0; x < GRIDSIZE; x++) {
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos+1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos+1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                if(playGrid.cells[cellCount].pos.x == xPos && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }                if(playGrid.cells[cellCount].pos.x == xPos-1 && playGrid.cells[cellCount].pos.y == yPos-1) {
                    if(playGrid.cells[cellCount].state == ALIVE) {
                            aliveNeighbours++;
                    }
                }
                cellCount++;
            }
        }

    }
    return aliveNeighbours;
}
STATE UpdateState(int aliveNeighbours,STATE cellState) {
    if(cellState == DEAD && aliveNeighbours == MAXALIVENEIGHBOUR) {
        return ALIVE;
    }
    else if (cellState == DEAD) {
        return DEAD;
    }
    if(aliveNeighbours < MINALIVENEIGHBOUR) {
        return DEAD;
    }
    if(aliveNeighbours > MAXALIVENEIGHBOUR) {
        return DEAD;
    }
    if(aliveNeighbours <= MAXALIVENEIGHBOUR && aliveNeighbours >= MINALIVENEIGHBOUR) {
        return ALIVE;
    }
}
 
Top