-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamestate.cpp
More file actions
294 lines (262 loc) · 7.68 KB
/
gamestate.cpp
File metadata and controls
294 lines (262 loc) · 7.68 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
#include "gamestate.h"
#include "MyConstant.h"
#include <QString>
int mymap[] = { 1,1,1,1,2,2,5,5,0,
1,2,2,1,2,2,2,1,0,
1,2,2,1,1,1,1,1,0,
4,3,3,3,3,3,2,3,0,
4,2,2,2,2,2,2,2,100}; //0表示换行,1表示近战路径,2表示远程路径,3表示障碍,
//4表示我方基地,5表示敌方基地,100表示地图结束
QString way1 = "(1,8)SSWWWWNNWWWSSS";
QString way2 = "(1,7)SSSSWWWWWW";
//判断一个生物是否能放到该格子上
bool canadd(Creature* crt, Place* pl)
{
bool result = false;
if (crt->getCanInRemote()) {
result = true;
}
if ((!crt->getCanInRemote()) && (!pl->getIs_remote())) {
result = true;
}
return result;
}
Crtvector GameState::getAllplant() const
{
return allplant;
}
Crtvector GameState::getAllzombie() const
{
return allzombie;
}
bool GameState::getGameover() const
{
return gameover;
}
placeMap GameState::getAllplace() const
{
return allplace;
}
int GameState::getRoll() const
{
return roll;
}
GameState::GameState()
{
resource = INITRESOURSE;
roll = 0;
gameover = false;
int cnt = 0;
for (int i = 1; ;i++) {
for (int j = 1; ; j++){
if (mymap[cnt] == 0 || mymap[cnt] == 100) break;
Place* newPlace = new Place(mymap[cnt], (double)i ,(double)j);
QString placePosition = QString("(%1,%2)").arg(i).arg(j);
allplace.insert(placePosition,newPlace);
cnt++;
}
if (mymap[cnt] == 100) break;
cnt++;
}
placeMap::iterator initplace = allplace.find(way1.mid(0,5));
if (initplace == allplace.end()) {
qDebug() << "Zombie can not begin out of their home!" << endl;
}
(*initplace)->setPway(way1.mid(5));
initplace = allplace.find(way2.mid(0,5));
if (initplace == allplace.end()) {
qDebug() << "Zombie can not begin out of their home!" << endl;
}
(*initplace)->setPway(way2.mid(5));
}
void GameState::addCreature(Creature* crt, QString posi)
{
placeMap::iterator addplace = allplace.find(posi);
if (addplace == allplace.end()) {
qDebug() << "wrong place position!" << endl;
}
else {
if (canadd(crt, *addplace)) {
if (crt->getIs_plant()) {
if ((*addplace)->haveplant()) {
qDebug() << "You can't add two plant in the same place!";
}
else {
(*addplace)->add_creature(crt);
allplant.push_back(crt);
qDebug() << QString("A %1 is planted!").arg(crt->getName());
resource -= crt->getCost();
}
}
else {
(*addplace)->add_creature(crt);
qDebug() << QString("A %1 is appeared!").arg(crt->getName());
allzombie.push_back(crt);
}
}
else {
qDebug() << "Add wrong type creature to the place!";
}
}
}
void GameState::actionInARoll()
{
roll++;
computerAction();
actionForPlant();
actionForZombie();
removeDead();
moveForward();
reportGameState();
GameOver();
qDebug() << endl;
}
void GameState::actionForPlant()
{
for (Crtvector::iterator it = allplant.begin(); it != allplant.end(); it++) {
attackZombie(*it);
}
}
void GameState::attackZombie(Creature* crt)
{
for (Crtvector::iterator it = allzombie.begin(); it != allzombie.end(); it++) {
if (crt->canAttack(*it)) {
(*it)->hPDecrease(crt->getDamage());
break;
}
}
}
void GameState::actionForZombie()
{
for (Crtvector::iterator it = allzombie.begin(); it != allzombie.end(); it++) {
attackPlant(*it);
}
}
void GameState::attackPlant(Creature* crt)
{
for (Crtvector::iterator it = allplant.begin(); it != allplant.end(); it++) {
if (crt->canAttack(*it)) {
(*it)->hPDecrease(crt->getDamage());
break;
}
}
}
QString GameState::nextPlace(Creature *zom) {
int x = zom->getCx(), y = zom->getCy();
QChar a = zom->getWay()[0];
if (a == 'N') {
x = floor(x);
}
else if (a == 'W') {
y = floor(y);
}
else if (a == 'S') {
x = ceil(x);
}
else if (a == 'E') {
y = ceil(y);
}
else {
qDebug() << "Wrong Direction!!!";
}
QString nextPosition = QString("(%1,%2)").arg(x).arg(y);
placeMap::iterator it = allplace.find(nextPosition);
if (it == allplace.end()) {
qDebug() << "The Place in way don't exist!";
}
return nextPosition;
}
bool GameState::canMove(Creature* zom) {
bool result;
placeMap::iterator it = allplace.find(nextPlace(zom));
bool nextplant = (*it)->haveplant();
if (!nextplant) {
result = true;
}
else if (zom->getIs_fly()) {
if (zom->getWarlike()) {
result = false;
}
else {
result = true;
}
}
else {
result = false;
}
return result;
}
void GameState::moveForward()
{
for (Crtvector::iterator it = allzombie.begin(); it != allzombie.end() ;it++) {
if (canMove(*it)) {
qDebug() << QString("%1 in (%2,%3) move forward!!!").arg((*it)->getName()).arg((*it)->getCx()).arg((*it)->getCy());
(*it)->zombieMove();
if ((*it)->getWay().isEmpty()) {
gameover = true;
}
}
}
}
void GameState::removeDead()
{
for (Crtvector::iterator it = allplant.begin(); it != allplant.end(); ) {
if ((*it)->getHP() <= 0) {
qDebug() << QString("%1 has dead").arg((*it)->getName());
int x = (*it)->getCx(), y = (*it)->getCy();
QString position = QString("(%1,%2)").arg(x).arg(y);
placeMap::iterator dplace = allplace.find(position);
(*dplace)->setPlantHere(nullptr);
delete (*it);
(*it) = nullptr;
it = allplant.erase(it);
}
else {
it++;
}
}
for (Crtvector::iterator it = allzombie.begin(); it != allzombie.end(); ) {
if ((*it)->getHP() <= 0) {
qDebug() << QString("%1 has dead").arg((*it)->getName());
delete (*it);
(*it) = nullptr;
it = allzombie.erase(it);
}
else {
it++;
}
}
}
void GameState::GameOver()
{
if (gameover) {
qDebug() << "Game Over!";
}
}
void GameState::reportGameState()
{
qDebug() << endl;
qDebug() << "Roll:" << roll;
qDebug() << QString("Resource:%1").arg(resource);
for (Crtvector::iterator it = allplant.begin(); it != allplant.end(); it++) {
qDebug() << QString("%1 in (%2,%3) HP:%4").arg((*it)->getName())
.arg((*it)->getCx()).arg((*it)->getCy()).arg((*it)->getHP());
}
for (Crtvector::iterator it = allzombie.begin(); it != allzombie.end(); it++) {
qDebug() << QString("%1 in (%2,%3) HP:%4").arg((*it)->getName())
.arg((*it)->getCx()).arg((*it)->getCy()).arg((*it)->getHP());
}
}
Place* GameState::findplace(QString posi) {
return allplace[posi];
}
void GameState::computerAction() {
if (roll % 20 == 0) {
NormalZombie* a2;
Ballon *b2;
a2 = new NormalZombie;
b2 = new Ballon;
addCreature(b2, "(1,7)");
addCreature(a2, "(1,8)");
}
}