-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgl.c
115 lines (100 loc) · 3.44 KB
/
cgl.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
/// File: cgl.c
/// Description: Backend implementation for Conway's "Game of Life" (cgl). Does
/// not provide a means of displaying cgl, this must be done through other means.
/// Author: Benjamin Piro, [email protected]
/// Date: 12/10/2020
#include <ncurses.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cgl.h"
static bool USE_WRAP = false;
void toggle_UW() {
USE_WRAP = !USE_WRAP;
}
void init_cgl(int rows, int cols, char start[rows][cols], Cell grid[rows][cols]) {
int i, j;
for (j = 0; j < rows; j++) {
for (i = 0; i < cols; i++) {
if (start[j][i] != 0) {
grid[j][i].live_state = 1;
} else {
grid[j][i].live_state = 0;
}
}
}
}
void grid_cpy(int rows, int cols, Cell grid[rows][cols], char grid_cpy[rows][cols]) {
int i, j;
for (j = 0; j < rows; j++) {
for (i = 0; i < cols; i++) {
grid_cpy[j][i] = grid[j][i].live_state;
}
}
}
static int mod(int m, int n) {
int result = m % n;
if (result < 0) result += n;
return result;
}
char nowrap_gln(int row, int col, int rows, int cols, char live_states[rows][cols]) {
char sum = 0;
// top left
if (row > 0 && col > 0) { if (live_states[row - 1][col - 1]) sum++; }
// top right
if (row > 0 && col < cols - 1) { if (live_states[row - 1][col + 1]) sum++; }
// bottom left
if (row < rows - 1 && col > 0) { if (live_states[row + 1][col - 1]) sum++; }
// bottom right
if (row < rows - 1 && col < cols - 1) { if (live_states[row + 1][col + 1]) sum++; }
// top
if (row > 0) { if (live_states[row - 1][col]) sum++; }
// left
if (col > 0) { if (live_states[row][col - 1]) sum++; }
// right
if (col < cols - 1) { if (live_states[row][col + 1]) sum++; }
// bottom
if (row < rows - 1) { if (live_states[row + 1][col]) sum++; }
return sum;
}
char wrap_gln(int row, int col, int rows, int cols, char live_states[rows][cols]) {
char sum = 0;
// top left
if (live_states[mod(row - 1, rows)][mod(cols - 1, cols)]) sum++;
// top right
if (live_states[mod(row - 1, rows)][mod(cols + 1, cols)]) sum++;
// bottom left
if (live_states[mod(row + 1, rows)][mod(cols - 1, cols)]) sum++;
// bottom right
if (live_states[mod(row + 1, rows)][mod(cols + 1, cols)]) sum++;
// top
if (live_states[mod(row - 1, rows)][col]) sum++;
// left
if (live_states[row][mod(col - 1, cols)]) sum++;
// right
if (live_states[row][mod(col + 1, cols)]) sum++;
// bottom
if (live_states[mod(row + 1, rows)][col]) sum++;
return sum;
}
char get_live_neighbors(int row, int col, int rows, int cols, char live_states[rows][cols]) {
if (USE_WRAP) return nowrap_gln(row, col, rows, cols, live_states);
else return wrap_gln(row, col, rows, cols, live_states);
}
void get_next_gen(int rows, int cols, Cell grid[rows][cols]) {
char live_states[rows][cols];
grid_cpy(rows, cols, grid, live_states);
}
/*
unsigned int getLiveNeighbors(Cell grid[HEIGHT][WIDTH], CellPtr_t cell, int row, int col) {
unsigned int sum = 0;
if (row > 0 && col > 0) { if (grid[row - 1][col - 1].live_state) sum++; }
if (row > 0 && col < WIDTH - 1) { if (grid[row - 1][col + 1].live_state) sum++; }
if (row < HEIGHT - 1 && col > 0) { if (grid[row + 1][col - 1].live_state) sum++; }
if (row < HEIGHT - 1 && col < WIDTH - 1) { if (grid[row + 1][col + 1].live_state) sum++; }
if (row > 0) { if (grid[row - 1][col].live_state) sum++; }
if (row < HEIGHT - 1) { if (grid[row + 1][col].live_state) sum++; }
if (col > 0) { if (grid[row][col - 1].live_state) sum++; }
if (col < WIDTH - 1) { if (grid[row][col + 1].live_state) sum++; }
return sum;
}
*/