-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
293 lines (283 loc) · 10.5 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Point2D.h"
#include "View.h"
#include "Model.h"
#include "GameCommand.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <ctime>
#include <fstream>
#include "Input_Handling.h"
using namespace std;
//Function prototypes
bool IsNumber(string s);
bool IsWhiteSpace(string s);
const string POKEMON_BANNER = " ,'\\\n"
" _.----. ____ ,' _\\ ___ ___ ____\n"
"_,-' `. | | /`. \\,-' | \\ / | | \\ |`.\n"
"\\ __ \\ '-. | / `. ___ | \\/ | '-. \\ | |\n"
" \\. \\ \\ | __ | |/ ,','_ `. | | __ | \\| |\n"
" \\ \\/ /,' _`.| ,' / / / / | ,' _`.| | |\n"
" \\ ,-'/ / \\ ,' | \\/ / ,`.| / / \\ | |\n"
" \\ \\ | \\_/ | `-. \\ `' /| | || \\_/ | |\\ |\n"
" \\ \\ \\ / `-.`.___,-' | |\\ /| \\ / | | |\n"
" \\ \\ `.__,'| |`-._ `| |__| \\/ | `.__,'| | | |\n"
" \\_.-' |__| `-._ | '-.| '-.| | |\n"
" `' '-._|";
// TODO: Add help menu
int main(int argc, char** argv) {
// Welcome Message
cout << "EC327: Introduction to Software Engineering" << endl;
cout << "Fall 2019" << endl;
cout << "Programming Assignment 3" << endl;
cout << POKEMON_BANNER << endl;
//constructing the game model
Model game_model;
View game_view;
bool game_is_running = true;
bool read_from_file = false;
game_model.ShowStatus();
game_model.Display(game_view);
ifstream input_file;
if (argc == 2) {
input_file.open(argv[1], ios_base::in);
if (!input_file.is_open()) {
cout << "Failed to open input file " << argv[1] << endl;
exit(EXIT_FAILURE);
}
read_from_file = true;
srand(0);
} else
srand(time(NULL));
while (game_is_running) {
cout << "Enter command: ";
char command = 'a';
int id = 0;
int id1 = 0;
int id2 = 0;
char type;
bool error = false;
string input = "'";
double x = 0;
double y = 0;
unsigned int stamina_amount = 0;
unsigned int training_unit_amount = 0;
if (read_from_file) {
getline(input_file, input);
} else
getline(cin, input);
if (input.length() > 0 and !IsWhiteSpace(input)) {
istringstream iss(input);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
if (tokens[0].length() == 1)
command = tokens[0][0];
else
error = true;
try{
switch (command) {
//moving a Pikachu to a location
case 'm':
if (tokens.size() == 4) {
id = atoi(tokens[1].c_str());
x = atof(tokens[2].c_str());
y = atof(tokens[3].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
Point2D moveTo = Point2D(x, y);
DoMoveCommand(game_model, id, moveTo);
game_model.Display(game_view);
}
break;
case 'c':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
id2 = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoMoveToCenterCommand(game_model, id, id2);
game_model.Display(game_view);
}
break;
case 'g':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
id2 = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoMoveToGymCommand(game_model, id, id2);
game_model.Display(game_view);
}
break;
case 'r':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
stamina_amount = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoRecoverInCenterCommand(game_model, id, stamina_amount);
game_model.Display(game_view);
}
break;
case 't':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
training_unit_amount = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoTrainInGymCommand(game_model, id, training_unit_amount);
game_model.Display(game_view);
}
break;
case 's':
if (tokens.size() == 2) {
id = atoi(tokens[1].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoStopCommand(game_model, id);
game_model.Display(game_view);
}
break;
case 'v':
if (tokens.size() == 1) {
DoGoCommand(game_model, game_view);
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
break;
//advance 5 time ticks or until next event
case 'x':
if (tokens.size() == 1) {
DoRunCommand(game_model, game_view);
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
break;
//quit the program
case 'q':
if (tokens.size() == 1) {
cout << "Terminating program." << endl;
game_is_running = false;
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
break;
case 'b':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
id2 = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoReadyToBattleCommand(game_model, id, id2);
game_model.Display(game_view);
}
break;
case 'a':
if (tokens.size() == 3) {
id = atoi(tokens[1].c_str());
id2 = atoi(tokens[2].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if (!error) {
DoMoveToArenaCommand(game_model, id, id2);
game_model.Display(game_view);
}
break;
case 'n':
if (tokens.size() == 5) {
type=(tokens[1].c_str()[0]);
id = atoi(tokens[2].c_str());
x = atoi(tokens[3].c_str());
y = atoi(tokens[4].c_str());
}
else {
error = true;
throw Invalid_Input("Invalid Parameters");
}
if(id>9)
{
error=true;
throw Invalid_Input("ID has to be less than 10");
}
if (!error) {
NewCommand(game_model, type, id, x,y);
game_model.Display(game_view);
}
break;
default:
error = true;
throw Invalid_Input("Invalid Command");
break;
}
}
catch (Invalid_Input& except)
{
cout<<" Invalid input- "<<except.msg_ptr <<endl;
}
}
else
error = true;
/* if (error)
cout << "ERROR: Please enter a valid command!" << endl;*/
}
return 0;
}
bool IsNumber(string s){
for(int i = 0; i < s.length(); i++) {
if(!(s[i] >= '0' && s[i] <= '9')) {
return false;
}
}
return true;
}
bool IsWhiteSpace(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != ' ' && s[i] != '\t') {
return false;
}
}
return true;
}