commit 9c67c06b7d6ad0a3c0783db84d706c8b2286c0dd Author: A.M. Rowsell Date: Sun May 28 02:01:36 2023 -0400 Initial commit of conway's game of life diff --git a/conway.c b/conway.c new file mode 100644 index 0000000..c8468a6 --- /dev/null +++ b/conway.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include + +void printGrid(int gridX, int gridY, int **arr) { + // print the array for debugging + wprintf(L"%lc", 0x2554); + for(int j = 0; j < gridY; j++) { + wprintf(L"%lc", 0x2550); + } + wprintf(L"%lc", 0x2557); + wprintf(L"\n"); + for(int j = 0; j < gridY; j++) { + wprintf(L"%lc", 0x2551); + for(int i = 0; i < gridX; i++) { + wprintf(L"%lc", ((arr[i][j] == 1) ? 0x0023 : 0x0020)); + } + wprintf(L"%lc\n", 0x2551); + } + wprintf(L"%lc", 0x255A); + for(int j = 0; j < gridY; j++) { + wprintf(L"%lc", 0x2550); + } + wprintf(L"%lc", 0x255D); + + return; +} + +int main(int argc, char *argv[]) { + if(argc < 3) { + printf("Usage: conway [seed]\n"); + exit(255); + } + setlocale(LC_CTYPE, ""); + int gridX, gridY, seed = 0, generation = 0; + for(int i = argc - 1; i > 0; i--) { + switch(i) { + case 1: + gridX = atoi(argv[i]); + break; + case 2: + gridY = atoi(argv[i]); + break; + case 3: + seed = atoi(argv[i]); + break; + } + } + if(seed == 0) { + time_t t; + srand((unsigned) time(&t)); + } else { + srand(seed); + } + // dynamically allocate the 2D array, could be quite large! + int *arr[gridX]; + for(int i = 0; i < gridY; i++) { + arr[i] = (int *)malloc(gridY * sizeof(int)); + } + // seed the array + for(int i = 0; i < gridX; i++) { + for(int j = 0; j < gridY; j++) { + arr[i][j] = (rand() % 2 == 0) ? 1 : 0; + } + } + printGrid(gridX, gridY, arr); + + return 0; +}