From 8cccbbc745abd5b6bf6d04ab980e3942f637b3df Mon Sep 17 00:00:00 2001 From: Katyanna Moura Date: Sun, 14 Jun 2015 23:17:35 -0300 Subject: [PATCH] Improves code readability --- defs.h | 3 ++ game.h | 15 ++++++++++ generation.h | 40 ++++++++++++++++++++++++++ main.c | 79 ++++------------------------------------------------ populate.h | 9 ++++++ 5 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 defs.h create mode 100644 game.h create mode 100644 generation.h create mode 100644 populate.h diff --git a/defs.h b/defs.h new file mode 100644 index 0000000..261d4ea --- /dev/null +++ b/defs.h @@ -0,0 +1,3 @@ +#define for_column for (int column = 0; column < width; column++) +#define for_line for (int line = 0; line < height; line++) +#define for_position for_line for_column \ No newline at end of file diff --git a/game.h b/game.h new file mode 100644 index 0000000..be86b28 --- /dev/null +++ b/game.h @@ -0,0 +1,15 @@ +#include "defs.h" + +int randomCellCreation(unsigned density){ + return rand() < (RAND_MAX / density) ? 1 : 0; +} + +void game(unsigned width, unsigned height){ + unsigned universe[height][width]; + for_position universe[line][column] = randomCellCreation(4); + while (1) { + populate(width, height, universe); + generation(width, height, universe); + usleep(200000); + } +} diff --git a/generation.h b/generation.h new file mode 100644 index 0000000..34b455f --- /dev/null +++ b/generation.h @@ -0,0 +1,40 @@ +#include "defs.h" + +struct Cells{ + unsigned currentCell; + unsigned top, topRight, topLeft; + unsigned bottom, bottomRight, bottomLeft; + unsigned left, right; +}; + +int cellStatus(struct Cells cell){ + int living = cell.top + cell.bottom + cell.left + cell.right + cell.topLeft + cell.topRight + cell.bottomRight + cell.bottomLeft; + + if (cell.currentCell){ + if (living <= 1 || living >= 4) return 0; + if (living == 2 || living == 3) return 1; + } else { + return (living == 3) ? 1 : 0; + } +} + +void generation(unsigned width, unsigned height, unsigned u[width][height]){ + unsigned new[width][height]; + struct Cells cell; + + for_position { + cell.currentCell = u[line][column]; + cell.top = u[line - 1][column]; + cell.bottom = u[line + 1][column]; + cell.left = u[line][column - 1]; + cell.right = u[line][column + 1]; + cell.topLeft = u[line - 1][column - 1]; + cell.topRight = u[line - 1][column + 1]; + cell.bottomLeft = u[line + 1][column - 1]; + cell.bottomRight = u[line + 1][column + 1]; + + new[line][column] = cellStatus(cell); + } + + for_line for_column u[line][column] = new[line][column]; +} diff --git a/main.c b/main.c index b0fa15c..27b089d 100644 --- a/main.c +++ b/main.c @@ -1,80 +1,11 @@ #include #include #include - -#define for_column for (int column = 0; column < w; column++) -#define for_line for (int line = 0; line < h; line++) -#define for_column_by_line for_column for_line -#define for_position for_line for_column - -void show(void *u, int w, int h){ - int (*universe)[w] = u; - printf("\033[H"); - for_line { - for_column printf(universe[line][column] ? "#" : " "); - printf("\n"); - } - fflush(stdout); -} - -int nature(int currentCell, int top, int bottom, int left, int right, int topLeft, int topRight, int bottomRight, int bottomLeft){ - int living = top + bottom + left + right + topLeft + topRight + bottomRight + bottomLeft; - - if (currentCell){ - if (living <= 1 || living >= 4){ - return 0; - } - - if (living == 2 || living == 3){ - return 1; - } - - } else { - if (living == 3) { - return 1; - } else { - return 0; - } - } -} - - -void evolve(void *u, int w, int h){ - unsigned (*universe)[w] = u; - unsigned new[w][h]; - int currentCell, topLeft, top, topRight, bottomLeft, bottom, bottomRight, left, right; - - for_position { - currentCell = universe[line][column]; - top = universe[line - 1][column]; - bottom = universe[line + 1][column]; - left = universe[line][column - 1]; - right = universe[line][column + 1]; - topLeft = universe[line - 1][column - 1]; - topRight = universe[line - 1][column + 1]; - bottomLeft = universe[line + 1][column - 1]; - bottomRight = universe[line + 1][column + 1]; - - new[line][column] = nature(currentCell, topLeft, top, topRight, bottomLeft, bottom, bottomRight, left, right); - } - - for_line for_column universe[line][column] = new[line][column]; -} - -int defineLiveCell(){ - return rand() < RAND_MAX / 5 ? 1 : 0; -} - -void game(int w, int h){ - unsigned universe[h][w]; - for_column_by_line universe[line][column] = defineLiveCell(); - while (1) { - show(universe, w, h); - evolve(universe, w, h); - usleep(200000); - } -} +#include "generation.h" +#include "populate.h" +#include "game.h" int main(){ - game(40, 40); + system("clear"); + game(80, 40); } diff --git a/populate.h b/populate.h new file mode 100644 index 0000000..6316abb --- /dev/null +++ b/populate.h @@ -0,0 +1,9 @@ +#include "defs.h" + +void populate(unsigned width, unsigned height, unsigned u[width][height]){ + printf("\033[H"); + for_line { + for_column printf(u[line][column] ? "#" : " "); + printf("\n"); + } +}