-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
417 lines (381 loc) · 12.3 KB
/
Controller.cpp
File metadata and controls
417 lines (381 loc) · 12.3 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "Controller.h"
#include "Agent.h"
#include "Agent_factory.h"
#include "Model.h"
#include "Structure_factory.h"
#include "Utility.h"
#include "View.h"
#include "Local_view.h"
#include "Map_view.h"
#include "Health_view.h"
#include "Amounts_view.h"
#include "Combat_view.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <iostream>
#include <new>
using std::any_of;
using std::bad_alloc;
using std::cin; using std::cout; using std::cerr; using std::endl;
using std::find;
using std::for_each;
using std::isalnum;
using std::shared_ptr;
using std::string;
const char* const invalid_name_c = "Invalid name for new object!";
const char* const invalid_double_c = "Expected a double!";
const char* const invalid_int_c = "Expected an integer!";
const char* const view_open_c = "View of that name already open!";
const char* const nonexistent_view_c = "No view of that name is open!";
const char* const map_view_closed_c = "No map view is open!";
const char* const nonexistent_object_c = "No object of that name!";
const char* const invalid_view_type_c = "No view of that type!";
const char* const unknown_command_c = "Unrecognized command!";
static Point read_new_obj_info(string& name, string& type);
static void check_name_validity(const string& name);
static Point read_Point();
static double read_double();
static int read_int();
static void print_error_msg_and_clear_input(const Error& error);
Controller::Controller()
{
// load command maps
map_view_commands["default"] = &Controller::restore;
map_view_commands["size"] = &Controller::size;
map_view_commands["zoom"] = &Controller::zoom;
map_view_commands["pan"] = &Controller::pan;
program_commands["open"] = &Controller::open;
program_commands["close"] = &Controller::close;
program_commands["status"] = &Controller::status;
program_commands["show"] = &Controller::show;
program_commands["go"] = &Controller::go;
program_commands["build"] = &Controller::build;
program_commands["train"] = &Controller::train;
agent_commands["move"] = &Controller::move;
agent_commands["work"] = &Controller::work;
agent_commands["attack"] = &Controller::attack;
agent_commands["stop"] = &Controller::stop;
agent_commands["heal"] = &Controller::heal;
}
void Controller::run()
{
Model& model = Model::get_instance();
string first_word;
while(true) {
cout << "\nTime " << model.get_time() << ": Enter command: ";
cin >> first_word;
if(first_word == "quit") {
for_each(view_ptrs.begin(), view_ptrs.end(),
[&model](shared_ptr<View> view_ptr){model.detach(view_ptr);});
cout << "Done" << endl;
return;
}
try {
// first test if it is an agent command
if(model.is_agent_present(first_word)) {
// In this case, the first word is the name of an agent
shared_ptr<Agent> agent_ptr = model.get_agent_ptr(first_word);
assert(agent_ptr -> is_alive());
string agent_command;
cin >> agent_command;
auto it = agent_commands.find(agent_command);
if(it != agent_commands.end()) {
(this ->* (it -> second))(agent_ptr);
continue;
} else {
throw Error(unknown_command_c);
}
}
// then test if it is a view command
auto view_it = map_view_commands.find(first_word);
if(view_it != map_view_commands.end()) {
if(map_view_ptr) {
(this ->* (view_it -> second))();
continue;
} else {
throw Error(map_view_closed_c);
}
}
// finally, test if it is a program command
auto program_it = program_commands.find(first_word);
if(program_it != program_commands.end()) {
(this ->* (program_it -> second))();
} else {
throw Error(unknown_command_c);
}
} catch(Error& e) {
print_error_msg_and_clear_input(e);
} catch(bad_alloc& ba) {
cerr << ba.what() << endl;
return;
} catch(...) {
cerr << "Unknown Exception caught!" << endl;
return;
}
}
}
void Controller::restore() const
{
map_view_ptr -> set_defaults();
}
void Controller::size() const
{
int size = read_int();
map_view_ptr -> set_size(size);
}
void Controller::zoom() const
{
double scale = read_double();
map_view_ptr -> set_scale(scale);
}
void Controller::pan() const
{
Point origin = read_Point();
map_view_ptr -> set_origin(origin);
}
void Controller::open()
{
string type;
cin >> type;
if(type == "map") {
if(map_view_ptr) {
throw Error(view_open_c);
} else {
map_view_ptr = shared_ptr<Map_view>(new Map_view);
add_view_ptr(map_view_ptr);
}
} else if(type == "health") {
if(health_view_ptr) {
throw Error(view_open_c);
} else {
health_view_ptr = shared_ptr<Health_view>(new Health_view);
add_view_ptr(health_view_ptr);
}
} else if(type == "amounts") {
if(amounts_view_ptr) {
throw Error(view_open_c);
} else {
amounts_view_ptr = shared_ptr<Amounts_view>(new Amounts_view);
add_view_ptr(amounts_view_ptr);
}
} else if(type == "local") {
string name;
cin >> name;
if(local_view_ptrs.find(name) != local_view_ptrs.end()) {
throw Error(view_open_c);
}
Point point;
bool exists = Model::get_instance().get_object_location(point, name);
if(exists) {
// if the object exists,
// add the Local_view pointer to all relevant containers
shared_ptr<Local_view> local_view_ptr(new Local_view(name, point));
local_view_ptrs[name] = local_view_ptr;
add_view_ptr(local_view_ptr);
} else {
throw Error(nonexistent_object_c);
}
} else if(type == "combat") {
string name;
cin >> name;
if(combat_view_ptrs.find(name) != combat_view_ptrs.end()) {
throw Error(view_open_c);
}
Point point;
bool exists = Model::get_instance().get_object_location(point, name);
if(exists) {
shared_ptr<Combat_view> combat_view_ptr(new Combat_view(name, point));
combat_view_ptrs[name] = combat_view_ptr;
add_view_ptr(combat_view_ptr);
} else {
throw Error(nonexistent_object_c);
}
} else {
throw Error(invalid_view_type_c);
}
}
void Controller::close()
{
string type;
cin >> type;
// check each type of the map, and then local map
// if none of the three specific maps matches
if(type == "map") {
if(!map_view_ptr) {
throw Error(nonexistent_view_c);
} else {
remove_view_ptr(map_view_ptr);
map_view_ptr.reset();
}
} else if(type == "health") {
if(!health_view_ptr) {
throw Error(nonexistent_view_c);
} else {
remove_view_ptr(health_view_ptr);
health_view_ptr.reset();
}
} else if(type == "amounts") {
if(!amounts_view_ptr) {
throw Error(nonexistent_view_c);
} else {
remove_view_ptr(amounts_view_ptr);
amounts_view_ptr.reset();
}
} else if(type == "local") {
string name;
cin >> name;
auto it = local_view_ptrs.find(name);
if(it == local_view_ptrs.end()) {
throw Error(nonexistent_view_c);
} else {
remove_view_ptr(it -> second);
local_view_ptrs.erase(it);
}
} else if(type == "combat") {
string name;
cin >> name;
auto it = combat_view_ptrs.find(name);
if(it == combat_view_ptrs.end()) {
throw Error(nonexistent_view_c);
} else {
remove_view_ptr(it -> second);
combat_view_ptrs.erase(it);
}
} else {
throw Error(invalid_view_type_c);
}
}
void Controller::status()
{
Model::get_instance().describe();
}
void Controller::show()
{
for(auto view_ptr : view_ptrs) {
view_ptr -> draw();
}
}
void Controller::go()
{
Model::get_instance().update();
}
void Controller::build()
{
string struct_name, struct_type;
Point location = read_new_obj_info(struct_name, struct_type);
shared_ptr<Structure> struct_ptr = create_structure(struct_name, struct_type, location);
Model::get_instance().add_structure(struct_ptr);
}
void Controller::train()
{
string agent_name, agent_type;
Point location = read_new_obj_info(agent_name, agent_type);
shared_ptr<Agent> agent_ptr = create_agent(agent_name, agent_type, location);
Model::get_instance().add_agent(agent_ptr);
}
void Controller::move(shared_ptr<Agent> agent_ptr) const
{
Point destination = read_Point();
agent_ptr -> move_to(destination);
}
void Controller::work(shared_ptr<Agent> agent_ptr) const
{
Model& model = Model::get_instance();
string source_name, destination_name;
cin >> source_name >> destination_name;
shared_ptr<Structure> source_ptr = model.get_structure_ptr(source_name);
shared_ptr<Structure> destination_ptr = model.get_structure_ptr(destination_name);
agent_ptr -> start_working(source_ptr, destination_ptr);
}
void Controller::heal(shared_ptr<Agent> agent_ptr) const
{
string target_name;
cin >> target_name;
shared_ptr<Agent> target_ptr = Model::get_instance().get_agent_ptr(target_name);
agent_ptr -> start_healing(target_ptr);
}
void Controller::attack(shared_ptr<Agent> agent_ptr) const
{
string target_name;
cin >> target_name;
shared_ptr<Agent> target_ptr = Model::get_instance().get_agent_ptr(target_name);
agent_ptr -> start_attacking(target_ptr);
}
void Controller::stop(shared_ptr<Agent> agent_ptr) const
{
agent_ptr -> stop();
}
void Controller::add_view_ptr(shared_ptr<View> view_ptr)
{
view_ptrs.push_back(view_ptr);
Model::get_instance().attach(view_ptr);
}
void Controller::remove_view_ptr(shared_ptr<View> view_ptr)
{
// find the view_ptr inside the sequential container and delete it
auto it = find(view_ptrs.begin(), view_ptrs.end(), view_ptr);
assert(it != view_ptrs.end());
view_ptrs.erase(it);
Model::get_instance().detach(view_ptr);
}
// Read the information of the to-be-created object and return its name, type and location.
static Point read_new_obj_info(string& name, string& type)
{
cin >> name;
check_name_validity(name);
cin >> type;
return read_Point();
}
// Check if the name is at least num_initial_letters characters long
// if it contains letters that is not alnum, and if it is already in use.
// Throw an exception if the name is invalid.
static void check_name_validity(const string& name)
{
if(name.length() < num_letters_c) {
throw Error(invalid_name_c);
}
if(any_of(name.begin(), name.end(), [](char c){return !isalnum(c);})) {
// throw exception if any of the character is not alnum
throw Error(invalid_name_c);
}
if(Model::get_instance().is_name_in_use(name)) {
throw Error(invalid_name_c);
}
}
// Read from the input two double values and return a Point
// whose x- and y- coordinates are the two doubles read.
static Point read_Point()
{
double x_coord = read_double(), y_coord = read_double();
return Point(x_coord, y_coord);
}
// Read a double from the input stream and return it if read is successful.
// Throw an exception if a double cannot be read.
static double read_double()
{
double number;
if(!(cin >> number)) {
cin.clear();
throw Error(invalid_double_c);
}
return number;
}
// Read an integer from the input stream and return it if read is successful.
// Throw an exception if an integer cannot be read.
static int read_int()
{
int number;
if(!(cin >> number)) {
cin.clear();
throw Error(invalid_int_c);
}
return number;
}
// Print the error message contained in the Error object
// and clean up the wrong line in the input.
static void print_error_msg_and_clear_input(const Error& error)
{
cout << error.msg << endl;
while(cin.get() != '\n');
}