-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConwaysGOL.cpp
More file actions
169 lines (129 loc) · 4.87 KB
/
Copy pathConwaysGOL.cpp
File metadata and controls
169 lines (129 loc) · 4.87 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <fstream>
// #include <cstring>
#include <string>
#include <algorithm>
#include <vector>
// TODO
// fix conventions.
using namespace std;
bool isspace(char c) {
if (c == ' ') return true;
return false;
}
void printBoard(vector<vector<int>> board) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
void printBoard(int** board) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
// int numNeighbors(vector<vector<int>> board, int i, int j) {
// int livingNeighbors = 0;
// if (board[i-1][j-1]) livingNeighbors++;
// if (board[i][j-1]) livingNeighbors++;
// if (board[i+1][j-1]) livingNeighbors++;
// if (board[i-1][j]) livingNeighbors++;
// if (board[i+1][j]) livingNeighbors++;
// if (board[i-1][j+1]) livingNeighbors++;
// if (board[i][j+1]) livingNeighbors++;
// if (board[i+1][j+1]) livingNeighbors++;
// return livingNeighbors;
// }
// Functionized finding neighbors and call it in loop. Creates a new board after each procedure call, or "tick"
void tick(vector<vector<int>> board) {
// Loop through each cell, determining how many alive neighbor's it has
// initialize to -1 so cell itself doesn't get counted as a living neighbor
// when finding num of living neighbors
// For when changing to M by N -> https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
// Also don't forget delete
int **newest= new int*[6];
for (int i = 0; i < 6; i++) {
newest[i] = new int[6];
}
int livingNeighbors = -1;
for (int i = 1; i < 6; i++) {
for (int j = 1; j < 6; j++) {
// livingNeighbors = -1;
livingNeighbors = 0;
// Search for living neighbors
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if( (i+k < 0) || // if row offset less than UPPER boundary
(i+k > board.size()-1) || // if row offset more than LOWER boundary
(j+l < 0) || // if column offset less than LEFT boundary
(j+l > board[i].size()-1)) // if column offset more than RIGHT boundary
continue;
livingNeighbors += board[i + k][j + l];
}
}
livingNeighbors -= board[i][j];
// livingNeighbors = numNeighbors(board, i, j);
// make this a switch...
// Cell is lonely and dies
if ((board[i][j] == 1) && (livingNeighbors < 2))
newest[i][j] = 0;
// Cell dies due to over population
else if ((board[i][j] == 1) && (livingNeighbors > 3))
newest[i][j] = 0;
// A new cell is born
else if ((board[i][j] == 0) && (livingNeighbors == 3))
newest[i][j] = 1;
// Remains the same
else
newest[i][j] = board[i][j];
// cout << "cell " << i << " " << j << " has " << livingNeighbors << " living neighbors\n";
}
}
cout << "post tick:\n";
printBoard(newest);
}
int main(int argc, char **argv) {
// read in table
if (argc != 2) {
return -1;
}
// Add a load of error checking
ifstream infile;
infile.open(argv[1]);
// Eventually initialize this depending on size of input board
vector<vector<int>> board { {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0} };
string line;
int i = 0;
int j = 0;
int k = 0;
int l = 0;
while (getline(infile, line)) {
// Nifty :)
line.erase(remove(line.begin(), line.end(),' '), line.end());
for (j = 0; j < line.size(); j++) {
// cout << "pushing back " << line[j] << " onto " << "vector " << i << endl;
// some ASCII fun
board[i][j] = line[j] - '0';
}
i++;
}
// Testing input parsing
// printBoard(board);
// Rules
// 1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
// 2. Any live cell with two or three live neighbours lives on to the next generation.
// 3. Any live cell with more than three live neighbours dies, as if by overpopulation.
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction
tick(board);
return 0;
}