Commit 11da7d3

mo khan <mo@mokhan.ca>
2014-12-30 14:53:14
update maint to use the new world object.
1 parent 21627c7
src/main.c
@@ -2,24 +2,21 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
-#include "gol.h"
-
-int WIDTH = 5;
-int HEIGHT = 5;
-int NUMBER_OF_CELLS;
+#include "world.h"
 
 void clear_screen(){
   system("clear");
 }
 
 int random_life() {
-  return rand() % 2 == 0 ? ALIVE : DEAD;
+  return rand() % 2 == 0 ? true : false;
 }
 
-char* random_world(){
-  char *world = malloc(sizeof(char) * NUMBER_OF_CELLS);
-  for (int i = 0; i < NUMBER_OF_CELLS; i++) {
-    world[i] = random_life();
+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;
 }
@@ -31,22 +28,21 @@ int from_env(char* env_name, int default_value) {
 }
 
 int main(int argc, char **argv) {
-  WIDTH=from_env("WIDTH", 5);
-  HEIGHT=from_env("HEIGHT", WIDTH);
-  NUMBER_OF_CELLS=WIDTH*HEIGHT;
+  int width = from_env("WIDTH", 5);
+  int height = from_env("HEIGHT", width);
 
   srand(time(NULL));
   clear_screen();
-  printf("%d x %d world\n", WIDTH, HEIGHT);
+  printf("%d x %d world\n", width, height);
   sleep(2);
 
-  char* new_world = random_world();
+  World *new_world = random_world(width, height);
   int i = 0;
 
   while(1) {
     printf("GENERATION: %d\n", i);
-    display(new_world);
-    char* tmp = evolve(new_world);
+    world_print(new_world);
+    World *tmp = world_evolve(new_world);
     free(new_world);
     new_world = tmp;
     sleep(1);
src/world.c
@@ -3,8 +3,6 @@
 #include <string.h>
 #include "world.h"
 
-int world_number_of_cells(World *world);
-
 int west_of(World *world, int index) {
   return (index % world->width == 0) ? index + (world->width-1) : index - 1;
 }
src/world.h
@@ -8,5 +8,7 @@ typedef struct {
 
 World* world_create(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);
Makefile
@@ -1,7 +1,7 @@
 default: src/*.c src/*.h
 	rm -fr bin
 	mkdir -p bin
-	gcc -std=c99 -Wall -o bin/game_of_life 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 src/print.c
 	./bin/game_of_life
 
 test: src/*.c src/*.h