/** ** Ansi C source code for Life starts here. ** Author Jawed Karim **/ #include #include #include #define ALIVE 1 #define DEAD 0 #define LR_SIZE 21 #define UD_SIZE 21 void fill_zero ( int [][LR_SIZE] ); void display_contents ( int [][LR_SIZE] ); void input_values ( int [][LR_SIZE] ); void check_nayburs ( int [][LR_SIZE] ); void copy_array ( int [][LR_SIZE], int [][LR_SIZE] ); int generation = 0; void main() { int life[UD_SIZE][LR_SIZE], kbinput; clrscr(); fill_zero ( life ); /* kill all cells */ display_contents ( life ); /* display contents of cells */ input_values ( life ); /* turn on/off cells */ while (1) { clrscr(); check_nayburs ( life ); display_contents ( life ); kbinput = getkey(); if (kbinput == K_Escape) exit(1); } } void copy_array ( int source[][LR_SIZE], int dest[][LR_SIZE] ) { int a, b; for (a = 0; a < UD_SIZE; a++) { for (b = 0; b < LR_SIZE; b++) { dest[a][b] = source[a][b]; } } } void check_nayburs ( int temp[][LR_SIZE] ) { int a, b, nayburs, original[UD_SIZE][LR_SIZE]; ++generation; copy_array ( temp, original ); for (a = 0; a < UD_SIZE; a++) { for (b = 0; b < LR_SIZE; b++) { /* ====== Counting of neighbors ====== */ nayburs = 0; if (b != LR_SIZE) { if (original[a][b+1] == ALIVE) /* check right */ nayburs++; } if (b != 0) { if (original[a][b-1] == ALIVE) /* check left */ nayburs++; } if (a != UD_SIZE) { if (original[a+1][b] == ALIVE) /* check down */ nayburs++; } if (a != 0) { if (original[a-1][b] == ALIVE) /* check up */ nayburs++; } if ( (a != UD_SIZE) && (b != LR_SIZE) ) { if (original[a+1][b+1] == ALIVE) /* check down-right */ nayburs++; } if ( (a != UD_SIZE) && (b != 0) ) { if (original[a+1][b-1] == ALIVE) /* check down-left */ nayburs++; } if ( (a != 0) && (b != LR_SIZE) ) { if (original[a-1][b+1] == ALIVE) /* check up-right */ nayburs++; } if ( (a != 0) && (b != 0) ) { if (original[a-1][b-1] == ALIVE) /* check up-left */ nayburs++; } /* ====== Life / Death Decision making ====== */ if ( (original[a][b] == DEAD) && (nayburs == 3) ) { temp[a][b] = ALIVE; } if ( (nayburs > 3) || (nayburs < 2) ) { temp[a][b] = DEAD; } nayburs = 0; } } } void fill_zero ( int temp[][LR_SIZE] ) { int a, b; for (a = 0; a < UD_SIZE; a++) { for (b = 0; b < LR_SIZE; b++) { temp[a][b] = DEAD; } } } void display_contents ( int temp[][LR_SIZE] ) { int a, b; for (a = 0; a < UD_SIZE; a++) { for (b = 0; b < LR_SIZE; b++) { if ( temp[a][b] == DEAD ) printf("_ "); if ( temp[a][b] == ALIVE) printf("X "); } if ( (a == 3) && (b == LR_SIZE) ) { if (generation <= 0) printf("\tGeneration: initial"); else printf("\tGeneration: %d", generation); } if ( (a == floor (UD_SIZE / 2)) && (b == LR_SIZE) ) { printf("\t\tGOF v1.0"); } if ( (a == floor (UD_SIZE / 2) + 1) && (b == LR_SIZE) ) { printf("\t(C) 1996 by Jawed Karim"); } if ( (a == floor (UD_SIZE / 2) + 6) && (b == LR_SIZE) ) { if (generation >= 1) printf("\tPress [ESC] to quit"); } printf("\n"); } } void input_values ( int temp[][LR_SIZE] ) { int a, b, value; printf("\nEnter COORDINATES and VALUE: (value != 1 && != 0 to start): "); scanf("%d %d %d", &a, &b, &value); if ( (value != 1) && (value != 0) ) return; if ( ((a <= UD_SIZE) && (b <= LR_SIZE)) && ((value == DEAD) || (value == ALIVE)) ) { temp[a][b] = value; clrscr(); display_contents ( temp ); input_values ( temp ); } }