/ Published in: C
URL: http://www.autofasurer.net/wp
First iteration of an ASCII game of life, running in the terminal (on OS X, don't know what it does on other systems)
Expand |
Embed | Plain Text
#include <stdio.h> //standard input/output #include <stdlib.h> // for the rand() and srand() function #include <time.h> // for seeding the rand() function with time() #include <unistd.h> //for the sleep function #include <math.h> // for the square root function int i, j, k, telOp; int arraySize, iterations; int square[50][50]; int squareTemp[50][50]; void initRand() { srand((unsigned)(time(0))); } void createMap(){ for (i = 0; i < arraySize; i++) { for (j = 0; j < arraySize; j++) { square[i][j] = rand()%2; squareTemp[i][j] = 0; //**print coordinates**// //printf("%i, %i\n", i, j); } } } void checkMap(){ int iMin,iPlus,jMin,jPlus; for (i = 0; i < arraySize; i++) { for (j = 0; j < arraySize; j++) { if (i == 0){ iMin = arraySize; } else {iMin = i-1;} if (i == arraySize){ iPlus = 0;} else {iPlus = i+1;} if (j == 0){ jMin = arraySize; } else {jMin = j-1;} if (j == arraySize){ jPlus = 0;} else {jPlus = j+1;} telOp = square[iMin][jMin]+ square[i][jMin]+ square[iPlus][jMin]+ square[iMin][j]+ square[iPlus][j]+ square[iMin][jPlus]+ square[i][jPlus]+ square[iPlus][jPlus]; if (telOp < 2){ squareTemp[i][j] = 0; } else if (telOp > 3){ squareTemp[i][j] = 0; } else if (telOp == 3){ squareTemp[i][j] = 1; } else if (telOp == 2){ squareTemp[i][j] = square[i][j]; } //printf("telop = %i\n", telOp); } } for (i = 0; i < arraySize; i++) { for (j = 0; j < arraySize; j++) { square[i][j] = squareTemp[i][j]; } } } void printMap(){ system("clear"); for (i = 0; i < arraySize; i++) { for (j = 0; j < arraySize; j++) { if (square[i][j] == 0) { else { } } } } int main (int argc, const char * argv[]) { // insert code here... //Initialize the map scanf("%i", &arraySize); scanf("%i", &iterations); if (arraySize <= 50 && arraySize > 0) { initRand(); createMap(); for (k = 0; k < iterations; k++) { checkMap(); printMap(); usleep(20000); } } //printf("%i",sizeof(square) / sizeof(int)); return 0; }
You need to login to post a comment.
