-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
400 lines (324 loc) · 10.1 KB
/
Model.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
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
#include "Model.h"
//creating the main model for the entire game
Model::Model()
{
time = 0;
//creating Point2D objects for initial locations
p1 = Point2D(1, 20); //PokemonCenter C1 location
p2 = Point2D(10, 20); //PokemonCenter C2 location
p3 = Point2D(5, 1); //Trainer T1 (Ash) location
p4 = Point2D(10, 1); //Trainer T2 (Misty) location
p5 = Point2D(0, 0); //PokemonGym G1 location
p6 = Point2D(5, 5); //PokemonGym G2 location
p7 = Point2D(10, 12); //WildPokemon1 location
p8 = Point2D(15, 5); //WildPokemon2 W2 location
//creating six main objects
PokemonCenter* C1 = new PokemonCenter(1, 1, 100, p1); //(id = 1) (cost = 1) (capacity = 100)
PokemonCenter* C2 = new PokemonCenter(2, 2, 200, p2); //(id = 1) (cost = 2) (capacity = 200)
Trainer* T1 = new Trainer("Ash", 1, 'T', 1, p3); //(id = 1) (speed = 1)
Trainer* T2 = new Trainer("Misty", 2, 'T', 2, p4) ; //(id = 2) (speed = 2)
PokemonGym* G1 = new PokemonGym(10, 1, 2, 3, 1, p5); //(battles = 10) (health = 1) (cost = 2) (XP = 3) (ID = 1)
PokemonGym* G2 = new PokemonGym(20, 5, 7.5, 4, 2, p6); //(battles = 20) (health = 5) (cost = 7.5) (XP = 4) (ID = 2)
//creating WildPokemon
WildPokemon* W1 = new WildPokemon("WildPokemon1", 5, 2, false, 1, p7); //(attack = 5) (health = 2) (no variant) (ID = 1)
WildPokemon* W2 = new WildPokemon("WildPokemon2", 5, 2, false, 2, p8); //(ID = 2)
//add all objects into list
object_ptrs.push_back(G1);
object_ptrs.push_back(G2);
object_ptrs.push_back(T1);
object_ptrs.push_back(T2);
object_ptrs.push_back(C1);
object_ptrs.push_back(C2);
object_ptrs.push_back(W1);
object_ptrs.push_back(W2);
//put all initial objects into active_ptrs array
active_ptrs.assign(object_ptrs.begin(), object_ptrs.end());
gym_ptrs.push_back(G1);
gym_ptrs.push_back(G2);
trainer_ptrs.push_back(T1);
trainer_ptrs.push_back(T2);
center_ptrs.push_back(C1);
center_ptrs.push_back(C2);
pokemon_ptrs.push_back(W1);
pokemon_ptrs.push_back(W2);
//sets numbers of initial objects
num_objects = 8;
num_trainers = 2;
num_centers = 2;
num_gyms = 2;
num_pokemon = 2;
}
//destructor
Model::~Model()
{
cout << "Model destructed" << endl;
}
//gets pointer from Trainer object
Trainer* Model::GetTrainerPtr(int id)
{
for (list<Trainer*> :: iterator iter1 = trainer_ptrs.begin(); iter1 != trainer_ptrs.end(); iter1++)
{
if (((*iter1) -> GetTrainerID()) == id)
{
return *iter1;
}
}
return nullptr;
}
//gets pointer from PokemonCenter object
PokemonCenter* Model::GetPokemonCenterPtr(int id)
{
for (list<PokemonCenter*> :: iterator iter1 = center_ptrs.begin(); iter1 != center_ptrs.end(); iter1++)
{
if (((*iter1) -> GetCenterID()) == id)
{
return *iter1;
}
}
return nullptr;
}
//gets pointer from PokemonGym object
PokemonGym* Model::GetPokemonGymPtr(int id)
{
for (list<PokemonGym*> :: iterator iter1 = gym_ptrs.begin(); iter1 != gym_ptrs.end(); iter1++)
{
if (((*iter1) -> GetGymID()) == id)
{
return *iter1;
}
}
return nullptr;
}
//gets pointer from Pokemon object
WildPokemon* Model::GetPokemonPtr(int id)
{
for (list<WildPokemon*> :: iterator iter1 = pokemon_ptrs.begin(); iter1 != pokemon_ptrs.end(); iter1++)
{
if (((*iter1) -> GetPokemonID()) == id)
{
return *iter1;
}
}
return nullptr;
}
//Update function specific to Model class
bool Model::Update()
{
//increment time
time = time + 1;
bool result = false;
bool did_we_update = false;
double distance;
Trainer* trainer_to_follow;
WildPokemon* following_pokemon;
//clears all non-active objects, removing ones that have fainted or died
for( list<GameObject*> :: iterator iter1 = active_ptrs.begin(); iter1 != active_ptrs.end(); iter1++)
{
if (!(*iter1) -> ShouldBeVisible())
{
active_ptrs.erase(iter1);
break;
}
}
//updates all gameObjects
for( list<GameObject*> :: iterator iter2 = active_ptrs.begin(); iter2 != active_ptrs.end(); iter2++)
{
if((!(*iter2) -> Update()) == true)
{
did_we_update = true;
result = result || did_we_update;
}
}
//removing dead pokemon
for(list<WildPokemon*> :: iterator iter7 = pokemon_ptrs.begin(); iter7 != pokemon_ptrs.end(); iter7++)
{
if (((*iter7) -> IsAlive()) == true)
{
pokemon_ptrs.erase(iter7);
break;
}
}
//update all WildPokemon's current trainers, figure out if any are following trainers
for(list<WildPokemon*> :: iterator iter5 = pokemon_ptrs.begin(); iter5 != pokemon_ptrs.end(); iter5++)
{
if ((*iter5) == nullptr)
{
break;
}
//iterates through list of trainers
for( list<Trainer*> :: iterator iter6 = trainer_ptrs.begin(); iter6 != trainer_ptrs.end(); iter6++)
{
//cout << "SECOND ITERATION" << endl;
if ((*iter6) == nullptr)
{
break;
}
Point2D trainer_location = (*iter6) -> GetLocation();
Point2D pokemon_location = (*iter5) -> GetLocation();
if ((trainer_location.x == pokemon_location.x) && (trainer_location.y == pokemon_location.y))
{
trainer_to_follow = *iter6;
following_pokemon = *iter5;
(following_pokemon) -> follow(trainer_to_follow);
}
}
}
//initialize variables
int gym_count = 0;
int trainer_count = 0;
//tally up how many gyms have been defeated
for( list<PokemonGym*> :: iterator iter3 = gym_ptrs.begin(); iter3 != gym_ptrs.end(); iter3++)
{
if ((*iter3) -> GetNumBattlesRemaining() == 0)
{
gym_count = gym_count + 1;
}
}
//if all gyms have been defeated, exit game
if (gym_count == num_gyms)
{
cout << "GAME OVER: You win! All battles done!" << endl;
exit(0);
}
//counting up how many trainers have fainted
for( list<Trainer*> :: iterator iter4 = trainer_ptrs.begin(); iter4 != trainer_ptrs.end(); iter4++)
{
if ((*iter4) -> HasFainted())
{
trainer_count = trainer_count + 1;
}
}
//if all trainers dead, game ends
if (trainer_count == num_trainers)
{
cout << "GAME OVER: You lose! All of your Trainers' Pokemon have fainted!" << endl;
exit(0);
}
return result;
}
void Model::Display(View& view)
{
//go through list of active pointers and plot them on the grid
for( list<GameObject*> :: iterator iter1 = active_ptrs.begin(); iter1 != active_ptrs.end(); iter1++)
{
if ((*iter1) == nullptr)
{
break;
}
else
{
view.Plot(*iter1);
}
}
view.Draw();
}
//ShowStatus for every object in object_ptrs list, including dead objects
void Model::ShowStatus()
{
cout << "Time is: " << time << endl;
for( list<GameObject*> :: iterator iter5 = object_ptrs.begin(); iter5 != object_ptrs.end(); iter5++)
{
if ((*iter5) == nullptr)
{
return;
}
else
{
(*iter5) -> ShowStatus();
}
}
}
void Model::NewCommand(char type, int id_num, int X, int Y)
{
//checking for unrecognized type code
if ((type != 't') && (type != 'g') && (type != 'c') && (type != 'w'))
{
throw Invalid_Input("Please enter a valid command");
return;
}
//checking to make sure that the object is placed on map
if ((X > 20) || (Y > 20))
{
throw Invalid_Input("Item out of bounds, please enter a different location");
return;
}
//input checking for TRAINER
if (type == 't')
{
//check to make sure there are no repeat Trainer ID numbers
for( list<Trainer*> :: iterator iter1 = trainer_ptrs.begin(); iter1 != trainer_ptrs.end(); iter1++)
{
int tmp_id = (*iter1) -> GetTrainerID();
//if input ID matches an existing one, through exception
if (tmp_id == id_num)
{
throw Invalid_Input("Trainer ID taken, please enter a diferent ID number");
return;
}
}
//Make new trainer object
Trainer* new_trainer = new Trainer("GREGORY", id_num, 'T', 1, Point2D(X, Y)); //speed of 1
trainer_ptrs.push_back(new_trainer);
active_ptrs.push_back(new_trainer);
num_trainers = num_trainers + 1;
}
//input checking for CENTER
if (type == 'c')
{
//check to make sure there are no repeat Trainer ID numbers
for( list<PokemonCenter*> :: iterator iter2 = center_ptrs.begin(); iter2 != center_ptrs.end(); iter2++)
{
int tmp_id = (*iter2) -> GetCenterID();
//if input ID matches an existing one, through exception
if (tmp_id == id_num)
{
throw Invalid_Input("PokemonCenter ID taken, please enter a diferent ID number");
return;
}
}
//make new PokemonCenter object
PokemonCenter* new_center = new PokemonCenter(id_num, 1, 100, Point2D(X, Y)); //speed of 1
center_ptrs.push_back(new_center);
active_ptrs.push_back(new_center);
num_centers = num_centers + 1;
}
//input checking for GYM
if (type == 'g')
{
//check to make sure there are no repeat Trainer ID numbers
for( list<PokemonGym*> :: iterator iter3 = gym_ptrs.begin(); iter3 != gym_ptrs.end(); iter3++)
{
int tmp_id = (*iter3) -> GetGymID();
//if input ID matches an existing one, through exception
if (tmp_id == id_num)
{
throw Invalid_Input("PokemonGym ID taken, please enter a diferent ID number");
return;
}
}
//make new PokemonGym object
PokemonGym* new_gym = new PokemonGym(10, 1, 2, 3, id_num, Point2D(X, Y));
gym_ptrs.push_back(new_gym);
active_ptrs.push_back(new_gym);
num_gyms = num_gyms + 1;
}
//input checking for WILDPOKEMON
if (type == 'w')
{
//check to make sure there are no repeat Trainer ID numbers
for( list<WildPokemon*> :: iterator iter4 = pokemon_ptrs.begin(); iter4 != pokemon_ptrs.end(); iter4++)
{
int tmp_id = (*iter4) -> GetPokemonID();
//if input ID matches an existing one, through exception
if (tmp_id == id_num)
{
throw Invalid_Input("WildPokemon ID taken, please enter a diferent ID number");
return;
}
}
WildPokemon* new_pokemon = new WildPokemon("Pikachu", 5, 2, false, id_num, Point2D(X, Y));
pokemon_ptrs.push_back(new_pokemon);
active_ptrs.push_back(new_pokemon);
num_pokemon = num_pokemon + 1;
}
}