Commit 17fef14
Changed files (3)
src
bin/test_game_of_life
Binary file
src/world.c
@@ -3,15 +3,23 @@
#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);
+Cell* cell_create(int number_of_cells) {
+ Cell *cells = malloc(sizeof(Cell) * number_of_cells);
+ memset(cells, 0, sizeof(Cell) * number_of_cells);
+
+ for (int i = 0; i < number_of_cells; i++) {
+ cells[sizeof(Cell) * i].alive = false;
+ }
+ return cells;
+}
+
+World *world_create(int width, int height) {
+ int number_of_cells = width*height;
+ World *world = (World *)malloc(sizeof(World));
+ memset(world, 0, sizeof(World));
- World *world = (World *)malloc(bytes_to_allocate);
- memset(world, 0, bytes_to_allocate);
world->width = width;
world->height = height;
+ world->cells = cell_create(number_of_cells);
return world;
}
src/world_test.c
@@ -16,9 +16,15 @@ void test_world_create() {
assert(height == world->height);
}
+void test_world_create_should_create_all_cells(){
+ World *world = world_create(1, 1);
+ assert(false == world->cells[0].alive);
+}
+
int main()
{
run_test(test_world_create);
+ run_test(test_world_create_should_create_all_cells);
printf("\nOK\n");
return 0;
}