-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokemon.cpp
572 lines (539 loc) · 20 KB
/
Pokemon.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "Pokemon.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
Pokemon::Pokemon() : GameObject('P') {
stamina = 20;
pokemon_dollars = 0.0;
current_center = NULL;
current_gym = NULL;
stamina_points_to_buy = 0;
training_units_to_buy = 0;
speed = 1;
this->name = "Pokemon";
is_in_gym = false;
is_in_center = false;
experience_points = 0;
this->speed = 5.0;
health = 20;
store_health= health;
physical_damage = 5;
magical_damage = 4;
defense= 15;
cout << "Pokemon constructed. " << endl;
}
Pokemon::Pokemon(string name, int id, char in_code, unsigned int speed, Point2D in_loc) :
GameObject(in_code,id, in_loc) {
stamina = 20;
pokemon_dollars = 0.0;
current_center = NULL;
current_gym = NULL;
stamina_points_to_buy = 0;
training_units_to_buy = 0;
this->name = name;
this->speed = speed;
is_in_gym = false;
is_in_center = false;
experience_points = 0;
health = 20;
store_health= health;
physical_damage = 5;
magical_damage = 4;
defense= 15;
cout << "Pokemon constructed. " << endl;
}
Pokemon::Pokemon(char in_code) : GameObject(in_code) {
stamina = 20;
pokemon_dollars = 0.0;
current_center = NULL;
current_gym = NULL;
stamina_points_to_buy = 0;
training_units_to_buy = 0;
this->name = "Pokemon";
this->speed = 5.0;
is_in_gym = false;
is_in_center = false;
experience_points = 0;
health = 20;
store_health= health;
physical_damage = 5;
magical_damage = 4;
defense= 15;
cout << "Pokemon constructed. " << endl;
}
Pokemon::Pokemon(string in_name, double speed, double hp, double phys_dmg, double
magic_dmg, double def, int in_id, char in_code, Point2D in_loc): GameObject('P',in_id , in_loc){
stamina = 20;
pokemon_dollars = 0.0;
current_center = NULL;
current_gym = NULL;
stamina_points_to_buy = 0;
training_units_to_buy = 0;
this->name = in_name;
this->speed = speed;
is_in_gym = false;
is_in_center = false;
experience_points = 0;
health = hp;
store_health= health;
physical_damage = phys_dmg;
magical_damage = magic_dmg;
defense = def;
cout << "Pokemon constructed. " << endl;
}
Pokemon::~Pokemon() {
cout << "Pokemon destructed. " << endl;
}
// PA4 code goes here....
bool Pokemon::IsAlive()
{
if (state == FAINTED)
return true;
else
return false;
}
void Pokemon::StartMovingToArena(BattleArena* arena){
if (!IsExhausted()) {
if (location == arena->GetLocation()) {
cout << display_code << GetId() << ": I am already at the Battle Arena!" << endl;
} else {
if (is_in_gym) {
current_gym->RemoveOnePokemon(); // handles corner case
is_in_gym = false;
} else if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
}else if (is_in_arena){
current_arena->RemoveOnePokemon();
is_in_arena = false;
}
state = MOVING_TO_ARENA;
current_arena = arena;
SetupDestination(arena->GetLocation());
cout << display_code << GetId() << ": On my way to arena " << arena->GetId() << endl;
}
} else
cout << display_code << GetId() << ": I am exhausted so I shouldn't be going to the arena.." << endl;
}
void Pokemon::TakeHit( double physical_damage, double magical_damage, double defense)
{
// This function will be called from StartBattle() function until either Pokemon
// or Rival has 0 or less than 0 health
double damage = 0;
if (health >0){
if (rand()%2)
{
cout <<"Physical damage hurts, Master!\n";
damage = (100.0 - defense)/ 100 * physical_damage;
}
else
{
cout <<"It is magic, Master!\n";
damage = (100.0 - defense)/ 100 * magical_damage;
}
// choose the attack type with a random function. Hint: Use rand()
health -= damage;
cout<< "Damage: "<< damage << endl;
cout<< "Health: "<< health << endl;
cout<< "*******\n";
}
}
void Pokemon::ReadyBattle(Rival* in_target)
{
if (!IsExhausted()) {
if (state == IN_ARENA || state == BATTLE) {
if (current_arena->IsAbleToFight(pokemon_dollars,stamina)){
if (!current_arena->IsBeaten()) {
if(in_target->IsAlive())
{target = in_target;
state = BATTLE;
cout << display_code << GetId() << ": Getting ready for the battle\n";
} else{
state = IN_ARENA;
cout << display_code << GetId() << ": No rivals in this Battle Arena!";
}
} else{
state = IN_ARENA;
cout << display_code << GetId() << ": Cannot battle! This Battle Arena is already beaten!"
<< current_gym->GetId() << endl;
}
} else {
cout << display_code << GetId() << ": Not enough stamina and/or money for battling" << endl;
state = IN_ARENA;
}
} else
cout << display_code << GetId() << ": I can only battle in a Battle Arena!" << endl;
} else
cout << display_code << GetId() << ": I am exhausted so no more battling for me.." << endl;
}
bool Pokemon::StartBattle(){
// Call this function in the BATTLE state
//while ((health > 0) && (target->IsAlive()))
this->stamina -= current_arena->GetStaminaCost();
this->pokemon_dollars-= current_arena->GetDollarCost();
while (health>=0)
{
this ->TakeHit(physical_damage, magical_damage, defense);
target->TakeHit(physical_damage, magical_damage, defense);
// if(!target->IsAlive()) {
if (target->get_health()<= 0){
target->IsAlive();
current_arena->num_rivals_remaining --;
this->health = store_health;
cout<< "Congrats Master, you defeated one rival!\n";
state = IN_ARENA;
return true;
}
}
cout<< "Oops, you was defeated by one rival!\n";
return false;
}
bool Pokemon::IsExhausted() {
return stamina == 0;
}
// returns true if the simulation should be stopped
bool Pokemon::Update() {
bool arrived = false;
unsigned int stamina_received = 0;
unsigned int stamina_cost = 0;
double dollar_cost = 0;
unsigned int num_training_units = 0;
unsigned int experience_gain = 0;
switch (state) {
case STOPPED:
return false;
case MOVING:
if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
} else if (is_in_gym) {
current_gym->RemoveOnePokemon();
is_in_gym = false;
}
if (stamina == 0) {
cout << name << " is out of stamina and can't move." << endl;
state = EXHAUSTED;
return true;
} else {
arrived = UpdateLocation();
if (arrived) {
state = STOPPED;
return true;
} else {
pokemon_dollars += GetRandomAmountOfPokemonDollars();
stamina -= 1;
return false;
}
}
case MOVING_TO_GYM:
if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
} else if (is_in_gym) {
current_gym->RemoveOnePokemon();
is_in_gym = false;
}
if (stamina == 0) {
cout << name << " is out of stamina and can't move." << endl;
state = EXHAUSTED;
return true;
} else {
arrived = UpdateLocation();
if (arrived) {
state = IN_GYM;
is_in_gym = true;
current_gym->AddOnePokemon();
return true;
} else {
pokemon_dollars += GetRandomAmountOfPokemonDollars();
stamina -= 1;
return false;
}
}
case MOVING_TO_CENTER:
if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
} else if (is_in_gym) {
current_gym->RemoveOnePokemon();
is_in_gym = false;
}
if (stamina == 0) {
cout << name << " is out of stamina and can't move." << endl;
state = EXHAUSTED;
return true;
} else {
arrived = UpdateLocation();
if (arrived) {
state = IN_CENTER;
is_in_center = true;
current_center->AddOnePokemon();
return true;
} else {
pokemon_dollars += GetRandomAmountOfPokemonDollars();
stamina -= 1;
return false;
}
}
case RECOVERING_STAMINA:
dollar_cost = current_center->GetDollarCost(stamina_points_to_buy);
pokemon_dollars -= dollar_cost;
stamina += current_center->DistributeStamina(stamina_points_to_buy);
cout << "** " << name << " recovered " << stamina_points_to_buy << " stamina point(s)! **" << endl;
stamina_points_to_buy = 0;
state = IN_CENTER;
return true;
case TRAINING_IN_GYM:
stamina_cost = current_gym->GetStaminaCost(training_units_to_buy);
dollar_cost = current_gym->GetDollarCost(training_units_to_buy);
experience_gain = current_gym->TrainPokemon(training_units_to_buy);
stamina -= stamina_cost;
pokemon_dollars -= dollar_cost;
experience_points += experience_gain;
cout << "** " << name << " completed " << training_units_to_buy << " training unit(s)! **" << endl;
cout << "** " << name << " gained " << experience_gain << " experience points! **" << endl;
training_units_to_buy = 0;
state = IN_GYM;
return true;
case MOVING_TO_ARENA:
if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
} else if (is_in_gym) {
current_gym->RemoveOnePokemon();
is_in_gym = false;
}else if (is_in_arena) {
current_arena->RemoveOnePokemon();
is_in_arena = false;
}
if (stamina == 0) {
cout << name << " is out of stamina and can't move." << endl;
state = EXHAUSTED;
return true;
} else {
arrived = UpdateLocation();
if (arrived) {
state = IN_ARENA;
is_in_arena = true;
current_arena->AddOnePokemon();
return true;
} else {
pokemon_dollars += GetRandomAmountOfPokemonDollars();
stamina -= 1;
return false;
}
}
case BATTLE:
pokemon_dollars -= current_arena->GetDollarCost();
stamina -= current_arena->GetStaminaCost();
if (StartBattle())
{
health = store_health;
state = IN_ARENA;
current_arena->GetNumRivalsRemaining();
target->IsAlive();
return true;
}
else{
cout<< "Oops, you was defeated by one rival!\n";
state= FAINTED;
target->IsAlive();
return false;
}
case EXHAUSTED:
case IN_GYM:
case IN_CENTER:
case IN_ARENA:
case FAINTED:
default:
return false;
}
}
void Pokemon::StartMovingToCenter(PokemonCenter* center) {
if (!IsExhausted()) {
if (location == center->GetLocation()) {
cout << display_code << GetId() << ": I am already at the Pokemon Center!" << endl;
} else {
if (is_in_center) {
current_center->RemoveOnePokemon(); // handles corner case
is_in_center = false;
} else if (is_in_gym) {
current_gym->RemoveOnePokemon();
is_in_gym = false;
}
state = MOVING_TO_CENTER;
current_center = center;
SetupDestination(center->GetLocation());
cout << display_code << GetId() << ": On my way to center " << center->GetId() << endl;
}
} else
cout << display_code << GetId() << ": I am exhausted so I can't move to recover stamina..." << endl;
}
void Pokemon::StartMovingToGym(PokemonGym* gym) {
if (!IsExhausted()) {
if (location == gym->GetLocation()) {
cout << display_code << GetId() << ": I am already at the Pokemon Gym!" << endl;
} else {
if (is_in_gym) {
current_gym->RemoveOnePokemon(); // handles corner case
is_in_gym = false;
} else if (is_in_center) {
current_center->RemoveOnePokemon();
is_in_center = false;
}
state = MOVING_TO_GYM;
current_gym = gym;
SetupDestination(gym->GetLocation());
cout << display_code << GetId() << ": On my way to gym " << gym->GetId() << endl;
}
} else
cout << display_code << GetId() << ": I am exhausted so I shouldn't be going to the gym.." << endl;
}
void Pokemon::StartTraining(unsigned int num_training_units) {
unsigned int training_units_in_gym = 0;
if (!IsExhausted()) {
if (state == IN_GYM || state == TRAINING_IN_GYM) {
if (current_gym->IsAbleToTrain(num_training_units,
pokemon_dollars, stamina)) {
if (!current_gym->IsBeaten()) {
training_units_in_gym = current_gym->GetNumTrainingUnitsRemaining();
training_units_to_buy = min(num_training_units, training_units_in_gym);
state = TRAINING_IN_GYM;
cout << display_code << GetId() << ": Started " << training_units_to_buy
<< " training unit(s) at Pokemon Gym " << current_gym->GetId() << endl;
} else
cout << display_code << GetId() << ": Cannot train! This Pokemon Gym is already beaten!"
<< current_gym->GetId() << endl;
} else
cout << display_code << GetId() << ": Not enough stamina and/or money for training" << endl;
} else
cout << display_code << GetId() << ": I can only train in a Pokemon Gym!" << endl;
} else
cout << display_code << GetId() << ": I am exhausted so no more training for me.." << endl;
}
void Pokemon::StartRecoveringStamina(unsigned int num_stamina_points) {
unsigned int stamina_count_in_center = 0;
if (state == IN_CENTER || state == RECOVERING_STAMINA) {
if (IsExhausted())
cout << display_code << GetId() << ": I barely made it to the Pokemon center!" << endl;
if (current_center->CanAffordStaminaPoints(num_stamina_points, pokemon_dollars)) {
if (current_center->HasStaminaPoints()) {
stamina_count_in_center = current_center->GetNumStaminaPointsRemaining();
state = RECOVERING_STAMINA;
stamina_points_to_buy = min(num_stamina_points, stamina_count_in_center);
cout << display_code << GetId() << ": Started recovering " << stamina_points_to_buy
<< " stamina point(s) at Pokemon Center " << current_center->GetId() << endl;
} else
cout << display_code << GetId()
<< ": Cannot recover! No stamina points available in this Pokemon Center "
<< current_center->GetId() << endl;
} else
cout << display_code << GetId() << ": Not enough money to recover stamina." << endl;
} else
cout << display_code << GetId() << ": I can only recover stamina at a Pokemon Center!" << endl;
}
void Pokemon::StartMoving(Point2D dest) {
if (!IsExhausted()) {
if (dest == this->GetLocation()) {
cout << display_code << GetId() << ": I'm already there. see?" << endl;
} else {
state = MOVING;
SetupDestination(dest);
cout << display_code << GetId() << ": On my way." << endl;
}
} else {
cout << display_code << GetId() << ": I am exhausted. I may move but you just cannot see me. " << endl;
}
}
void Pokemon::SetupDestination(Point2D dest) {
destination = dest;
delta = (destination - location)*(speed / GetDistanceBetween(destination, location));
}
void Pokemon::Stop() {
state = STOPPED;
cout << display_code << GetId() << ": Stopping.." << endl;
}
bool Pokemon::UpdateLocation() {
//check if within one step
if (fabs((destination - location).x) <= fabs(delta.x) &&
fabs((destination - location).y) <= fabs(delta.y)) {
location = destination;
cout << display_code << GetId() << ": I’m there!" << endl;
return true;
}
else {
location = location + delta;
cout << display_code << GetId() << ": step..." << endl;
return false;
}
}
string Pokemon::GetName() {
return name;
}
double GetRandomAmountOfPokemonDollars() {
double f_min = 0.0;
double f_max = 2.0;
double f = (double)rand() / RAND_MAX;
return f_min + f * (f_max - f_min);
}
void Pokemon::ShowStatus() {
cout << name << " status: ";
GameObject::ShowStatus();
switch (state) {
case STOPPED:
cout << " stopped" << endl;
break;
case MOVING:
cout << " moving at a speed of " << speed
<< " to " << destination << " at each step of " << delta << endl;
break;
case MOVING_TO_CENTER:
cout << " heading to Pokemon Center " << current_center->GetId()
<< " at a speed of " << speed << " at each step of " << delta << endl;
break;
case MOVING_TO_GYM:
cout << " heading to Pokemon Gym " << current_gym->GetId()
<< " at a speed of " << speed << " at each step of " << delta << endl;
break;
case IN_GYM:
cout << " inside Pokemon Gym " << current_gym->GetId() << endl;
break;
case IN_CENTER:
cout << " inside Pokemon Center " << current_center->GetId() << endl;
break;
case TRAINING_IN_GYM:
cout << " training in Pokemon Gym " << current_gym->GetId() << endl;
case RECOVERING_STAMINA:
cout << " recovering stamina in Pokemon Center " << current_gym->GetId() << endl;
break;
case IN_ARENA:
cout << " inside Battle Arena " << current_arena->GetId() << endl;
break;
case MOVING_TO_ARENA:
cout << " heading to Battle Arena " << current_arena->GetId()
<< " at a speed of " << speed << " at each step of " << delta << endl;
break;
case BATTLE:
cout << " battling in Battle Arena" << current_arena->GetId() << endl;
break;
case FAINTED:
cout << " fainted " << endl;
break;
case EXHAUSTED:
cout << endl;
default:
break;
}
// Print stats here
cout << "\tStamina: " << stamina << endl;
cout << "\tPokemon Dollars: " << pokemon_dollars << endl;
cout << "\tExperience Points: " << experience_points << endl;
cout << "\tHealth: " << health << endl;
cout << "\tPhysical Damage: " << physical_damage << endl;
cout << "\tMagical Damage: " << magical_damage << endl;
cout << "\tDefense: " << defense << endl;
}
bool Pokemon::ShouldBeVisible() {
return !IsExhausted();
}