-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.c
168 lines (153 loc) · 3.81 KB
/
gameoflife.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
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
void fillBoard(int** board, int rows, int cols);
void updateBoard(int** board, int rows, int cols);
void printBoard(int** board, int rows, int cols);
void clearScreen();
// fills board with random 0's and 1's
void fillBoard(int** board, int rows, int cols)
{
srand((unsigned int) time(NULL));
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
board[row][col] = rand() % 2;
}
}
}
// evolves board 1 step
void updateBoard(int** board, int rows, int cols)
{
// make copy of board
int** oldBoard;
oldBoard = malloc(sizeof(*oldBoard) * rows);
for (int row = 0; row < rows; row++)
{
oldBoard[row] = malloc(sizeof(*oldBoard[row]) * cols);
}
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
oldBoard[row][col] = board[row][col];
}
}
// iterate thru board cells
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
// get previous/next row relative to current cell
int rowPrev = row - 1;
int rowNext = row + 1;
if (rowPrev < 0)
{
rowPrev = rows - 1;
}
if (rowNext >= rows)
{
rowNext = 0;
}
// get previous/next col relative to current cell
int colPrev = col - 1;
int colNext = col + 1;
if (colPrev < 0)
{
colPrev = cols - 1;
}
if (colNext >= cols)
{
colNext = 0;
}
// get neighbor count
int neighbors = 0;
neighbors += oldBoard[rowPrev][colPrev];
neighbors += oldBoard[rowPrev][col];
neighbors += oldBoard[rowPrev][colNext];
neighbors += oldBoard[row][colPrev];
neighbors += oldBoard[row][colNext];
neighbors += oldBoard[rowNext][colPrev];
neighbors += oldBoard[rowNext][col];
neighbors += oldBoard[rowNext][colNext];
// update current cell
if (oldBoard[row][col] == 0)
{
if (neighbors == 3)
{
board[row][col] = 1;
}
}
else if (oldBoard[row][col] == 1)
{
if (neighbors != 2)
{
board[row][col] = 0;
}
}
}
}
// free copy of board
for (int row = 0; row < rows; row++)
{
free(oldBoard[row]);
}
free(oldBoard);
}
void printBoard(int** board, int rows, int cols)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (board[row][col] == 1)
{
printf("%i ", board[row][col]);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
void clearScreen()
{
system("clear");
}
void game(int rows, int cols)
{
// declare, initialize, & fill board
int** board;
board = malloc(sizeof(*board) * rows);
for (int row = 0; row < rows; row++)
{
board[row] = malloc(sizeof(*board[row]) * cols);
}
fillBoard(board, rows, cols);
// main loop
clearScreen();
while (1)
{
clearScreen();
printBoard(board, rows, cols);
updateBoard(board, rows, cols);
usleep(100000);
}
// free board
for (int row = 0; row < rows; row++)
{
free(board[row]);
}
free(board);
}
int main(void)
{
int rows = 50;
int cols = 50;
game(rows, cols);
}