-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.cpp
223 lines (152 loc) · 4.94 KB
/
life.cpp
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
// This is the CPP file you will edit and turn in.
// Also remove these comments here and add your own.
// TODO: remove this comment header!
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "lifegui.h"
using namespace std;
Grid<char> SetInitialDish (ifstream &in, Grid <char> &InitialDish);
void SetPattern(Grid <char> &InitialDish);
int main() {
ifstream in;
while (true){
string s = getLine("Grid input file name? ");
in.open(s);
if (in.is_open()){
break;
}
if (in.fail()){
cout << "Unable to open that file. Try again." << endl;
}
}
//Set up initial dish
Grid <char> InitialDish;
InitialDish = SetInitialDish(in, InitialDish);
//Print the grid in a prettier way
for (int RowOfInitialDish = 0; RowOfInitialDish < InitialDish.numRows(); RowOfInitialDish ++){
for (int ColOfInitialDish = 0; ColOfInitialDish < InitialDish.numCols(); ColOfInitialDish ++){
if (InitialDish[RowOfInitialDish][ColOfInitialDish] == 'X'){
cout << InitialDish[RowOfInitialDish][ColOfInitialDish];
} else {
cout << InitialDish[RowOfInitialDish][ColOfInitialDish];
}
}
cout << endl;
}
while (true){
string s2 = getLine("a)nimate, t)ick, q)uit? ");
if (s2 == "q"){
cout << "Have a nice Life!";
break;
} else if (s2 == "t"){
SetPattern(InitialDish);
} else if (s2 == "a") {
string frames;
while (true){
frames = getLine("How many frames? ");
if (stringIsInteger(frames)){
break;
}
cout << "Illegal integer format. Try again." << endl;
}
int Frames = stringToInteger(frames);
for (int count = 0; count <= Frames; count ++){
clearConsole();
SetPattern(InitialDish);
pause(50);
}
} else {
cout << "Invalid choice; please try again." << endl;
}
}
return 0;
}
Grid<char> SetInitialDish (ifstream &in, Grid <char> &InitialDish){
string temp;
int a;
int b;
getline(in, temp);
a = stringToInteger(temp);
getline(in, temp);
b = stringToInteger(temp);
InitialDish.resize(a, b);
int count = 0;
while(true){
string line;
getline(in, line);
for (int i = 0; i < line.length(); i++){
char ch;
ch = line[i];
InitialDish.set(count, i, ch);
}
count++;
if (in.fail()){
break;
}
if (count >= InitialDish.numRows()){
break;
}
}
in.clear(); //Do I need to clear the getline function?
in.close();
return InitialDish;
}
void SetPattern(Grid <char> &InitialDish){
Grid <char> TempGrid;
TempGrid.resize(InitialDish.numRows(), InitialDish.numCols());
for (int row = 0; row < (InitialDish.numRows()); row ++){
for (int col = 0; col < (InitialDish.numCols()); col ++){
int NumOfNeighbors;
if (InitialDish[row][col] == 'X'){
NumOfNeighbors = -1;
} else {
NumOfNeighbors = 0;
}
for (int r = -1; r < 2; r++){
for (int c = -1; c < 2; c++){
if (InitialDish.inBounds((row + r), (col + c)) &&
InitialDish[row +r][col + c] == 'X'){
NumOfNeighbors ++;
}
}
}
if (NumOfNeighbors == 0 || NumOfNeighbors == 1){
//Zero or one neighbors, empty in the next generation
TempGrid.set(row, col, '-');
}
else if (NumOfNeighbors == 2){
//Two neighbors, stable
TempGrid.set(row, col, InitialDish[row][col]);
}
else if (NumOfNeighbors == 3){
TempGrid.set(row, col, 'X');
}
else if (NumOfNeighbors > 3){
TempGrid.set(row, col, '-');
}
else {
TempGrid.set(row, col, '-');
}
}
}
InitialDish =TempGrid;
for (int RowOfInitialDish = 0; RowOfInitialDish < InitialDish.numRows(); RowOfInitialDish ++){
for (int ColOfInitialDish = 0; ColOfInitialDish < InitialDish.numCols(); ColOfInitialDish ++){
if (InitialDish[RowOfInitialDish][ColOfInitialDish] == 'X'){
cout << InitialDish[RowOfInitialDish][ColOfInitialDish];
} else {
cout << InitialDish[RowOfInitialDish][ColOfInitialDish];
}
}
cout << endl;
}
}