I just started working with C in my programing class. We have an assignment to create Conways game of life. I have very little knowledge of C from the class so far. I have no idea on how to implement the rules into code.
How do I compare the values of an array based on other values in the array? How would I change or keep the value then store it in a new array?
The professor has barley gone over arrays. The only thing we learned so far is declaring an array and how it is stored in memory.
Rules:
- If two or three of the four cells above, below, or to the left or right of an occupied cell are also occupied, the the cell remains occupied; otherwise, the organism in the cell dies from loneliness.
- If all four of an occupied cell's neighbors are occupied, the organism dies from overcrowding.
- An empty cell becomes occupied by an organism if exactly three of its adjacent cells are occupied.
This is what I have so far.
Code:
#include <stdio.h>
#include <stdlib.h>
char r,c;
int main (void) {
FILE *fp; // three lines to define the data file
const char *fn;
fn="testData5.dat";
//char lineIn[30];
fp=fopen(fn,"r"); // open file for input
char gameofLife[25] [25]; //array for game board original pattern
char gameofLife1[25] [25]; //array for current pattern
char gameofLife2[25] [25]; //array for revised pattern
int i,j,count,it;
count=fread(&gameofLife[0][0], sizeof(char), 25*25, fp); //Wrties file into gameofLife array
//with a size of 24*24 charatcters
char *h = &gameofLife1[0][0]; //Assign pointer h to gameofLife1 array
char *r = &gameofLife2[0][0]; //Assign pointer r to gameofLife2 array
char *g = &gameofLife[0][0]; //Assign pointer g to gameofLife array
if(*h!=*r){
printf("change = Yes\n");
}
else{
printf("change = No\n");
}
for(i=0;i<count;++i) //for loop displays data stored in array
printf("%c", *g++); //displayes gameofLife array
fclose(fp); // close file
return 0;
}