-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2885.cpp
412 lines (380 loc) · 11.3 KB
/
2885.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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::ios;
#define CANNOT_USE_THIS_CARD true
#define CAN_USE_THIS_CARD false
#define THIS_PIG_IS_DEAD true
#define THIS_PIG_IS_NOT_DEAD false
#define THIS_PIG_IS_JUMP true
#define THIS_PIG_IS_NOT_JUMP false
#define THIS_PIG_LIKE_BAD true
#define THIS_PIG_DO_NOT_LIKE_BAD false
#define CROSSBOW_ON true
#define CROSSBOW_DOWN false
#define THIS_TURN_USED_KILL true
#define THIS_TURN_DO_NOT_USE_KILL false
struct card {
char which;
bool use;
};
typedef std::vector<card>::iterator myit;
class pig {
private:
int hp;
bool use_kill;
bool crossbow;
bool jumped;
bool like_bad;
void die(int);
void clear(void);
void del(myit&);
myit find(char);
void cut(void);
void use(card&);
void hurt(int);
bool canuse(char);
int find_K(void);
int find_F(void);
bool K_respond(void);
bool N_respond(void);
bool W_respond(void);
bool ask_J(int);
bool back_J(int);
public:
pig(void);
int num;
bool dead;
std::string name;
std::vector<card> cards;
void myturn(void);
void jump(void);
void K(int);
void F(int);
void N(void);
void W(void);
};
void gameover(std::string);
int n, m;
int FP;
card stack[2005];
pig member[15];
int top = 1;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (register int i = 1; i <= n; i++) {
cin >> member[i].name;
member[i].num = i;
if (member[i].name == "FP") FP++;
for (register int j = 1; j <= 4; j++) {
card c;
cin >> c.which;
c.use = CAN_USE_THIS_CARD;
member[i].cards.push_back(c);
}
}
for (top = 1; top <= m; top++) {
card c;
cin >> c.which;
c.use = CAN_USE_THIS_CARD;
stack[top] = c;
}
top = 1;
int point = 0;
member[1].jump();
while (true) {
point = point % n + 1;
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
member[point].myturn();
}
}
void gameover(std::string who) {
cout << who << std::endl;
for (register int i = 1; i <= n; i++) {
if (member[i].dead == THIS_PIG_IS_DEAD)
cout << "DEAD";
else
for (myit it = member[i].cards.begin(); it != member[i].cards.end(); it++) {
if (it->use == CANNOT_USE_THIS_CARD) continue;
cout << it->which << ' ';
}
cout << std::endl;
}
exit(0);
}
void pig::die(int who) {
this->dead = THIS_PIG_IS_DEAD;
if (this->name == "MP") gameover("FP");
if (this->name == "FP") FP--;
if (FP == 0) gameover("MP");
if (this->name == "FP") {
member[who].cut();
member[who].cut();
member[who].cut();
}
if (this->name == "ZP" && member[who].name == "MP") member[who].clear();
return;
}
void pig::clear(void) {
this->cards.clear();
this->crossbow = CROSSBOW_DOWN;
return;
}
void pig::del(myit& it) {
it->use = CANNOT_USE_THIS_CARD;
return;
}
myit pig::find(char which) {
for (myit it = this->cards.begin(); it != this->cards.end(); it++) {
if (it->use == CANNOT_USE_THIS_CARD) continue;
if (it->which == which) return it;
}
return this->cards.end();
}
void pig::cut(void) {
this->cards.push_back(stack[top++]);
if (top > m) top = m;
return;
}
void pig::use(card& c) {
if (c.which == 'K') {
int who;
who = this->find_K();
if (who != 0) {
c.use = CANNOT_USE_THIS_CARD;
this->K(who);
}
} else if (c.which == 'P') {
if (this->hp < 4) {
c.use = CANNOT_USE_THIS_CARD;
this->hp++;
}
} else if (c.which == 'F') {
int who;
who = this->find_F();
if (who != 0) {
c.use = CANNOT_USE_THIS_CARD;
this->F(who);
}
} else if (c.which == 'N') {
c.use = CANNOT_USE_THIS_CARD;
this->N();
} else if (c.which == 'W') {
c.use = CANNOT_USE_THIS_CARD;
this->W();
} else if (c.which == 'Z') {
c.use = CANNOT_USE_THIS_CARD;
this->crossbow = CROSSBOW_ON;
}
return;
}
void pig::hurt(int who) {
this->hp--;
if (this->hp == 0) {
myit t = this->find('P');
if (t == this->cards.end())
this->die(who);
else {
this->del(t);
this->hp++;
}
}
return;
}
bool pig::canuse(char c) {
if (c == 'P' && this->hp < 4) return true;
if ((c == 'K' && this->find_K() != 0) || (c == 'F' && this->find_F() != 0) || c == 'N' || c == 'W' || c == 'Z') return true;
return false;
}
int pig::find_K(void) {
if (this->use_kill == THIS_TURN_USED_KILL && this->crossbow == CROSSBOW_DOWN) return 0;
if (this->name == "MP")
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].like_bad == THIS_PIG_LIKE_BAD ||
(member[point].name == "FP" && member[point].jumped == THIS_PIG_IS_JUMP))
return point;
else
return 0;
}
else if (this->name == "ZP")
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].name == "FP" && member[point].jumped == THIS_PIG_IS_JUMP)
return point;
else
return 0;
}
else if (this->name == "FP")
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].name != "FP" && member[point].jumped == THIS_PIG_IS_JUMP)
return point;
else
return 0;
}
return 0;
}
int pig::find_F(void) {
if (this->name == "MP")
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].like_bad == THIS_PIG_LIKE_BAD ||
(member[point].name == "FP" && member[point].jumped == THIS_PIG_IS_JUMP))
return point;
}
else if (this->name == "ZP")
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].name == "FP" && member[point].jumped == THIS_PIG_IS_JUMP) return point;
}
else if (this->name == "FP")
return 1;
return 0;
}
bool pig::K_respond(void) {
myit it = this->find('D');
if (it == this->cards.end()) return false;
this->del(it);
return true;
}
bool pig::N_respond(void) {
myit it = this->find('K');
if (it == this->cards.end()) return false;
this->del(it);
return true;
}
bool pig::W_respond(void) {
myit it = this->find('D');
if (it == this->cards.end()) return false;
this->del(it);
return true;
}
bool pig::ask_J(int who) {
bool check = true;
if (this->jumped == THIS_PIG_IS_NOT_JUMP) return false;
for (register int point = who; point != who || check == true; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
check = false;
if (this->name != "FP" && member[point].name == "FP") continue;
if (this->name == "FP" && member[point].name != "FP") continue;
myit it = member[point].find('J');
if (it != member[point].cards.end()) {
member[point].del(it);
member[point].jump();
return !member[point].back_J(this->num);
}
}
return false;
}
bool pig::back_J(int to) {
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (this->name != "FP" && member[point].name == "ZP") continue;
if (this->name == "FP" && member[point].name == "FP") continue;
if (this->name == "ZP" && member[point].name != "FP") continue;
myit it = member[point].find('J');
if (it != member[point].cards.end()) {
member[point].del(it);
member[point].jump();
return !member[to].ask_J(point);
}
}
return false;
}
pig::pig(void) {
this->hp = 4;
this->dead = THIS_PIG_IS_NOT_DEAD;
this->jumped = THIS_PIG_IS_NOT_JUMP;
this->crossbow = CROSSBOW_DOWN;
this->like_bad = THIS_PIG_DO_NOT_LIKE_BAD;
this->use_kill = THIS_TURN_DO_NOT_USE_KILL;
return;
}
void pig::myturn(void) {
this->cut();
this->cut();
bool check = true;
while (check) {
check = false;
for (myit it = this->cards.begin(); it != this->cards.end(); it++) {
if (it->use == CANNOT_USE_THIS_CARD) continue;
if (canuse(it->which) == false) continue;
use(*it);
if (this->dead == THIS_PIG_IS_DEAD) return;
it = this->cards.begin() - 1;
check = true;
}
}
member[this->num].use_kill = THIS_TURN_DO_NOT_USE_KILL;
return;
}
void pig::jump(void) {
this->jumped = THIS_PIG_IS_JUMP;
return;
}
void pig::K(int to) {
if (this->use_kill == THIS_TURN_USED_KILL && this->crossbow == false) return;
this->jump();
this->like_bad = THIS_PIG_DO_NOT_LIKE_BAD;
this->use_kill = THIS_TURN_USED_KILL;
if (member[to].K_respond() == false) member[to].hurt(this->num);
return;
}
void pig::F(int to) {
this->jump();
this->like_bad = THIS_PIG_DO_NOT_LIKE_BAD;
if (member[to].ask_J(this->num) == true) return;
if (this->name == "MP" && member[to].name == "ZP") {
member[to].hurt(this->num);
return;
}
int who;
while (true) {
myit t;
t = member[to].find('K');
if (t == member[to].cards.end()) {
who = 2;
break;
} else
member[to].del(t);
t = this->find('K');
if (t == this->cards.end()) {
who = 1;
break;
} else
this->del(t);
}
if (who == 2) member[to].hurt(this->num);
if (who == 1) this->hurt(to);
return;
}
void pig::N(void) {
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].ask_J(this->num) == true) continue;
if (member[point].N_respond() == false) {
member[point].hurt(this->num);
if (member[point].name == "MP" && this->jumped == THIS_PIG_IS_NOT_JUMP) {
this->like_bad = THIS_PIG_LIKE_BAD;
}
}
}
return;
}
void pig::W(void) {
for (register int point = this->num % n + 1; point != this->num; point = point % n + 1) {
if (member[point].dead == THIS_PIG_IS_DEAD) continue;
if (member[point].ask_J(this->num) == true) continue;
if (member[point].W_respond() == false) {
member[point].hurt(this->num);
if (member[point].name == "MP" && this->jumped == THIS_PIG_IS_NOT_JUMP) this->like_bad = THIS_PIG_LIKE_BAD;
}
}
return;
}