There are a few things you need to look at in your code. The best thing you could do is enable compiler warnings, if you are using gcc
add the -Wall
(warn all) flag to the compile line, so to compile use:
gcc -Wall source.c -o Program
Most compilers support some sort of warning flag/option. When I compiled your code with this flag I got the following output (trimmed a bit):
7:5: warning: missing braces around initializer [-Wmissing-braces]
signed char cell_to_check [1][1] = {0};
31:13: warning: statement with no effect [-Wunused-value]
else(board[(i+(x_inc[x]))][(a+(y_inc[y]))] == 0);{
44:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (live_n_cells = 0){
12:17: warning: implicit declaration of function ‘evaluate_cell’ [-Wimplicit-function-declaration]
evaluate_cell(i,a);
When I went through and fixed these issues, the compiler provided more issues, but eventually you should have a nice warning free program (suggesting that your code might be correct).
The first warning above is because you are trying to assign an array with one element to a variable which expects an array of and array with one element.
The second warning above is because you are missing an if
after your else, so this should be else if (...)
, however something else you should note is that you have a ;
before the curly brackets, this probably shouldn't be there at all, you should go through your code an make sure you don't have extra ;
s before curlly brackets.
The third warning is probably because you are making an assignment live_n_cells = 0
instead of checking live_n_cells == 0
. If you actually meant to make the assignment, then write (live_n_cells = 0)
. A good way to avoid every accidently making assignments is to use 0 == live_n_cells
, this way if you forget the second equals sign you never make an assignment.
The fourth warning is about a missing declaration of your function. to resolve this, you can add something like int evaluate_cell(int i,int a);
to the top of your source file.
Try these things and then post an update to your question (these tips may even help to solve your problem).
EDIT:
A few more tips:
Functions can be declared as void
, which means that they don't return a value. This could be useful in your program since evaluate_cell
currently returns 0, which you simply ignore. You could instead write void evaluate_cell(int i, int j)
, but remember to change the declaration at the top of your source file too.
I went over your reproduction code, it doesn't seem to contain all the Game of Life operations (at least according to the Wikipedia page http://en.wikipedia.org/wiki/Conway's_Game_of_Life), try the following:
if (live_n_cells == 3 && board[i][a] == 0) { // production
temp_board[i][a] = 1;
} else if (live_n_cells > 3) { // over population
temp_board[i][a] = 0;
} else if (live_n_cells < 2) { // under population
temp_board[i][a] = 0;
} else { // reproduction
temp_board[i][a] = board[i][a];
}
And also call your evaluate cell routine for all cells, not just the currently alive cells. You also probably notice the use of the variable temp_board
in the above code. This is related to the comment posted by aschepler on your question. I also changed the definition of the board to the following, the +2
is padding to make sure you never try to access elements outside the array.
#define WIDTH 10
#define HEIGHT 10
signed char board [HEIGHT+2][WIDTH+2]; ///< the board
signed char temp_board [HEIGHT+2][WIDTH+2]; ///< temporary board
Finally, I changed the code in evaluateboard
to:
int i, j; ///< loop counters
// Evaluate each cell in array
for (j=1; j<=HEIGHT; j++) {
for (i=1; i<=WIDTH; i++) {
evaluate_cell(j,i);
}
}
// copy temp_board into board
for (j=1; j<=HEIGHT; ++j) {
for (i=1; i<=WIDTH; ++i) {
board[j][i] = temp_board[j][i];
}
}
Using most of your code, with the above few tweaks, I managed to get Conway's Game of Life running with the expected results.
One final tip, it is usually the convention in C/C++ to use i, j, k, l, m, n
(often in that order) as your loop counting variables.
Good luck. :)