Commit db6b15d

mo khan <mo@mokhan.ca>
2014-12-29 22:14:35
start to define a world object.
1 parent 6919c25
bin/test_game_of_life
Binary file
src/print.c
src/world.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "world.h"
+
+World *world_create(int width, int height){
+  int bytes_for_cells = ((width*height)*sizeof(Cell));
+  int bytes_for_world = sizeof(World);
+  int bytes_to_allocate = bytes_for_world + bytes_for_cells;
+  printf("%d\n", bytes_to_allocate);
+
+  World *world = (World *)malloc(bytes_to_allocate);
+  memset(world, 0, bytes_to_allocate);
+  world->width = width;
+  world->height = height;
+  return world;
+}
src/world.h
@@ -0,0 +1,17 @@
+#include <stdbool.h>
+
+typedef struct {
+  bool alive;
+} Cell;
+
+typedef struct {
+  int width;
+  int height;
+  Cell* cells;
+} World;
+
+World* world_create(int width, int height);
+int world_neighbours(World* world, int index);
+World* world_evolve(World* world);
+
+void world_display(World* world);
src/world_test.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "world.h"
+
+#define run_test(function_name)\
+  printf("%s\n", #function_name);\
+  function_name();
+
+void test_world_create() {
+  int width = 3;
+  int height = 3;
+  World *world = world_create(width, height);
+
+  assert(width == world->width);
+  assert(height == world->height);
+}
+
+int main()
+{
+  run_test(test_world_create);
+  printf("\nOK\n");
+  return 0;
+}
Makefile
@@ -1,5 +1,5 @@
 default: src/*.c src/*.h
 	rm -fr bin
 	mkdir -p bin
-	gcc -std=c99 -Wall -o bin/gol src/gol.c src/main.c
-	./bin/gol
+	gcc -std=c99 -Wall -o bin/test_game_of_life src/world_test.c src/world.c src/print.c
+	./bin/test_game_of_life
Rakefile
@@ -14,3 +14,8 @@ task :test => :clean do
   sh "gcc -std=c99 -Wall -g src/gol.c src/gol_test.c -o bin/gol_test"
   sh "./bin/gol_test"
 end
+
+task :spec => :clean do
+  sh "gcc -std=c99 -Wall -g src/world.c src/world_test.c -o bin/game_of_life"
+  sh "./bin/game_of_life"
+end