-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha.cpp
627 lines (542 loc) · 18.1 KB
/
a.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Tank 裁判程序
// 作者:zhouhy
// https://www.botzone.org.cn/games/Tank
#include "Tools/json.hpp"
#include <cstring>
#include <ctime>
#include <iostream>
#include <set>
#include <stack>
#include <string>
using json = nlohmann::json;
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::string;
namespace TankGame {
using std::istream;
using std::set;
using std::stack;
enum GameResult { NotFinished = -2, Draw = -1, Blue = 0, Red = 1 };
enum FieldItem {
None = 0,
Brick = 1,
Steel = 2,
Base = 4,
Blue0 = 8,
Blue1 = 16,
Red0 = 32,
Red1 = 64
};
template <typename T> inline T operator~(T a) { return (T) ~(int)a; }
template <typename T> inline T operator|(T a, T b) {
return (T)((int)a | (int)b);
}
template <typename T> inline T operator&(T a, T b) {
return (T)((int)a & (int)b);
}
template <typename T> inline T operator^(T a, T b) {
return (T)((int)a ^ (int)b);
}
template <typename T> inline T &operator|=(T &a, T b) {
return (T &)((int &)a |= (int)b);
}
template <typename T> inline T &operator&=(T &a, T b) {
return (T &)((int &)a &= (int)b);
}
template <typename T> inline T &operator^=(T &a, T b) {
return (T &)((int &)a ^= (int)b);
}
enum Action {
Invalid = -2,
Stay = -1,
Up,
Right,
Down,
Left,
UpShoot,
RightShoot,
DownShoot,
LeftShoot
};
// 坐标左上角为原点(0, 0),x 轴向右延伸,y 轴向下延伸
// Side(对战双方) - 0 为蓝,1 为红
// Tank(每方的坦克) - 0 为 0 号坦克,1 为 1 号坦克
// Turn(回合编号) - 从 1 开始
const int fieldHeight = 9, fieldWidth = 9, sideCount = 2, tankPerSide = 2;
// 基地的横坐标
const int baseX[sideCount] = {fieldWidth / 2, fieldWidth / 2};
// 基地的纵坐标
const int baseY[sideCount] = {0, fieldHeight - 1};
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const FieldItem tankItemTypes[sideCount][tankPerSide] = {{Blue0, Blue1},
{Red0, Red1}};
int maxTurn = 100;
inline bool ActionIsMove(Action x) { return x >= Up && x <= Left; }
inline bool ActionIsShoot(Action x) { return x >= UpShoot && x <= LeftShoot; }
inline bool ActionDirectionIsOpposite(Action a, Action b) {
return a >= Up && b >= Up && (a + 2) % 4 == b % 4;
}
inline bool CoordValid(int x, int y) {
return x >= 0 && x < fieldWidth && y >= 0 && y < fieldHeight;
}
// 判断 item 是不是叠在一起的多个坦克
inline bool HasMultipleTank(FieldItem item) {
// 如果格子上只有一个物件,那么 item 的值是 2 的幂或 0
// 对于数字 x,x & (x - 1) == 0 当且仅当 x 是 2 的幂或 0
return !!(item & (item - 1));
}
inline int GetTankSide(FieldItem item) {
return item == Blue0 || item == Blue1 ? Blue : Red;
}
inline int GetTankID(FieldItem item) {
return item == Blue0 || item == Red0 ? 0 : 1;
}
// 获得动作的方向
inline int ExtractDirectionFromAction(Action x) {
if (x >= Up)
return x % 4;
return -1;
}
// 物件消失的记录,用于回退
struct DisappearLog {
FieldItem item;
// 导致其消失的回合的编号
int turn;
int x, y;
bool operator<(const DisappearLog &b) const {
if (x == b.x) {
if (y == b.y)
return item < b.item;
return y < b.y;
}
return x < b.x;
}
};
class TankField {
public:
//!//!//!// 以下变量设计为只读,不推荐进行修改 //!//!//!//
// 游戏场地上的物件(一个格子上可能有多个坦克)
FieldItem gameField[fieldHeight][fieldWidth] = {};
// 坦克是否存活
bool tankAlive[sideCount][tankPerSide] = {{true, true}, {true, true}};
// 基地是否存活
bool baseAlive[sideCount] = {true, true};
// 坦克横坐标,-1表示坦克已炸
int tankX[sideCount][tankPerSide] = {
{fieldWidth / 2 - 2, fieldWidth / 2 + 2},
{fieldWidth / 2 + 2, fieldWidth / 2 - 2}};
// 坦克纵坐标,-1表示坦克已炸
int tankY[sideCount][tankPerSide] = {{0, 0},
{fieldHeight - 1, fieldHeight - 1}};
// 当前回合编号
int currentTurn = 0;
// 我是哪一方
int mySide;
// 用于回退的log
stack<DisappearLog> logs;
// 过往动作(previousActions[x] 表示所有人在第 x 回合的动作,第 0
// 回合的动作没有意义)
Action previousActions[101][sideCount][tankPerSide] = {
{{Stay, Stay}, {Stay, Stay}}};
//!//!//!// 以上变量设计为只读,不推荐进行修改 //!//!//!//
// 本回合双方即将执行的动作,需要手动填入
Action nextAction[sideCount][tankPerSide] = {{Invalid, Invalid},
{Invalid, Invalid}};
// 判断行为是否合法(出界或移动到非空格子算作非法)
// 未考虑坦克是否存活
bool ActionIsValid(int side, int tank, Action act) {
if (act == Invalid)
return false;
if (act > Left &&
previousActions[currentTurn - 1][side][tank] > Left) // 连续两回合射击
return false;
if (act == Stay || act > Left)
return true;
int x = tankX[side][tank] + dx[act], y = tankY[side][tank] + dy[act];
return CoordValid(x, y) && gameField[y][x] == None;
}
// 判断 nextAction 中的所有行为是否都合法
// 忽略掉未存活的坦克
bool ActionIsValid() {
for (int side = 0; side < sideCount; side++)
for (int tank = 0; tank < tankPerSide; tank++)
if (tankAlive[side][tank] &&
!ActionIsValid(side, tank, nextAction[side][tank]))
return false;
return true;
}
private:
void _destroyTank(int side, int tank) {
tankAlive[side][tank] = false;
tankX[side][tank] = tankY[side][tank] = -1;
}
public:
// 执行 nextAction 中指定的行为并进入下一回合,返回行为是否合法
bool DoAction() {
if (!ActionIsValid())
return false;
// 1 移动
for (int side = 0; side < sideCount; side++)
for (int tank = 0; tank < tankPerSide; tank++) {
Action act = nextAction[side][tank];
// 保存动作
previousActions[currentTurn][side][tank] = act;
if (tankAlive[side][tank] && ActionIsMove(act)) {
int &x = tankX[side][tank], &y = tankY[side][tank];
FieldItem &items = gameField[y][x];
// 记录 Log
DisappearLog log;
log.x = x;
log.y = y;
log.item = tankItemTypes[side][tank];
log.turn = currentTurn;
logs.push(log);
// 变更坐标
x += dx[act];
y += dy[act];
// 更换标记(注意格子可能有多个坦克)
gameField[y][x] |= log.item;
items &= ~log.item;
}
}
// 2 射♂击
set<DisappearLog> itemsToBeDestroyed;
for (int side = 0; side < sideCount; side++)
for (int tank = 0; tank < tankPerSide; tank++) {
Action act = nextAction[side][tank];
if (tankAlive[side][tank] && ActionIsShoot(act)) {
int dir = ExtractDirectionFromAction(act);
int x = tankX[side][tank], y = tankY[side][tank];
bool hasMultipleTankWithMe = HasMultipleTank(gameField[y][x]);
while (true) {
x += dx[dir];
y += dy[dir];
if (!CoordValid(x, y))
break;
FieldItem items = gameField[y][x];
if (items != None) {
// 对射判断
if (items >= Blue0 && !hasMultipleTankWithMe &&
!HasMultipleTank(items)) {
// 自己这里和射到的目标格子都只有一个坦克
Action theirAction =
nextAction[GetTankSide(items)][GetTankID(items)];
if (ActionIsShoot(theirAction) &&
ActionDirectionIsOpposite(act, theirAction)) {
// 而且我方和对方的射击方向是反的
// 那么就忽视这次射击
break;
}
}
// 标记这些物件要被摧毁了(防止重复摧毁)
for (int mask = 1; mask <= Red1; mask <<= 1)
if (items & mask) {
DisappearLog log;
log.x = x;
log.y = y;
log.item = (FieldItem)mask;
log.turn = currentTurn;
itemsToBeDestroyed.insert(log);
}
break;
}
}
}
}
for (auto &log : itemsToBeDestroyed) {
switch (log.item) {
case Base: {
int side = log.x == baseX[Blue] && log.y == baseY[Blue] ? Blue : Red;
baseAlive[side] = false;
break;
}
case Blue0:
_destroyTank(Blue, 0);
break;
case Blue1:
_destroyTank(Blue, 1);
break;
case Red0:
_destroyTank(Red, 0);
break;
case Red1:
_destroyTank(Red, 1);
break;
case Steel:
continue;
default:;
}
gameField[log.y][log.x] &= ~log.item;
logs.push(log);
}
for (int side = 0; side < sideCount; side++)
for (int tank = 0; tank < tankPerSide; tank++)
nextAction[side][tank] = Invalid;
return true;
}
// 游戏是否结束?谁赢了?
GameResult GetGameResult() {
bool fail[sideCount] = {};
for (int side = 0; side < sideCount; side++)
if ((!tankAlive[side][0] && !tankAlive[side][1]) || !baseAlive[side])
fail[side] = true;
if (fail[0] == fail[1])
return fail[0] || currentTurn > maxTurn ? Draw : NotFinished;
if (fail[Blue])
return Red;
return Blue;
}
// 三个 int 表示场地 01 矩阵(每个 int 用 27 位表示 3 行)
TankField(int hasBrick[3], int mySide) : mySide(mySide) {
for (int i = 0; i < 3; i++) {
int mask = 1;
for (int y = i * 3; y < (i + 1) * 3; y++) {
for (int x = 0; x < fieldWidth; x++) {
if (hasBrick[i] & mask)
gameField[y][x] = Brick;
mask <<= 1;
}
}
}
for (int side = 0; side < sideCount; side++) {
for (int tank = 0; tank < tankPerSide; tank++)
gameField[tankY[side][tank]][tankX[side][tank]] =
tankItemTypes[side][tank];
gameField[baseY[side]][baseX[side]] = Base;
}
gameField[baseY[0] + 1][baseX[0]] = gameField[baseY[1] - 1][baseX[1]] =
Steel;
}
// 打印场地
void DebugPrint() {
// #ifndef _BOTZONE_ONLINE
// const string side2String[] = {"蓝", "红"};
// const string boolean2String[] = {"已炸", "存活"};
// const char *boldHR = "==============================";
// const char *slimHR = "------------------------------";
// cout << boldHR << endl
// << "图例:" << endl
// << ". - 空\t# - 砖\t% - 钢\t* - 基地\t@ - 多个坦克" << endl
// << "b - 蓝0\tB - 蓝1\tr - 红0\tR - 红1" << endl
// << slimHR << endl;
// for (int y = 0; y < fieldHeight; y++) {
// for (int x = 0; x < fieldWidth; x++) {
// switch (gameField[y][x]) {
// case None:
// cout << '.';
// break;
// case Brick:
// cout << '#';
// break;
// case Steel:
// cout << '%';
// break;
// case Base:
// cout << '*';
// break;
// case Blue0:
// cout << 'b';
// break;
// case Blue1:
// cout << 'B';
// break;
// case Red0:
// cout << 'r';
// break;
// case Red1:
// cout << 'R';
// break;
// default:
// cout << '@';
// break;
// }
// }
// cout << endl;
// }
// cout << slimHR << endl;
// for (int side = 0; side < sideCount; side++) {
// cout << side2String[side] << ":基地" <<
// boolean2String[baseAlive[side]]; for (int tank = 0; tank <
// tankPerSide; tank++)
// cout << ", 坦克" << tank <<
// boolean2String[tankAlive[side][tank]];
// cout << endl;
// }
// cout << "当前回合:" << currentTurn << ",";
// GameResult result = GetGameResult();
// if (result == -2)
// cout << "游戏尚未结束" << endl;
// else if (result == -1)
// cout << "游戏平局" << endl;
// else
// cout << side2String[result] << "方胜利" << endl;
// cout << boldHR << endl;
// #endif
}
bool operator!=(const TankField &b) const {
for (int y = 0; y < fieldHeight; y++)
for (int x = 0; x < fieldWidth; x++)
if (gameField[y][x] != b.gameField[y][x])
return true;
for (int side = 0; side < sideCount; side++)
for (int tank = 0; tank < tankPerSide; tank++) {
if (tankX[side][tank] != b.tankX[side][tank])
return true;
if (tankY[side][tank] != b.tankY[side][tank])
return true;
if (tankAlive[side][tank] != b.tankAlive[side][tank])
return true;
}
if (baseAlive[0] != b.baseAlive[0] || baseAlive[1] != b.baseAlive[1])
return true;
if (currentTurn != b.currentTurn)
return true;
return false;
}
};
TankField *field;
} // namespace TankGame
namespace TankJudge {
using namespace TankGame;
int fieldBinary[3];
bool visited[fieldHeight][fieldWidth];
void InitializeField() {
memset(visited, 0, sizeof(visited));
bool hasBrick[fieldHeight][fieldWidth] = {};
int portionH = (fieldHeight + 1) / 2;
for (int y = 0; y < portionH; y++)
for (int x = 0; x < fieldWidth; x++)
hasBrick[y][x] = rand() % 3 > 1;
int bx = baseX[0], by = baseY[0];
hasBrick[by + 1][bx + 1] = hasBrick[by + 1][bx - 1] = hasBrick[by][bx + 1] =
hasBrick[by][bx - 1] = true;
hasBrick[by][bx] = hasBrick[by + 1][bx] = hasBrick[by][bx + 2] =
hasBrick[by][bx - 2] = false;
for (int y = 0; y < portionH; y++)
for (int x = 0; x < fieldWidth; x++)
hasBrick[fieldHeight - y - 1][fieldWidth - x - 1] = hasBrick[y][x];
for (int y = 2; y < fieldHeight - 2; y++)
hasBrick[y][fieldWidth / 2] = true;
for (int x = 0; x < fieldWidth; x++)
hasBrick[fieldHeight / 2][x] = true;
for (int i = 0; i < 3; i++) {
int mask = 1;
for (int y = i * 3; y < (i + 1) * 3; y++) {
for (int x = 0; x < fieldWidth; x++) {
if (hasBrick[y][x])
fieldBinary[i] |= mask;
mask <<= 1;
}
}
}
}
} // namespace TankJudge
int main() {
unsigned int seed;
const string int2str[] = {"0", "1"};
json output;
srand(seed = time(nullptr));
TankJudge::InitializeField();
TankGame::field = new TankGame::TankField(TankJudge::fieldBinary, 0);
for (int side = 0; side < TankGame::sideCount; side++) {
std::stringstream ss;
for (int i = 0; i < 3; i++) {
ss << (i == 0 ? "" : " ") << TankJudge::fieldBinary[i];
}
output["content"][int2str[side]] = ss.str();
}
output["command"] = "request";
output["display"] = json(std::vector(std::begin(TankJudge::fieldBinary),
std::end(TankJudge::fieldBinary)))
.dump();
cout << output << std::endl;
TankGame::field->DebugPrint();
bool invalid[TankGame::sideCount] = {};
auto setWinner = [&](int to) {
if (to == -1)
output["content"]["0"] = output["content"]["1"] = 1;
else if (to == 1) {
output["content"]["0"] = 0;
output["content"]["1"] = 2;
} else {
output["content"]["0"] = 2;
output["content"]["1"] = 0;
}
};
for (;;) {
TankGame::field->currentTurn++;
output = json();
output["command"] = "request";
json to_judger;
cin >> to_judger;
int result = -2;
for (int side = 0; side < TankGame::sideCount; side++) {
json response = to_judger[int2str[side]];
string raw = response["raw"].get<string>();
TankGame::Action act0, act1;
int i0, i1;
std::stringstream ss;
ss << raw;
ss >> i0 >> i1;
act0 = (TankGame::Action)i0;
act1 = (TankGame::Action)i1;
if (!TankGame::field->tankAlive[side][0] ||
!TankGame::field->ActionIsValid(side, 0, act0))
act0 = TankGame::Action::Invalid;
if (!TankGame::field->tankAlive[side][1] ||
!TankGame::field->ActionIsValid(side, 1, act1))
act1 = TankGame::Action::Invalid;
string action = std::to_string(act0) + " " + std::to_string(act1);
output["display"][int2str[side]] = output["content"][int2str[1 - side]] =
action;
if ((!TankGame::field->tankAlive[side][0] ||
TankGame::field->ActionIsValid(side, 0, act0)) &&
(!TankGame::field->tankAlive[side][1] ||
TankGame::field->ActionIsValid(side, 1, act1))) {
TankGame::field->nextAction[side][0] = act0;
TankGame::field->nextAction[side][1] = act1;
continue;
}
invalid[side] = true;
output["display"]["loseReason"][side] =
"INVALID_INPUT_VERDICT_" + response["verdict"].get<string>();
}
if (invalid[0] || invalid[1]) {
output["command"] = "finish";
if (invalid[0] == invalid[1])
setWinner(-1);
else if (invalid[0])
setWinner(1);
else
setWinner(0);
goto ed;
}
TankGame::field->DoAction();
result = TankGame::field->GetGameResult();
if (result != -2) {
output["command"] = "finish";
setWinner(result);
for (int side = 0; side < TankGame::sideCount; side++) {
bool tankExist = TankGame::field->tankAlive[side][0] ||
TankGame::field->tankAlive[side][1];
bool baseExist = TankGame::field->baseAlive[side];
if (!tankExist && !baseExist)
output["display"]["loseReason"][side] = "BASE_TANK_ALL_DESTROYED";
else if (!tankExist)
output["display"]["loseReason"][side] = "TANK_ALL_DESTROYED";
else if (!baseExist)
output["display"]["loseReason"][side] = "BASE_DESTROYED";
}
goto ed;
}
ed:;
output["display"] = output["display"].dump();
cout << output << endl;
TankGame::field->DebugPrint();
if (output["command"] == "finish") {
break;
}
}
}