-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
171 lines (162 loc) · 4.7 KB
/
main.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
#include "gameoflife.h"
#include <unistd.h>
/*!
* @brief prints main menu
*/
void print_main_menu() {
cout << "MENU \n";
cout << "Choose an action: \n";
cout << "A: Import custom state. \n";
cout << "B: Generate new random state.\n";
cout << "C: Export current state to a file. \n";
cout << "D: Start evolution. \n";
cout << "E: Change a particular cell. \n";
cout << "F: Exit. \n";
cout << ">>> ";
}
/*!
* @brief Handles user input for file import
* @param game: current game
* @return modified game
*/
GameOfLife import_wrapper(GameOfLife game) {
cout << "Enter the path to your file: \n>>> ";
string input;
cin >> input;
ifstream file(input);
if (file.good()) {
try {
game.import_state(input);
cout << "Import successful. \n";
} catch (int) {
cout << "Invalid file. \n";
game.change_dimensions({30, 30});
}
} else cout << "File not found.\n";
return game;
}
/*!
* @brief Handles user input for generating new random field
* @param game: current game
* @return modified game
*/
GameOfLife random_start_wrapper(GameOfLife game) {
cout << "Default field size is 30x30. Modify field size? [Y/N] \n>>> ";
string input;
pair<int,int> dimensions = {30, 30};
try {
cin >> input;
if (input == "Y") {
cout << "Enter number of rows: \n>>> ";
cin >> input;
dimensions.first = GameOfLife::to_stoi(input);
cout << "Enter number of columns: \n>>> ";
cin >> input;
dimensions.second = GameOfLife::to_stoi(input);
game.change_dimensions(dimensions);
}
else if (dimensions.first != game.nrows || dimensions.second != game.ncols) {
game.change_dimensions(dimensions);
}
game.get_random_field();
cout << "Successfully generated random state. \n";
} catch (int) {
cout << "Invalid input. \n";
}
return game;
}
/*!
* @brief Handles user input for export to file
* @param game: current game
* @return game
*/
GameOfLife export_wrapper(GameOfLife game) {
cout << "Enter export path: \n>>> ";
string input;
cin >> input;
game.write_to_file(input);
ifstream fileTest(input);
if (fileTest.good()) cout << "Export successful. \n";
else cout << "Something went wrong. \n";
return game;
}
/*!
* @brief Handles user input (number of steps) for starting evolution
* @param game: current game
* @return game after n generations
*/
GameOfLife evolution_wrapper(GameOfLife game) {
cout << "Enter number of steps: \n>>> ";
string input;
try {
cin >> input;
int nSteps = GameOfLife::to_stoi(input);
cout << "Starting evolution. \n";
int i = 0;
game.print_current();
while (i < nSteps) {
game.evolve();
game.print_current();
usleep(80000);
i++;
}
} catch (int) {
cout << "Invalid input. \n";
}
return game;
}
/*!
* @brief Handles user input for changing cell content
* @param game: current game
* @return game after n generations
*/
GameOfLife change_cell_wrapper(GameOfLife game) {
string input;
cout << "Enter row of cell: \n>>> ";
try {
cin >> input;
int row = GameOfLife::to_stoi(input) - 1;
cout << "Enter column of cell: \n>>> ";
cin >> input;
int col = GameOfLife::to_stoi(input) - 1;
if (row >= game.nrows || col >= game.ncols) {
cout << "Invalid row or column.\n";
} else {
cout << "Enter new value of cell: [*/o] \n>>> ";
cin >> input;
game.currentGeneration[row][col].set_status(input.front());
cout << "Change successful. \n";
}
} catch (int) {
cout << "Invalid input. \n";
}
return game;
}
/*!
* @brief selects wrapper based on user input
*/
GameOfLife console(GameOfLife game) {
print_main_menu();
string input;
cin >> input;
if (input == "A") return import_wrapper(game);
else if (input == "B") return random_start_wrapper(game);
else if (input == "C") return export_wrapper(game);
else if (input == "D") return evolution_wrapper(game);
else if (input == "E") return change_cell_wrapper(game);
else if (input == "F") { cout << "Bye! \n"; game.is_running = false; }
else {cout << "Try again. \n";}
return game;
}
/*!
* @brief Creates a new game, promts for user input and prints state after each action
*/
int main() {
GameOfLife game = GameOfLife();
while (game.is_running){
cout << "Current state is: \n \n";
game.print_current();
game = console(game);
}
return 0;
}