Commit f2a08bf

mo khan <mo@mokhan.ca>
2014-10-27 03:54:27
add test for finding neighbors to the nw, ne, sw and se.
1 parent fb30b98
Changed files (2)
gol.c
@@ -5,40 +5,43 @@
 #include <string.h>
 #include "gol.h"
 
-int alive(char value) {
-  return value == 'x' ? 1 : 0;
-};
+int alive(char value) { return value == 'x' ? 1 : 0; };
 
-int west_of(char* world, int index) {
-  if (index == 0 || index == 3 || index == 6) {
-    return alive(world[index + 2]);
-  }
-  return alive(world[index - 1]);
+int west_of(int index) {
+  if (index == 0 || index == 3 || index == 6) { return index + 2; }
+  return index - 1;
 }
 
-int east_of(char* world, int index) {
-  if (index == 2 || index == 5 || index == 8) {
-    return alive(world[index - 2]);
-  }
-  return alive(world[index + 1]);
+int east_of(int index) {
+  if (index == 2 || index == 5 || index == 8) { return index - 2; }
+  return index + 1;
 }
 
-int north_of(char* world, int index) {
-  if (index == 0 || index == 1 || index == 2) {
-    return alive(world[index + 6]);
-  }
-  return alive(world[index - 3]);
+int north_of(int index) {
+  if (index == 0 || index == 1 || index == 2) { return index + 6; }
+  return index - 3;
 }
 
-int south_of(char* world, int index) {
-  if (index == 6 || index == 7 || index == 8) {
-    return alive(world[index - 6]);
-  }
-  return alive(world[index + 3]);
+int south_of(int index) {
+  if (index == 6 || index == 7 || index == 8) { return index - 6; }
+  return index + 3;
 }
 
+int north_west_of(int index) { return west_of(north_of(index)); }
+int north_east_of(int index) { return east_of(north_of(index)); }
+int south_west_of(int index) { return west_of(south_of(index)); }
+int south_east_of(int index) { return east_of(south_of(index)); }
+
 int living_neighbours_for(char* world, int index){
-  return west_of(world, index) + east_of(world, index) + north_of(world, index) + south_of(world, index);
+  return alive(world[west_of(index)])
+    + alive(world[east_of(index)])
+    + alive(world[north_of(index)])
+    + alive(world[south_of(index)])
+    + alive(world[north_west_of(index)])
+    + alive(world[north_east_of(index)])
+    + alive(world[south_west_of(index)])
+    + alive(world[south_east_of(index)])
+    ;
 }
 
 char* evolve(char* world) {
gol_test.c
@@ -77,17 +77,18 @@ static char* it_returns_the_correct_number_of_living_neighbors() {
   char world[3][3] = {
     { 'x', ' ', 'x' },
     { ' ', ' ', ' ' },
-    { ' ', ' ', ' ' },
+    { 'x', ' ', 'x' },
   };
-  assert_equal(living_neighbours_for(*world, 0) == 1, "0, 0 should return 1");
-  assert_equal(living_neighbours_for(*world, 1) == 2, "1, 0 should return 2");
-  assert_equal(living_neighbours_for(*world, 2) == 1, "2, 0 should return 1");
-  assert_equal(living_neighbours_for(*world, 3) == 1, "0, 1 should return 1");
-  assert_equal(living_neighbours_for(*world, 4) == 0, "1, 1 should return 0");
-  assert_equal(living_neighbours_for(*world, 5) == 1, "2, 1 should return 1");
-  assert_equal(living_neighbours_for(*world, 6) == 1, "0, 2 should return 1");
-  assert_equal(living_neighbours_for(*world, 7) == 0, "1, 2 should return 0");
-  assert_equal(living_neighbours_for(*world, 8) == 1, "2, 2 should return 1");
+  printf("FOUND: %d\n", living_neighbours_for(*world, 0));
+  assert_equal(living_neighbours_for(*world, 0) == 3, "0, 0 should return 3");
+  assert_equal(living_neighbours_for(*world, 1) == 4, "1, 0 should return 4");
+  assert_equal(living_neighbours_for(*world, 2) == 3, "2, 0 should return 3");
+  assert_equal(living_neighbours_for(*world, 3) == 4, "0, 1 should return 4");
+  assert_equal(living_neighbours_for(*world, 4) == 4, "1, 1 should return 4");
+  assert_equal(living_neighbours_for(*world, 5) == 4, "2, 1 should return 4");
+  assert_equal(living_neighbours_for(*world, 6) == 3, "0, 2 should return 3");
+  assert_equal(living_neighbours_for(*world, 7) == 4, "1, 2 should return 4");
+  assert_equal(living_neighbours_for(*world, 8) == 3, "2, 2 should return 3");
   return 0;
 }