Commit b1176a9
Changed files (4)
gol.c
@@ -1,67 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
-#include "gol.h"
#include <time.h>
+#include <math.h>
+#include <string.h>
+#include "gol.h"
+
+int alive(char value) {
+ return value == '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;
- }
+int west_of(char* world, int index) {
+ if (index == 0 || index == 3 || index == 6) {
+ return alive(world[index + 2]);
}
- Cell null_cell;
- null_cell.x = -1;
- null_cell.y = -1;
- null_cell.alive = ' ';
- return null_cell;
+ return alive(world[index - 1]);
}
-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;
+int east_of(char* world, int index) {
+ if (index == 2 || index == 5 || index == 8) {
+ return alive(world[index - 2]);
+ }
+ return alive(world[index + 1]);
}
-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' : ' ';
+int north_of(char* world, int index) {
+ if (index == 0 || index == 1 || index == 2) {
+ return alive(world[index + 6]);
}
- return world;
+ return alive(world[index - 3]);
}
-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);
+int south_of(char* world, int index) {
+ if (index == 6 || index == 7 || index == 8) {
+ return alive(world[index - 6]);
}
- printf("\n---\n");
+ return alive(world[index + 3]);
}
-char random_life() {
- /*srand(time(NULL));*/
- /*return rand() % 7 == 0 ? 'x' : ' ';*/
- return 'x';
+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);
}
-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;
- }
+char* evolve(char* world) {
+ int number_of_cells = 9;
+ char new_world[number_of_cells + 1];
+ memset(new_world, ' ', number_of_cells);
+ new_world[number_of_cells] = 0;
+ for (int i = 0; i < number_of_cells; i++) {
+ int neighbours = living_neighbours_for(world, i);
+ new_world[i] = (neighbours >= 2) ? 'x' : ' ';
}
- world.cells = cells;
- world.number_of_cells = height*width;
- return world;
+ return new_world;
+}
+
+void print(char* world) {
+ int number_of_cells = 9;
+ for (int i = 0; i < number_of_cells; i++) {
+ char cell = world[i];
+ if (i % 3 == 0) { printf("\n---\n"); }
+ printf("%c", cell);
+ }
+ printf("\n---\n");
}
gol.h
@@ -1,16 +1,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);
+int living_neighbours_for(char* world, int index);
+char* evolve(char* world);
+void print(char* world);
gol_test.c
@@ -11,40 +11,47 @@ static char* test_foo() {
}
static char* any_live_cell_with_fewer_than_two_live_neighbours_dies_as_if_caused_by_under_population() {
- printf("%s\n", __func__);
+ printf("TEST: %s\n", __func__);
char world[3][3] = {
{ 'x', ' ', ' ' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
};
- evolve(world);
- assert_equal(world[0][1] == ' ', "should die because has 1 living neighbours");
- assert_equal(world[2][2] == ' ', "should die because has 0 living neighbours");
+ char* new_world = evolve(*world);
+ printf("%s\n", new_world);
+ assert_equal(new_world[0] == ' ', "should die because has 1 living neighbours");
+ assert_equal(new_world[8] == ' ', "should die because has 0 living neighbours");
return 0;
}
static char* any_live_cell_with_two_live_neighbours_lives_on_to_the_next_generation() {
- printf("%s\n", __func__);
+ printf("TEST: %s\n", __func__);
char world[3][3] = {
{ 'x', ' ', 'x' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
};
- char* new_world = evolve(world);
- assert_equal(&new_world[0][1] == 'x', "should live because has 2 live neighbors");
+ char* new_world = evolve(*world);
+ assert_equal(new_world[1] == 'x', "should live because has 2 live neighbors");
return 0;
}
static char* it_returns_the_correct_number_of_living_neighbors() {
- printf("%s\n", __func__);
+ printf("TEST: %s\n", __func__);
char world[3][3] = {
{ 'x', ' ', 'x' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
};
- assert_equal(living_neighbours_for(world, 0, 0) == 0, "should return 0");
- assert_equal(living_neighbours_for(world, 1, 0) == 2, "should return 2");
- assert_equal(living_neighbours_for(world, 0, 2) == 0, "should return 0");
+ 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");
return 0;
}
@@ -64,7 +71,7 @@ static char* all_tests() {
int main(int argc, char **argv) {
char *result = all_tests();
if (result != 0) {
- printf("%s\n", result);
+ printf("FAILED: %s\n", result);
}
else {
printf("ALL TESTS PASSED\n");
main.c
@@ -1,12 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include "gol.h"
int main(int argc, char **argv) {
- World world = create_world(3, 3);
+ char world[3][3] = {
+ { ' ', ' ', ' ' },
+ { 'x', 'x', 'x' },
+ { ' ', ' ', ' ' },
+ };
system("clear");
- /*for (int i = 0; i < 5; i++) {*/
- /*evolve(world);*/
- print(world);
- /*}*/
+ for (int i = 0; i < 5; i++) {
+ evolve(*world);
+ print(*world);
+ sleep(1);
+ system("clear");
+ }
}