-
Notifications
You must be signed in to change notification settings - Fork 2
/
life.c
374 lines (325 loc) · 9.76 KB
/
life.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
Copyright (c) 2009 Mats Rauhala <mats.rauhala@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* XXX: Toggle the cells */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <locale.h>
#include <time.h>
#include "macros.h"
/* Array to hold the cells */
static int *cells = NULL;
/* Array to hold the cells of the next round */
static int *buffer = NULL;
/* Size of the 'game area' */
static int lifecols = 0;
static int lifelines = 0;
/* Default tick size. How many iterations are made in a tick */
static int ticksize = 1;
/* Information window */
static WINDOW *info = NULL;
/* Game area */
static WINDOW *life = NULL;
/* Holds the sleep time */
static struct timespec sleeptime = {0};
/* Prints the game area, but is not used since the curses interface. Obsolete
* */
void print();
/* Prints status information about the game. Window size, game size, tick size
* etc. Populates the info-window */
void status();
/* Initialize the game */
void init();
/* Initialize curses */
void initcurses();
/* Clean up the mess */
void finish();
/* Go forward a tick (ticksize iterations) */
void tick();
/* Activate a cell and the visual representation of it */
void activate(int y, int x);
/* A helper function for creating new windows */
WINDOW *createwin(int height, int width, int begy, int begx);
/* Keyboard utilities */
bool kbd(int ch);
/* Calculate the live neighbours for a cell */
int neighbours(int y, int x);
int main()
{
int ch; /* Holds the keyboard char */
init();
do
{ /* Keyboard loop */
ch = getch();
}
while(kbd(ch)); /* When the kbd returns false, we stop */
finish();
return 0;
}
bool kbd(int ch)
{
int x,y; /* Holds the coordinates */
getyx(life, y, x); /* Get the coordinates from curses life window */
switch(ch)
{ /* hjkl + q + space + enter */
case 'q':
return false;
break;
case 'l': /* Go right */
x=(x+1)%CMAX;
break;
case 'h': /* Go left */
x=(x-1)%CMAX;
break;
case 'j': /* Go down */
y=(y+1)%LMAX;
break;
case 'k': /* Go up */
y=(y-1)%LMAX;
break;
case ' ': /* Activate a cell */
activate(y, x);
break;
case 10: /* Enter. Start the tick */
tick();
break;
case KEY_UP: /* Increase tick size */
ticksize++;
status();
break;
case KEY_DOWN: /* Reduce tick size */
ticksize--;
status();
break;
}
wmove(life, y, x); /* Move the coordinates to the new location and refresh
the game area */
wrefresh(life);
return true;
}
void tick()
{
struct timespec *remaining; /* Holds the remaining time. Not used */
/* t = current tick iteration
* x = x coordinate
* y = y coordinate
* n = neighbours for a coordinate
*/
int t, x, y, n;
/* Sync the buffer with the game area
* buffer <- cells.
* The buffer and cells are separated so that the calculations from this
* round won't affect this round. For example let's imagine that coordinate
* (y,x) (1,1) is active but is deemed dead after visiting it. Then we
* check the coordinate (y,x) (2,1) which now has only 1 neighbour and also
* dies. By separating the cells and buffer we keep the cells untouchable
* during the round, but after we have finished the round, we can sync
* them. */
COPYC;
for(t = 0; t < ticksize; t++)
{ /* Iterations */
for(x = 0; x < lifecols; x++)
{
for(y = 0; y < lifelines; y++)
{
/* Get the neighbours for the coordinate */
n = neighbours(y, x);
/* If a cell has less than two neighbours it dies, no matter
* what. If a cell has more than three neighbours it's
* overcrowded and dies too. */
if(n < 2 || n > 3)
{
ABCPR(y,x);
mvwaddch(life, y, x, ' ');
}
/* If a cell has 3 neighbours it revives */
if(n == 3)
{
BCPR(y, x);
mvwaddch(life, y, x, 'X');
}
/* The only possibility left is that the cell has two
* neighbours, which means that the cell stays alive if it's
* alive, if it's dead it stays dead. */
}
}
/* Sync the cells with the buffer */
COPYB;
/* Refresh the screen */
wrefresh(life);
/* Sleep for while */
nanosleep(&sleeptime, remaining);
}
}
int neighbours(int y, int x)
{
int n = 0; /* Alive neighbours */
/* The curses coordinate system puts y=0 to the top, so y-1 means checking
* for above and y+1 means the bottom */
/* Check for boundaries and then sum up the alive cells */
if(y < LMAX)
{
n += ALIVE(y+1,x); /* Bottom */
}
if(y > 0)
{
n += ALIVE(y-1,x); /* Top */
}
if(x < CMAX)
{
n += ALIVE(y, x+1); /* Right */
if(y > 0)
{
n += ALIVE(y-1, x+1); /* Top right */
}
if(y < LMAX)
{
n += ALIVE(y+1, x+1); /* Bottom right */
}
}
if(x > 0)
{
n += ALIVE(y, x-1); /* Left */
if(y > 0)
{
n += ALIVE(y-1, x-1); /* Top left */
}
if(y < LMAX)
{
n += ALIVE(y+1, x-1); /* Bottom left */
}
}
return n;
}
void finish()
{
/* Free the cells and buffer */
free(cells);
free(buffer);
/* Delete the two windows we made */
delwin(info);
delwin(life);
/* And end curses mode. It's bad if this is forgotten. Vey very bad */
endwin();
}
WINDOW *createwin(int height, int width, int begy, int begx)
{
/* Hold the window */
WINDOW *local_win = NULL;
/* Create the window */
local_win = newwin(height, width, begy, begx);
if(local_win == NULL) /* Check that it succeeded */
exit(EXIT_FAILURE);
wmove(local_win, 1, 1); /* Looks better if the cursor is not on
top of lines */
wrefresh(local_win); /* And refresh */
return local_win;
}
void initcurses()
{
/* Initialize curses */
initscr();
/* Don't echo keys */
noecho();
raw();
/* Allow arrows and other weird keys */
keypad(stdscr, true);
/* Refresh the standard screen */
refresh();
lifecols = COLS-ICOLS-1; /* Leave one column empty */
lifelines = LINES; /* Full height */
/* Create the two windows */
life = createwin(lifelines, lifecols, 0, 0);
info = createwin(ILINES, ICOLS, 0, lifecols+1);
/* Make a default box around the info-window */
box(info, 0, 0);
/* Move, window, printw; Write to window info and move the cursor to 1,1
* before writing. */
mvwprintw(info, 1,1, "INFO");
/* Move the cursor back to game area. The user don't need to see the cursor
* jumping to the info-window */
wmove(life, 1,1);
/* Refresh both of the windows */
wrefresh(info);
wrefresh(life);
}
void init()
{
/* Set the sleep time */
sleeptime.tv_nsec = SLEEP;
/* Initialize the curses. Curses initialises few variables that we need
* further on */
initcurses();
/* Locale */
setlocale(LC_ALL, "");
/* Initialize arrays. SIZE is dependent on the life-window */
cells = malloc(SIZE);
if(cells == NULL)
exit(1);
memset(cells, 0, SIZE); /* Fill it with 0 */
/* The buffer is identical to cells */
buffer = malloc(SIZE);
if(buffer == NULL)
exit(1);
memset(buffer, 0, SIZE);
/* Write the first status information */
status();
}
void print()
{
int x, y;
printf("\n");
for(x = 0; x < COLS+2; x++)
printf("-");
printf("\n");
for(y = 0; y < LINES; y++)
{
printf("|");
for(x = 0; x < COLS; x++)
printf("%d", CELL(y, x));
printf("|\n");
}
for(x = 0; x < COLS+2; x++)
printf("-");
printf("\n");
}
void status()
{
int y,x;
mvwprintw(info, 2, 1, "Total sizes");
mvwprintw(info, 3, 1, " Columns: %d", COLS);
mvwprintw(info, 4, 1, " Lines: %d", LINES);
mvwprintw(info, 5, 1, "Life sizes");
mvwprintw(info, 6, 1, " Columns: %d", lifecols);
mvwprintw(info, 7, 1, " Lines: %d", lifelines);
mvwprintw(info, 8, 1, "Array size: %d", ASIZE);
mvwprintw(info, 9, 1, "Tick size: %d", ticksize);
getyx(life, y, x);
wmove(life, y, x);
wrefresh(info);
wrefresh(life);
}
void activate(int y, int x)
{
/* Set the cell alive in the array */
CPR(y,x);
/* And show it visually too */
mvwaddch(life, y, x, 'X');
}