-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo.c
More file actions
154 lines (134 loc) · 4.45 KB
/
Copy pathtwo.c
File metadata and controls
154 lines (134 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#include <string.h>
typedef unsigned char Cell;
static Cell *grid = NULL;
static Cell *next = NULL;
static Cell *ext = NULL;
static int ROWS = 0;
static int COLS = 0;
static HANDLE hOut;
int gridAlloc(int rows, int cols) {
ROWS = rows;
COLS = cols;
size_t coreSize = (size_t)ROWS * COLS * sizeof(Cell);
size_t extSize = (size_t)(ROWS + 2) * (COLS + 2) * sizeof(Cell);
grid = realloc(grid, coreSize);
next = realloc(next, coreSize);
ext = realloc(ext, extSize);
if (!grid || !next || !ext) {
fprintf(stderr, "Failed to allocate memory.\n");
return 0;
}
return 1;
}
void initRandom() {
srand((unsigned)time(NULL));
for (int i = 0; i < ROWS * COLS; i++) {
grid[i] = rand() & 1;
}
}
void updatePadGrid() {
int extCols = COLS + 2;
for (int r = 0; r < ROWS; r++) {
memcpy(&ext[(r + 1) * extCols + 1], &grid[r * COLS], COLS * sizeof(Cell));
}
memcpy(&ext[0 * extCols + 1], &grid[(ROWS - 1) * COLS], COLS * sizeof(Cell));
memcpy(&ext[(ROWS + 1) * extCols + 1], &grid[0], COLS * sizeof(Cell));
for (int r = 0; r < ROWS; r++) {
ext[(r + 1) * extCols + 0] = grid[r * COLS + (COLS - 1)];
ext[(r + 1) * extCols + (COLS + 1)] = grid[r * COLS + 0];
}
ext[0 * extCols + 0] = grid[(ROWS - 1) * COLS + (COLS - 1)];
ext[0 * extCols + (COLS + 1)] = grid[(ROWS - 1) * COLS + 0];
ext[(ROWS + 1) * extCols + 0] = grid[0 * COLS + (COLS - 1)];
ext[(ROWS + 1) * extCols + (COLS + 1)] = grid[0];
}
void step() {
int extCols = COLS + 2;
updatePadGrid();
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
Cell *p = &ext[(r + 1) * extCols + (c + 1)];
int n = p[-extCols - 1] + p[-extCols] + p[-extCols + 1]
+ p[-1] + p[1]
+ p[extCols - 1] + p[extCols] + p[extCols + 1];
int idx = r * COLS + c;
next[idx] = grid[idx] ? (n == 2 || n == 3) : (n == 3);
}
}
memcpy(grid, next, (size_t)ROWS * COLS * sizeof(Cell));
}
void draw() {
// Reset cursor position to top-left
COORD coord = {0, 0};
SetConsoleCursorPosition(hOut, coord);
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
putchar(grid[r * COLS + c] ? 'O' : ' ');
}
putchar('\n');
}
printf("Controls: [space] Play/Pause [+]/[-] Speed [q] Quit\n");
fflush(stdout);
}
int main(int argc, char *argv[]) {
int default_rows = 40;
int default_cols = 80;
int rows = default_rows;
int cols = default_cols;
if (argc == 3) {
rows = atoi(argv[1]);
cols = atoi(argv[2]);
if (rows <= 0 || cols <= 0) {
fprintf(stderr, "Invalid grid size. Using default %dx%d.\n", default_rows, default_cols);
rows = default_rows;
cols = default_cols;
}
} else {
printf("Usage: %s [rows cols]\nUsing default size %dx%d.\n", argv[0], default_rows, default_cols);
}
if (!gridAlloc(rows, cols)) return EXIT_FAILURE;
// Setup console for ANSI and cursor control
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
// Hide cursor
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hOut, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cursorInfo);
initRandom();
int running = 1;
// High-resolution timer for 30 FPS
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
while (1) {
QueryPerformanceCounter(&start);
draw();
if (_kbhit()) {
int c = _getch();
if (c == 'q' || c == 'Q') break;
if (c == ' ') running = !running;
}
if (running) step();
QueryPerformanceCounter(&end);
double elapsed = (double)(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
// Target ~33.33ms per frame for 30 FPS
if (elapsed < 16.7) {
Sleep((DWORD)(16.7 - elapsed));
}
}
// Restore cursor
GetConsoleCursorInfo(hOut, &cursorInfo);
cursorInfo.bVisible = TRUE;
SetConsoleCursorInfo(hOut, &cursorInfo);
free(grid);
free(next);
free(ext);
return EXIT_SUCCESS;
}