I made first board, but when I use function next_generation, it prints wrong result. For example, since first board is
01000
00100
11100
00000
00000,
next one should be
00000
10100
01100
01000
00000 .
But my program prints
00000
00100
01100
00000
-
Here is my code.
include <stdio.h>#define WIDTH 10 #define HEIGHT 10 int lifegame[WIDTH][HEIGHT]; int x; int y; void initialize(){ for (x=1;x<WIDTH;x++) for (y=1;y<HEIGHT;y++){ lifegame[x][y]=0; } } void next_generation() { for(x=1;x<WIDTH;x++) { for(y=1;y<HEIGHT;y++) {int count=0; if(lifegame[x-1][y-1]==1){ count++;} if(lifegame[x-1][y]==1){ count++;} if(lifegame[x-1][y+1]==1 ){ count++;} if(lifegame[x][y-1]==1 ){ count++;} if(lifegame[x][y+1]==1){ count++;} if(lifegame[x+1][y-1]==1){ count++;} if(lifegame[x+1][y]==1){ count++;} if(lifegame[x+1][y+1]==1){ count++;} if(lifegame[x][y]==0 && count==3){ lifegame[x][y]=1; } if(lifegame[x][y]==1 && (count<2 || count>3)){ lifegame[x][y]=0; } if(lifegame[x][y]==1 && (count==2 || count ==3)){ lifegame[x][y]=1; } } } } void start(){ initialize(); for(x=0;x<WIDTH;x++){ lifegame[x][0]=8; } lifegame[1][2]=1; lifegame[2][3]=1; lifegame[3][1]=1; lifegame[3][2]=1; lifegame[3][3]=1; printf("\n"); } void express(){ for(x=1; x<WIDTH; x++) { for(y=0; y<HEIGHT; y++) { if(lifegame[x][y]==0) printf(" |"); if(lifegame[x][y]==1) printf("*|"); if(lifegame[x][y]==8) printf("|"); } printf("\n"); } } int main(){ start(); next_generation(); express(); }