Had mixed up Y with X in some for loops, this caused the printing to be messed up on anything but square grids. Now grid generation and printing is complete, have to start implementing the actual life algorithm next. Also added .gitignore with cmake and c defaults
75 lines
1.9 KiB
C
75 lines
1.9 KiB
C
#include <locale.h>
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <wchar.h>
|
|
|
|
void printGrid(int gridX, int gridY, int** arr)
|
|
{
|
|
// print the array for debugging
|
|
wprintf(L"%lc", 0x2554UL); // prints ╔
|
|
for (int j = 0; j < gridX; j++) {
|
|
wprintf(L"%lc", 0x2550UL); // prints ═
|
|
}
|
|
wprintf(L"%lc", 0x2557UL); // prints ╗
|
|
wprintf(L"\n");
|
|
for (int j = 0; j < gridY; j++) {
|
|
wprintf(L"%lc", 0x2551UL); // prints ║
|
|
for (int i = 0; i < gridX; i++) {
|
|
wprintf(L"%lc", ((arr[i][j] == 1) ? 0x0023UL : 0x0020UL));
|
|
}
|
|
wprintf(L"%lc\n", 0x2551UL); // prints ║
|
|
}
|
|
wprintf(L"%lc", 0x255AUL); // prints ╚
|
|
for (int j = 0; j < gridX; j++) {
|
|
wprintf(L"%lc", 0x2550UL);
|
|
}
|
|
wprintf(L"%lc", 0x255DUL); // prints ╝
|
|
|
|
return;
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
if (argc < 3) {
|
|
printf("Usage: conway <xsize> <ysize> [seed]\n");
|
|
exit(255);
|
|
}
|
|
setlocale(LC_CTYPE, "");
|
|
int gridX = 0, gridY = 0, 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 < gridX; 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;
|
|
}
|