-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.cpp
243 lines (215 loc) · 5.78 KB
/
Game.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "Game.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <time.h>
#include <unistd.h>
#include <string>
#include "Grid.h"
#include "GameMode.h"
//Constructors==================================================================
Game::Game() //default constructor
{
promptGameMode(); //constructor calls all methods for gameplay
promptCells();
promptOutput();
gameLoop();
}
Game::~Game()
{
delete grid;
delete mode;
}
//Auxiliary functions===========================================================
//Main Loop
void Game::gameLoop() {
if(returnMode == 1) { //Game will loop on its own until termination
int count = 0;
bool loop = true;
while(loop) {
cout << "Generation " << count << endl;
grid->printGrid();
loop = mode->update(*grid);
count++;
usleep(100000);
}
cout << "Press Enter to Continue." << endl;
cin.ignore();
cin.ignore();
}
else if(returnMode == 2) {
//Enter to update generations
int count = 0;
bool loop = true;
while(loop) { //Game will loop when enter is pressed until termination
cout << "Generation " << count << endl;
grid->printGrid();
cout << "Press Enter to Continue." << endl;
if(count == 0)
{
cin.ignore();
}
cin.ignore();
loop = mode->update(*grid);
count++;
}
}
else if(returnMode == 3) {
//output game to file prompted for earlier
int count = 0;
bool loop = true;
ofstream fout;
fout.open(outputFileName);
if(!fout){
exit(EXIT_FAILURE);
}
cout << "Outputting Game to File." << endl;
while(loop) {
fout << "Generation " << count << endl;
grid->printGridFile(fout);
loop = mode->update(*grid);
count++;
}
fout.close();
cout << "File has been created. Please look for file in your directory." << endl;
cout << "Press Enter to Continue." << endl;
cin.ignore();
cin.ignore();
}
else {
cout << "An error has occured. Execution terminated." << endl;
}
}
void Game::promptGameMode() //prompts user for gamemode
{
while(true){
string gameModeControl;
cout << "What mode would you like to play in: " << endl;
cout << "1. classic" << endl;
cout << "2. donut" << endl;
cout << "3. mirror" << endl;
getline(cin,gameModeControl);
if(gameModeControl == "1") {
mode = new GameMode;
cout << "\nClassic mode selected!\n" << endl;
break;
}
else if(gameModeControl == "2") {
mode = new GameMode(1);
cout << "\nDonut mode selected!\n" << endl;
break;
}
else if(gameModeControl == "3") {
mode = new GameMode(2);
cout << "\nMirror mode selected!\n" << endl;
break;
}
else {
//catch errors
cout << "\nInvalid input." << endl;
}
}
}
void Game::promptCells() //prompts user for map file or dimensions
{
string inputControl;
cout << "Would you like to read a map file ('1') or provide dimensions\nand density for a random grid of cells ('2')?" << endl;
cout << "1. Read map file." << endl;
cout << "2. Provide dimensions and density." << endl;
getline(cin,inputControl);
if(inputControl == "2") {
cout << "Height:\n";
//prompt for height as str and convert to int
int height;
cin >> height;
if (cin.fail())
{
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
//prompt for width as str and convert to int
cout << "Width:\n";
int width;
cin >> width;
if (cin.fail())
{
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
//prompt for density as str and convert to int
cout << "Population Density (Decimal between 0 and 1):\n";
double density;
cin >> density;
if (cin.fail())
{
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
//check for int between 0 and 1
if(density > 1 || density < 0) {
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
grid = new Grid(height,width,density);
}
else if (inputControl == "1") {
grid = new Grid(promptFileName());
}
else
{
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
}
void Game::promptOutput() //Prompts how the user would like game outputted
{
int outputControl;
cout << "\nHow do you want this game outputted for you?" << endl;
cout << "If you want to have a brief pause between each generation, enter '1': " << endl;
cout << "If you want to press the enter key for each generation to appear, enter '2': " << endl;
cout << "If you want to output everything to a file, enter '3': " << endl;
cin >> outputControl;
if (cin.fail())
{
cerr << "ERROR Invalid input. Exit Program." << endl;
exit(EXIT_FAILURE);
}
if(outputControl == 1)
{
returnMode = 1;
}
else if(outputControl == 2)
{
returnMode = 2;
}
else if(outputControl == 3)
{
returnMode = 3;
outputFileName = outFileName();
}
}
string Game::promptFileName() //prompts the user for a file path
{
while(true) {
string fileName;
cout << "\nWhat is the name of the file?: " << endl;
getline(cin,fileName);
//Check to make sure file exists
ifstream f;
f.open(fileName);
if(f) {
f.close();
return fileName;
}
else {
cout << "File not found. Try again." << endl;
}
}
}
string Game::outFileName()
{
string fileName;
cout << "\nWhat is the name of the file?: " << endl;
cin >> fileName;
return fileName;
}