-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,11 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |