diff --git a/GameOfLife.c b/GameOfLife.c index cb02cd8066bc99f92f1e4bf1bdd05aaf5fb77612..c185e686b97a631385acdf89e620b82c19f34d06 100644 --- a/GameOfLife.c +++ b/GameOfLife.c @@ -3,6 +3,10 @@ #include <unistd.h> #include <string.h> +#define ALIVE 1 +#define DEAD 0 +#define clear() printf("\033[H\033[J") + typedef struct Generation { int **generation; int rows; @@ -17,10 +21,6 @@ typedef struct Game { int cycleTime; } game_t; -#define ALIVE 1 -#define DEAD 0 -#define clear() printf("\033[H\033[J") - void startGame (game_t *game, char **argv); void defineSizes (game_t *game, char **argv); void memoryAlloc (game_t *game); @@ -29,13 +29,14 @@ void addCell (game_t *game, int* i); void removeCell (game_t *game, int* i); int testSize (game_t *game, int x, int y); void playGame (game_t *game); -void printGeneration (game_t *game); +void createNextGeneration (game_t *game); void nextCellStatus (game_t *game, int x, int y); int checkNeighbors (game_t *game, int x, int y); void createLife (game_t *game, int x, int y); void keepStatus (game_t *game, int x, int y); void killCell (game_t *game, int x, int y); void incrementGeneration (game_t *game); +void printGeneration (game_t *game); int main (int argc, char **argv) { @@ -206,15 +207,12 @@ int testSize (game_t *game, int x, int y) void playGame (game_t *game) { - int i, j; - printGeneration (game); while (game->generationNumber < game->totalGenerations) { - for ( i=0; i < game->this.rows; i++ ) - for ( j=0; j < game->this.cols; j++ ) - nextCellStatus (game, i, j); + + createNextGeneration (game); incrementGeneration (game); @@ -228,59 +226,16 @@ void playGame (game_t *game) clear(); } -void printGeneration (game_t *game) +void createNextGeneration (game_t *game) { int i, j; - clear(); - - printf ("Generation: %d\n\n", game->generationNumber); - - printf (" "); - for ( j=1; j <= game->this.cols; j++) - if (j % 5 == 0 || j == 1) - printf ("%2d", j); - else - printf (" "); - printf ("\n"); - - for ( i=1; i <= game->this.rows; i++ ) - { - if (i % 5 == 0 || i == 1) - printf ("%2d", i); - else - printf (" "); - - for ( j=1; j <= game->this.cols; j++ ) - { - - if (game->this.generation[i-1][j-1] == ALIVE) - printf (" +"); - else if (game->this.generation[i-1][j-1] == DEAD) - printf (" "); - else - printf (" !"); - } - - if (i % 5 == 0 || i == 1) - printf ("%2d", i); - else - printf (" "); - - printf ("\n"); - } - - printf (" "); - for ( j=1; j <= game->this.cols; j++) - if (j % 5 == 0 || j == 1) - printf ("%2d", j); - else - printf (" "); - printf ("\n"); - + for ( i=0; i < game->this.rows; i++ ) + for ( j=0; j < game->this.cols; j++ ) + nextCellStatus (game, i, j); } -/* Checks if the status of a Cell in the next generation. */ +/* Updates the status of a Cell in the next generation. */ void nextCellStatus (game_t *game, int x, int y) { switch ( checkNeighbors (game, x, y) ) @@ -303,13 +258,14 @@ int checkNeighbors (game_t *game, int x, int y) { int i, j, aliveCellCount = 0; - /* Two fors that runs all the neighbors of a cell. */ + /* Two fors that runs through all the neighbors of a cell. */ for ( i=-1; i <= 1; i++ ) for ( j=-1; j <= 1; j++ ) /* Checks if the coordinate is inside the matrix. */ if ( !( (x+i < 0) || (y+j < 0) || (x+i >= game->this.rows) || (y+j >= game->this.cols) ) ) /* Make sure to not compare with the middle cell. */ if ( !( (i==0) && (j==0) ) ) + /* Increment the counter if the cell is alive */ if ( game->this.generation[x+i][y+j] == ALIVE ) aliveCellCount++; @@ -343,3 +299,53 @@ void incrementGeneration (game_t *game) game->generationNumber++; } +void printGeneration (game_t *game) +{ + int i, j; + + clear(); + + printf ("Generation: %d\n\n", game->generationNumber); + + printf (" "); + for ( j=1; j <= game->this.cols; j++) + if (j % 5 == 0 || j == 1) + printf ("%2d", j); + else + printf (" "); + printf ("\n"); + + for ( i=1; i <= game->this.rows; i++ ) + { + if (i % 5 == 0 || i == 1) + printf ("%2d", i); + else + printf (" "); + + for ( j=1; j <= game->this.cols; j++ ) + { + + if (game->this.generation[i-1][j-1] == ALIVE) + printf (" +"); + else if (game->this.generation[i-1][j-1] == DEAD) + printf (" "); + else + printf (" !"); + } + + if (i % 5 == 0 || i == 1) + printf ("%2d", i); + else + printf (" "); + + printf ("\n"); + } + + printf (" "); + for ( j=1; j <= game->this.cols; j++) + if (j % 5 == 0 || j == 1) + printf ("%2d", j); + else + printf (" "); + printf ("\n"); +}