commit 340a2d4d7fba4f23b3c551aef8d47feb536fd4e6
parent 500323d480e42c227671f46b109dc01290fe5da3
Author: Anders Damsgaard <anders.damsgaard@geo.au.dk>
Date: Sat, 8 Mar 2014 16:41:43 +0100
It's working
Diffstat:
5 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
CFLAGS=-Wall -pedantic -g -O2
-#LDLIBS=-lm
+#LDLIBS=-lmgl
BIN=gameoflife
diff --git a/main.c b/main.c
@@ -5,13 +5,13 @@
#include "utility.h"
#include "rules.h"
-#define VERSION 0.1
+#define GOLVERSION 0.1
int main(int argc, char **argv)
{
int **cells;
int **neighbors;
- int nx = 10;
+ int nx = 40;
int ny = 10;
int c;
unsigned int it = 0;
@@ -30,7 +30,7 @@ int main(int argc, char **argv)
case 'v':
printf("%s: Conway's Game of Life, version %.1f\n"
"Written by Anders Damsgaard, "
- "https://github.com/anders-dc/game-of-life\n", argv[0], VERSION);
+ "https://github.com/anders-dc/game-of-life\n", argv[0], GOLVERSION);
return 0;
break;
case '?':
@@ -56,9 +56,8 @@ int main(int argc, char **argv)
random_population(cells, nx, ny, 0.5);
- print_matrix("cells", cells, nx, ny);
+ print_cell_matrix("cells", cells, nx, ny);
- /*while ((c = getchar()) != 'q') {*/
while (world_is_dead == 0) {
world_is_dead = find_neighbor_count(cells, neighbors, nx, ny);
@@ -66,7 +65,7 @@ int main(int argc, char **argv)
printf("it = %d\n", it);
print_matrix("neighbors", neighbors, nx, ny);
- print_matrix("cells", cells, nx, ny);
+ print_cell_matrix("cells", cells, nx, ny);
sleep(1);
diff --git a/rules.c b/rules.c
@@ -25,13 +25,17 @@ int find_neighbor_count(int **cells, int **neighbors, int nx, int ny)
int i, j, x, y;
int nneighbors;
- for (i=1; i<nx-1; i++) {
- for (j=1; j<ny-1; j++) {
+ /*for (i=1; i<nx-1; i++) {
+ for (j=1; j<ny-1; j++) {*/
+ for (i=0; i<nx; i++) {
+ for (j=0; j<ny; j++) {
nneighbors = 0;
for (x=-1; x<2; x++) {
for (y=-1; y<2; y++) {
- if (x != 0 && y != 0) {
+ if (x != 0 && y != 0 &&
+ i+x > 0 && i+x < nx &&
+ j+y > 0 && j+y < ny) {
nneighbors += cells[i+x][j+y];
}
}
@@ -51,8 +55,8 @@ int find_neighbor_count(int **cells, int **neighbors, int nx, int ny)
void cell_transitions(int **cells, int **neighbors, int nx, int ny)
{
int i, j, nneighbors;
- for (i=1; i<nx-1; i++) {
- for (j=1; j<ny-1; j++) {
+ for (i=0; i<nx; i++) {
+ for (j=0; j<ny; j++) {
nneighbors = neighbors[i][j];
if (cells[i][j] == 1) { /* alive */
diff --git a/utility.c b/utility.c
@@ -35,3 +35,18 @@ void print_matrix(char* description, int **M, int nx, int ny)
printf("\n");
}
}
+
+void print_cell_matrix(char* description, int **M, int nx, int ny)
+{
+ int i, j;
+ printf("%s:\n", description);
+ for (j=0; j<ny; j++) {
+ for (i=0; i<nx; i++) {
+ if (M[i][j] == 1)
+ printf("X");
+ else
+ printf(".");
+ }
+ printf("\n");
+ }
+}
diff --git a/utility.h b/utility.h
@@ -4,5 +4,6 @@
int allocate_matrix(int ***M, int nx, int ny);
void free_matrix(int ***M, int nx);
void print_matrix(char* description, int **M, int nx, int ny);
+void print_cell_matrix(char* description, int **M, int nx, int ny);
#endif