Initial commit of conway's game of life
This commit is contained in:
commit
9c67c06b7d
1 changed files with 73 additions and 0 deletions
73
conway.c
Normal file
73
conway.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <wchar.h>
|
||||
#include <locale.h>
|
||||
|
||||
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 <xsize> <ysize> [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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue