-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cpp
More file actions
86 lines (66 loc) · 2.12 KB
/
Character.cpp
File metadata and controls
86 lines (66 loc) · 2.12 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
#include "Character.h"
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
Character::Character(string _name, string _type, int _attack, int _defense, int _remainingHealth, int _nMaxRounds) {
this->name = _name;
this->type = _type;
this->attack = _attack;
this->defense = _defense;
this->remainingHealth = _remainingHealth;
this->nMaxRounds = _nMaxRounds;
this->isAlive = true;
this->nRoundsSinceSpecial = 0;
this->healthHistory = new int[_nMaxRounds +1];
this->healthHistory[0] = _remainingHealth;
}
Character::Character(const Character& character) {
this->name = character.name;
this->type = character.type;
this->attack = character.attack;
this->defense = character.defense;
this->remainingHealth = character.remainingHealth;
this->nMaxRounds = character.nMaxRounds;
this->isAlive = character.isAlive;
this->nRoundsSinceSpecial = character.nRoundsSinceSpecial;
this->healthHistory = new int[nMaxRounds +1];
this->healthHistory[0] = remainingHealth;
}
Character Character::operator=(const Character& character) {
if(this == &character) {
return *this;
}
// if this object was used before, free the memory it has used, test the effectiveness by assigning objects to one another sp many times! DOO
if(this != NULL) {
delete[] this->healthHistory;
}
this->name = character.name;
this->type = character.type;
this->attack = character.attack;
this->defense = character.defense;
this->remainingHealth = character.remainingHealth;
this->nMaxRounds = character.nMaxRounds;
this->isAlive = character.isAlive;
this->nRoundsSinceSpecial = character.nRoundsSinceSpecial;
this->healthHistory = character.healthHistory;
return *this;
}
bool Character::operator<(const Character& other) {
int length = min(this->name.length(), other.name.length());
int ind = 0;
while(ind != length) {
if(this->name[ind] > other.name[ind]) {
return false;
break;
}else if(this->name[ind] < other.name[ind]) {
return true;
break;
}else{
ind++;
}
}
}
Character::~Character() {
delete[] healthHistory;
}