-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
298 lines (247 loc) · 8.21 KB
/
Source.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
#include "Header.h"
// Implementation
int RandomIntegerGenerator::next() {
return rand() % 100;
}
int RandomIntegerGenerator::next(int max) {
return rand() % max;
}
int RandomIntegerGenerator::next(int min, int max) {
return rand() % (max - min + 1) + min;
}
Character::Character(int hp, int atk) {
_hp = hp;
_atk = atk;
_state = new NormalState();
}
void IVisitor::select(Character* opponent) {
if (typeid(*opponent) == typeid(Swordman)) {
attack(*((Swordman*)opponent));
}
else if (typeid(*opponent) == typeid(Knight)) {
attack(*((Knight*)opponent));
}
else if (typeid(*opponent) == typeid(Spikeman)) {
attack(*((Spikeman*)opponent));
}
}
int Character::hp() { return _state->transfromHp(_hp); }
int Character::atk() { return _state->transfromAtk(_atk); }
void Character::setHp(int value) { _hp = value > 0 ? value : 0; }
void Character::setAtk(int value) { _atk = value > 0 ? value : 0; }
bool Character::isAlive() { return _hp > 0; }
void Character::setState(State* state) { _state = state; }
// --- Swordman ---
Swordman::Swordman(int hp, int atk) : Character(hp, atk) {}
string Swordman::toString() {
stringstream ss;
ss << "Swordman (HP " << hp() << ", ATK " << atk() << ")";
string result = ss.str();
return result;
}
void Swordman::attack(Swordman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Swordman::attack(Knight& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Swordman::attack(Spikeman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Swordman::attack(Character* opponent) {
select(opponent);
}
void Swordman::hitBy(Swordman& opponent) { _hp = _hp - opponent.atk(); }
void Swordman::hitBy(Knight& opponent) { _hp = _hp - opponent.atk() + 10; }
void Swordman::hitBy(Spikeman& opponent) { _hp = _hp - opponent.atk(); }
// --- Knight ---
Knight::Knight(int hp, int atk) : Character(hp, atk) {}
string Knight::toString() {
stringstream ss;
ss << "Knight (HP " << hp() << ", ATK " << atk() << ")";
string result = ss.str();
return result;
}
void Knight::attack(Swordman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Knight::attack(Knight& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Knight::attack(Spikeman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Knight::attack(Character* opponent) {
select(opponent);
}
void Knight::hitBy(Swordman& opponent) { _hp = _hp - opponent.atk(); }
void Knight::hitBy(Knight& opponent) { _hp = _hp - opponent.atk(); }
void Knight::hitBy(Spikeman& opponent) { _hp = _hp - opponent.atk() + 25; }
// --- Spikeman ---
Spikeman::Spikeman(int hp, int atk) : Character(hp, atk) {}
string Spikeman::toString() {
stringstream ss;
ss << "Spikeman (HP " << hp() << ", ATK " << atk() << ")";
string result = ss.str();
return result;
}
void Spikeman::attack(Swordman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Spikeman::attack(Knight& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Spikeman::attack(Spikeman& opponent) {
opponent.hitBy(*this);
if (opponent.isAlive()) { hitBy(opponent); }
}
void Spikeman::attack(Character* opponent) {
select(opponent);
}
void Spikeman::hitBy(Swordman& opponent) { _hp = _hp - opponent.atk() + 12; }
void Spikeman::hitBy(Knight& opponent) { _hp = _hp - opponent.atk(); }
void Spikeman::hitBy(Spikeman& opponent) { _hp = _hp - opponent.atk(); }
// --- State ---
int NormalState::transfromHp(int old) { return old; }
int NormalState::transfromAtk(int old) { return old; }
int ThreeSwordmanState::transfromHp(int old) { return old; }
int ThreeSwordmanState::transfromAtk(int old) { return old * 3; }
int ThreeKnightState::transfromHp(int old) { return old; }
int ThreeKnightState::transfromAtk(int old) { return old * 2; }
int ThreeSpikemanState::transfromHp(int old) { return old * 2; }
int ThreeSpikemanState::transfromAtk(int old) { return old * 5; }
// --- Player ---
void Player::add(Character* piece) {
_pieces.push_back(piece);
}
string Player::toString() {
stringstream builder;
for (int i = 0; i < _pieces.size(); i++) {
// delete "," if it is the last one
if (i == _pieces.size() - 1) {
builder << _pieces[i]->toString();
}
else {
builder << _pieces[i]->toString() << ", ";
}
}
string result = builder.str();
return result;
}
// --- Game ---
Game::Game() {
_rules.push_back(new ThreeSwordmanRule());
_rules.push_back(new ThreeKnightRule());
_rules.push_back(new ThreeSpikemanRule());
_blueTeam.checker = &Game::checkRule;
_redTeam.checker = &Game::checkRule;
}
void Game::checkRule(Player* player) {
for (int i = 0; i < _rules.size(); i++) {
if (_rules[i]->satisfyWith(player)) {
_rules[i]->apply(player);
}
}
}
void Game::start() {
RandomIntegerGenerator rig;
// random 3 pieces for each team
for (int i = 0; i < 3; i++)
{
int bluePieces = rig.next(1, 3);
int redPieces = rig.next(1, 3);
if (bluePieces == 1) { _blueTeam.add(new Swordman(1000, 12)); }
else if (bluePieces == 2) { _blueTeam.add(new Knight(800, 8)); }
else if (bluePieces == 3) { _blueTeam.add(new Spikeman(1500, 18)); }
if (redPieces == 1) { _redTeam.add(new Swordman(100, 12)); }
else if (redPieces == 2) { _redTeam.add(new Knight(800, 12)); }
else if (redPieces == 3) { _redTeam.add(new Spikeman(1500, 18)); }
}
checkRule(&_redTeam);
checkRule(&_blueTeam);
}
void Game::play() {
RandomIntegerGenerator rig;
int decide = rig.next(1, 2);
if (decide == 1) {
cout << endl << "Red team goes first:" << endl;
for (int i = 0; i < 3; i++) {
cout << "Turn " << i + 1 << endl;
_redTeam._pieces[i]->attack(_blueTeam._pieces[i]);
_blueTeam._pieces[i]->attack(_redTeam._pieces[i]);
cout << _redTeam.toString() << endl;
cout << _blueTeam.toString() << endl;
}
}
else {
cout << endl << "Blue team goes first:" << endl;
for (int i = 0; i < 3; i++) {
cout << "Turn " << i + 1 << endl;
_blueTeam._pieces[i]->attack(_redTeam._pieces[i]);
_redTeam._pieces[i]->attack(_blueTeam._pieces[i]);
cout << _redTeam.toString() << endl;
cout << _blueTeam.toString() << endl;
}
}
}
string Game::toString() {
stringstream builder;
builder << "Red team: " << _redTeam.toString() << endl;
builder << "Blue team: " << _blueTeam.toString() << endl;
string result = builder.str();
return result;
}
// --- Rules ---
bool ThreeSwordmanRule::satisfyWith(Player* player) {
int count = 0;
for (int i = 0; i < player->_pieces.size(); i++) {
if (typeid(*player->_pieces[i]) == typeid(Swordman)) {
count++;
}
}
bool result = count == 3;
return result;
}
void ThreeSwordmanRule::apply(Player* player) {
for (int i = 0; i < player->_pieces.size(); i++) {
player->_pieces[i]->setState(new ThreeSwordmanState());
}
}
bool ThreeKnightRule::satisfyWith(Player* player) {
int count = 0;
for (int i = 0; i < player->_pieces.size(); i++) {
if (typeid(*player->_pieces[i]) == typeid(Knight)) {
count++;
}
}
bool result = count == 3;
return result;
}
void ThreeKnightRule::apply(Player* player) {
for (int i = 0; i < player->_pieces.size(); i++) {
player->_pieces[i]->setState(new ThreeKnightState());
}
}
bool ThreeSpikemanRule::satisfyWith(Player* player) {
int count = 0;
for (int i = 0; i < player->_pieces.size(); i++) {
if (typeid(*player->_pieces[i]) == typeid(Spikeman)) {
count++;
}
}
bool result = count == 3;
return result;
}
void ThreeSpikemanRule::apply(Player* player) {
for (int i = 0; i < player->_pieces.size(); i++) {
player->_pieces[i]->setState(new ThreeSpikemanState());
}
}