I'm working on a 1D Game of Life (based upon the rules set out here at Mathworld). Essentially, each generation is represented as a row of 0's or 1's (dead or alive) and the next generation is created based upon the binary representation of a "rule" command line argument.
For example, rule 30 turns into 00011110 (binary for 30) and this is used to determine which patterns of bits will spawn new cells or die off themselves in the subsequent generation.
In order to program this, I need to be able to access bits in groups of three (to apply a rule to) from the previous row. Below is a sample image (note that the starting row is always 0's with a central 1):
00000100000 #seed row
11001011001 #generated from seed row
...........
11110010101 #n-th row, generated from n-1 row
In order to generate a row, I must look at the bits from the row above in groups of three and then apply the rule as a 1/0, live/die decision.
Basically I plan to match the 3 bit pattern and the rule and use that to print either a 0 or a 1 for the offspring. this is the general algo:
if three_bit_pattern == 'xxx' && rule[x] == 0/1 {print 0/1} else {print 1/0}
The portion of the program where I am having difficulties is in accessing the contents of the previous row. All my attempts yield garbage or incorrect data.
In short, how would I access the previous row's values in groups of three bits?
The rows are created like this:
int i, j, k;
int row = atoi(argv[1]) + 1;
int col = 2 * atoi(argv[1]) + 1;
int arr[col];
int output[col];
char rule[9]; //binary representation of rule (2^8 stores up to 255 + null term)
int2binary(atoi(argv[2]), &rule, 10);
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
if(i == 0){
if(j == col / 2) //print 1 in center of first row
arr[i] = 1;
else
arr[i] = 0;
printf("%d", arr[i]);
}
else{
//output[i] = arr[i-1];
output[i+1] = arr[i];
output[i+2] = arr[i+1];
output[i+3] = arr[i+2];
printf("%s", output);
}
}//end inner for_loop
printf("\n");
}//end outer for_loop
}
Ok so I made this a whole lot simpler and am just going to have two arrays (one holding the previous column and one with the current). What I don't understand is why printing the output array produces garbage? Is output[i] = arr[i] not a valid expression?