Commit f233e93
src/main.c
@@ -3,50 +3,42 @@
#include <unistd.h>
#include <time.h>
#include "world.h"
+#include "display.h"
void clear_screen(){
system("clear");
}
-int random_life() {
- return rand() % 2 == 0 ? true : false;
-}
-
-World* random_world(int width, int height){
- World *world = world_create(width, height);
- int number_of_cells = world_number_of_cells(world);
- for (int i = 0; i < number_of_cells; i++) {
- world_cell_at(world, i)->alive = random_life();
- }
- return world;
-}
-
int from_env(char* env_name, int default_value) {
char* value = getenv(env_name);
- if(value != NULL) { return atoi(value); }
- return default_value;
+ return (value != NULL) ? atoi(value) : default_value;
}
-int main(int argc, char **argv) {
- int width = from_env("WIDTH", 5);
- int height = from_env("HEIGHT", width);
-
+void world_start(World *world) {
srand(time(NULL));
clear_screen();
- printf("%d x %d world\n", width, height);
+ printf("%d x %d world\n", world->width, world->height);
sleep(2);
- World *new_world = random_world(width, height);
+ World *new_world = world;
int i = 0;
- while(1) {
+ while(true) {
printf("GENERATION: %d\n", i);
world_print(new_world);
World *tmp = world_evolve(new_world);
- free(new_world);
+ world_destroy(new_world);
new_world = tmp;
sleep(1);
clear_screen();
++i;
}
}
+
+int main(int argc, char **argv) {
+ int width = from_env("WIDTH", 5);
+ int height = from_env("HEIGHT", width);
+
+ world_start(world_random(width, height));
+ return 0;
+}
src/print.c
src/world.c
@@ -63,7 +63,7 @@ int world_neighbours(World *world, int index) {
+ cell_alive(south_east_cell);
}
-World *world_create(int width, int height) {
+World* world_create(int width, int height) {
int number_of_cells = width*height;
World *world = (World *)malloc(sizeof(World));
memset(world, 0, sizeof(World));
@@ -74,10 +74,21 @@ World *world_create(int width, int height) {
return world;
}
+int random_life() {
+ return rand() % 2 == 0 ? true : false;
+}
+
+World* world_random(int width, int height){
+ World *world = world_create(width, height);
+ for (int i = 0; i < world_number_of_cells(world); i++) {
+ world_cell_at(world, i)->alive = random_life();
+ }
+ return world;
+}
+
World* world_evolve(World *world) {
- int number_of_cells = world_number_of_cells(world);
World *new_world = world_create(world->width, world->height);
- for (int i = 0; i < number_of_cells; i++) {
+ for (int i = 0; i < world_number_of_cells(world); i++) {
int neighbours = world_neighbours(world, i);
if (world->cells[i*sizeof(Cell)].alive == true) {
new_world->cells[i].alive = (neighbours >= 2 && neighbours <= 3) ? true : false;
@@ -89,10 +100,13 @@ World* world_evolve(World *world) {
}
void world_print(World *world) {
- int number_of_cells = world_number_of_cells(world);
- for (int i = 0; i < number_of_cells; i++) {
+ for (int i = 0; i < world_number_of_cells(world); i++) {
if (i % world->width == 0) { printf("\n"); }
printf("%s", world->cells[i].alive ? "X" : " ");
}
printf("\n");
}
+
+void world_destroy(World *world) {
+ free(world);
+}
src/world.h
@@ -7,8 +7,10 @@ typedef struct {
} World;
World* world_create(int width, int height);
+World* world_random(int width, int height);
World* world_evolve(World* world);
Cell* world_cell_at(World *world, int index);
int world_number_of_cells(World *world);
int world_neighbours(World *world, int index);
void world_print(World *world);
+void world_destroy(World *world);
Makefile
@@ -1,11 +1,11 @@
default: src/*.c src/*.h
rm -fr bin
mkdir -p bin
- gcc -std=c99 -Wall -o bin/game_of_life src/main.c src/world.c src/cell.c src/print.c
+ gcc -std=c99 -Wall -o bin/game_of_life src/main.c src/world.c src/cell.c
./bin/game_of_life
test: src/*.c src/*.h
rm -fr bin
mkdir -p bin
- gcc -std=c99 -Wall -o bin/test_game_of_life src/world_test.c src/world.c src/cell.c src/print.c
+ gcc -std=c99 -Wall -o bin/test_game_of_life src/world_test.c src/world.c src/cell.c
./bin/test_game_of_life