/**************************************** * THE GAME OF LIFE * * Copyright 2006 Sergiusz Bazanski * * Liscenced under the GNU GPL * *****************************************/ #include int calculate(int occupied, int neighbours); void printtable(); void printn(); void calculaten(); void nextstep(); int table [10][10]; int neighbourhood[10][10]; int main() { //Initialise tables int x = 0; int y = 0; for (x = 0; x < 10; x++){ for (y = 0; y < 10; y++) { table[x][y] = 0; } } x = 0; y = 0; for (x = 0; x < 10; x++){ for (y = 0; y < 10; y++) { neighbourhood[x][y] = 0; } } //Sample values, insert fancy GUI here table[1][7] = 1; table[2][7] = 1; table[3][7] = 1; table[3][8] = 1; table[2][9] = 1; printf("------------\n"); int i = 0; while (1){ printtable(); calculaten(); printf("------------\n"); nextstep(); } return 0; } int calculate(int occupied, int neighbours) { if (occupied == 1){ if (neighbours <= 1 || neighbours >= 4) return 0; if (neighbours == 2 || neighbours == 3) return 1; } else{ if (neighbours == 3) return 1; else return 0; } } void printtable() { int x = 0; int y = 0; for (x = 0; x < 10; x++){ printf("|"); for (y = 0; y < 10; y++){ if (table[y][x] == 1) printf("8"); else printf(" "); } printf("|\n"); } } void printn() { int x = 0; int y = 0; for (x = 0; x < 10; x++){ for (y = 0; y < 10; y++){ printf("%d", neighbourhood[y][x]); } printf("\n"); } } void calculaten(){ int x = 0; int y = 0; int i = 0; for(x = 0; x < 10; x++){ for(y = 0; y < 10; y++){ int i = 0; if (table[x-1][y-1] == 1) i++; if (table[x][y-1] == 1) i++; if (table[x+1][y-1] == 1) i++; if (table[x-1][y] == 1) i++; if (table[x+1][y] == 1) i++; if (table[x-1][y+1] == 1) i++; if (table[x][y+1] == 1) i++; if (table[x+1][y+1] == 1) i++; neighbourhood[x][y] = i; } } } void nextstep() { int x = 0; int y = 0; int occ = 0; int neigh = 0; int result = 0; for (x = 0; x < 10; x++){ for (y = 0; y < 10; y++) { occ = table[x][y]; neigh = neighbourhood[x][y]; result = calculate(occ, neigh); table[x][y] = result; } } }