Commit e9d4a6a

mo khan <mo@mokhan.ca>
2014-10-27 01:44:25
refactor to use structs.
1 parent 9052cc8
gol.c
@@ -1,28 +1,67 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include "gol.h"
+#include <time.h>
 
-int living_neighbours_for(char world[][3], int x, int y){
-  int west = world[y][x-1] == 'x' ? 1 : 0;
-  int east = world[y][x+1] == 'x' ? 1 : 0;
+Cell find_cell(World world, int x, int y) {
+  for (int i = 0; i < world.number_of_cells; i++) {
+    Cell cell = world.cells[i];
+    if (cell.x == x && cell.y == y) {
+      return cell;
+    }
+  }
+  Cell null_cell;
+  null_cell.x = -1;
+  null_cell.y = -1;
+  null_cell.alive = ' ';
+  return null_cell;
+}
+
+int living_neighbours_for(World world, Cell cell){
+  int west = find_cell(world, cell.y, cell.x - 1).alive == 'x' ? 1 : 0;
+  int east = find_cell(world, cell.y, cell.x + 1).alive == 'x' ? 1 : 0;
   return west + east;
 }
 
-void evolve(char world[3][3]) {
-  for (int y = 0; y < 3; y++) {
-    for (int x = 0; x < 3; x++) {
-      char* cell = &world[y][x];
-      int neighbours = living_neighbours_for(world, x, y);
-      printf("%d, %d = '%c'. n: %d\n", x, y, *cell, neighbours);
-      world[y][x] = (neighbours >= 2) ? 'x' : ' ';
-    }
+World evolve(World world) {
+  for (int i = 0; i < world.number_of_cells; i++) {
+    Cell cell = world.cells[i];
+    int neighbours = living_neighbours_for(world, cell);
+    cell.alive = (neighbours >= 2) ? 'x' : ' ';
   }
+  return world;
+}
+
+void print(World world) {
+  for (int i = 0; i < world.number_of_cells; i++) {
+    Cell cell = world.cells[i];
+    if (i % 3 == 0) { printf("\n---\n"); }
+    printf("%c", cell.alive);
+  }
+  printf("\n---\n");
+}
+
+char random_life() {
+  /*srand(time(NULL));*/
+  /*return rand() % 7 == 0 ? 'x' : ' ';*/
+  return 'x';
 }
 
-void print(char world[][3]) {
-  for (int y = 0; y < 3; y++) {
-    printf("%d:", y);
-    for (int x = 0; x < 3; x++) {
-      printf("%c", world[y][x]);
+World create_world(int height, int width){
+  World world;
+  Cell* cells = (Cell*)malloc(sizeof(Cell) * (height * width));
+  int cell_number = 0;
+  for (int y = 0; y < height; y++) {
+    for (int x = 0; x < width; x++) {
+      Cell cell;
+      cell.x = x;
+      cell.y = y;
+      cell.alive = random_life();
+      cells[cell_number] = cell;
+      ++cell_number;
     }
-    printf("\n");
   }
+  world.cells = cells;
+  world.number_of_cells = height*width;
+  return world;
 }
gol.h
@@ -1,3 +1,16 @@
-int living_neighbours_for(char world[][3], int x, int y);
-void evolve(char world[3][3]);
-void print(char world[][3]);
+
+typedef struct {
+  int x;
+  int y;
+  char alive;
+} Cell;
+
+typedef struct {
+  Cell* cells;
+  int number_of_cells;
+} World;
+
+int living_neighbours_for(World world, Cell cell);
+World evolve(World world);
+void print(World world);
+World create_world(int height, int width);
gol_test.c
@@ -30,8 +30,8 @@ static char* any_live_cell_with_two_live_neighbours_lives_on_to_the_next_generat
     { ' ', ' ', ' ' },
     { ' ', ' ', ' ' },
   };
-  evolve(world);
-  assert_equal(world[0][1] == 'x', "should live because has 2 live neighbors");
+  char* new_world = evolve(world);
+  assert_equal(&new_world[0][1] == 'x', "should live because has 2 live neighbors");
   return 0;
 }
 
main.c
@@ -3,12 +3,10 @@
 #include "gol.h"
 
 int main(int argc, char **argv) {
-  char world[3][3] = {
-    { 'x', 'x', 'x' },
-    { 'x', 'x', 'x' },
-    { 'x', 'x', 'x' },
-  };
+  World world = create_world(3, 3);
   system("clear");
-  evolve(world);
-  print(world);
+  /*for (int i = 0; i < 5; i++) {*/
+    /*evolve(world);*/
+    print(world);
+  /*}*/
 }