-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmove.cpp
370 lines (351 loc) · 9.67 KB
/
move.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
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
extern int field[11][10];
extern int record[300];
extern int chase_record[300]; //用于记录是否有捉着,以便判断长捉
extern int turns;
extern vector<int> valid_moves; /*合法走子方法集合。
走子方法用一个6位整数表示:前2位为棋子类型代号,
3-4位为起始点坐标,5-6位为走子后坐标,
第一位可能是空位;坐标为“10”的记为“0”。*/
struct pos{ //位置结构
public:
int i;
int j;
pos(int i_ = 0, int j_ = 0): i(i_), j(j_){};
friend bool operator == (const pos& x, const pos& y){
return (x.i == y.i) && (x.j == y.j) && (x.i != 0) && (x.j != 0);
};
};
inline int get_color(int id){ //参数:兵种代号
if(!id)
return -1; //-1无子
if(id < 10)
return 1; //1红
return 0; //0黑
}
inline int valid_pos(int i, int j){
if(i > 0 && i <= 10 && j > 0 && j <= 9)
return 1;
return 0;
}
bool bailianjiang(){ //白脸将
int black_i = 0, black_j = 0;
for(int i = 1; i <= 3; ++i){
for(int j = 4; j <= 6; ++j){
if(field[i][j] == 11){
black_i = i;
black_j = j;
}
}
}
int red_i = 0, red_j = 0;
for(int i = 8; i <= 10; ++i){
for(int j = 4; j <= 6; ++j){
if(field[i][j] == 1){
red_i = i;
red_j = j;
}
}
}
if(red_j != black_j)
return 0;
for(int i = black_i + 1; i < red_i; ++i){
if(field[i][red_j])
return 0;
}
return 1;
}
int recur(int director, bool king){ //循环着法,例如长将,长捉。此函数有缺陷。
if (king)
return 0;
if(turns > 16){
if((record[turns - 1] == record[turns - 5])
//&& (record[turns - 1] == record[turns - 9])
&& (record[turns - 2] == record[turns - 6])
//&& (record[turns - 2] == record[turns - 10])
&& (record[turns - 3] == record[turns - 7])
//&& (record[turns - 3] == record[turns - 11])
&& (director == record[turns - 4]))
//&& (director == record[turns - 8]))
return 1;
else
return 0;
}
else
return 0;
}
int push(int color, int type, int from_i, int from_j, int to_i, int to_j, bool king, bool check_long_chase){
int move_with_info(int director);
int move(int director);
if(valid_pos(to_i,to_j) && get_color(field[to_i][to_j]) != color){
int save[11][10];
for(int i = 0; i <= 10; ++i){
for(int j = 0; j <= 9; ++j)
save[i][j] = field[i][j];
}
int director = (from_i << 12) + (from_j << 8) + (to_i << 4) + to_j;
move(director);
bool invalid = 0;
if(bailianjiang())
invalid = 1;
for(int i = 0; i <= 10; ++i){
for(int j = 0; j <= 9; ++j)
field[i][j] = save[i][j];
}
if(!invalid){
if(recur(director, king))
return -1;
if ((check_long_chase) && (turns > 18)){
if (chase_record[turns - 2] && chase_record[turns - 4] &&
chase_record[turns - 6] && chase_record[turns - 8] &&
chase_record[turns - 10] && chase_record[turns - 12] &&
chase_record[turns - 14]) {
if ((move_with_info(director) == 24) && (!king)) {
for (int i = 0; i <= 10; ++i) {
for (int j = 0; j <= 9; ++j)
field[i][j] = save[i][j];
}
return -1;
}
for (int i = 0; i <= 10; ++i) {
for (int j = 0; j <= 9; ++j)
field[i][j] = save[i][j];
}
}
}
valid_moves.push_back(director);
}
return invalid;
}
return -1;
}
void king(int i, int j, int color, bool check_long_chase){
int di[4] = {-1,0,1,0};
int dj[4] = {0,1,0,-1};
for(int k = 0; k < 4; ++k){
int to_i = i + di[k], to_j = j + dj[k];
if(to_i > 3 && to_i < 8 || to_j < 4 || to_j > 6)
continue;
push(color,1,i,j,to_i,to_j,1,check_long_chase);
}
return;
}
void guard(int i, int j, int color, bool check_long_chase){
int di[4] = {-1,-1,1,1};
int dj[4] = {-1,1,1,-1};
for(int k = 0; k < 4; ++k){
int to_i = i + di[k], to_j = j + dj[k];
if(to_i > 3 && to_i < 8 || to_j < 4 || to_j > 6)
continue;
push(color,2,i,j,to_i,to_j,0,check_long_chase);
}
return;
}
void bishop(int i, int j, int color, bool check_long_chase){
int di[4] = {-2,-2,2,2};
int dj[4] = {-2,2,2,-2};
int obi[4] = {-1,-1,1,1}; //obstruct
int obj[4] = {-1,1,1,-1};
for(int k = 0; k < 4; ++k){
int to_i = i + di[k], to_j = j + dj[k];
int by_i = i + obi[k], by_j = j + obj[k]; //come by
if(valid_pos(by_i,by_j) && !field[by_i][by_j]){
if(color && to_i > 5 || !color && to_i < 6)
push(color,3,i,j,to_i,to_j,0, check_long_chase);
}
}
return;
}
void rook(int i, int j, int color, bool check_long_chase){
int di[4] = {-1,0,1,0};
int dj[4] = {0,1,0,-1};
for(int k = 0; k < 4; ++k){
int to_i = i + di[k], to_j = j + dj[k];
while(valid_pos(to_i,to_j)){
if(get_color(field[to_i][to_j]) == color)
break;
if(get_color(field[to_i][to_j]) == 1 - color){
push(color,4,i,j,to_i,to_j,0, check_long_chase);
break;
}
push(color,4,i,j,to_i,to_j,0, check_long_chase);
to_i += di[k];
to_j += dj[k];
}
}
return;
}
void knight(int i, int j, int color, bool check_long_chase){
int di[8] = {-2,-1,1,2,2,1,-1,-2};
int dj[8] = {1,2,2,1,-1,-2,-2,-1};
int obi[8] = {-1,0,0,1,1,0,0,-1}; //obstruct
int obj[8] = {0,1,1,0,0,-1,-1,0};
for(int k = 0; k < 8; ++k){
int to_i = i + di[k], to_j = j + dj[k];
int by_i = i + obi[k], by_j = j + obj[k]; //come by
if(valid_pos(by_i,by_j) && !field[by_i][by_j])
push(color,5,i,j,to_i,to_j,0, check_long_chase);
}
return;
}
void cannon(int i, int j, int color, bool check_long_chase){
int di[4] = {-1,0,1,0};
int dj[4] = {0,1,0,-1};
for(int k = 0; k < 4; ++k){
int to_i = i + di[k], to_j = j + dj[k];
int by = 0; //come by
while(valid_pos(to_i,to_j)){
if(by == 0){
if(get_color(field[to_i][to_j]) == -1)
push(color,6,i,j,to_i,to_j,0, check_long_chase);
else
by = 1;
}
else if(by == 1){
if(get_color(field[to_i][to_j]) == 1 - color){
push(color,6,i,j,to_i,to_j,0, check_long_chase);
break;
}
else if(get_color(field[to_i][to_j]) == color)
break;
}
to_i += di[k];
to_j += dj[k];
}
}
return;
}
void pawn(int i, int j, int color, bool check_long_chase){
if(color){
if(i > 5)
push(color,7,i,j,i - 1,j,0, check_long_chase);
else{
push(color,7,i,j,i - 1,j,0, check_long_chase);
push(color,7,i,j,i,j + 1,0, check_long_chase);
push(color,7,i,j,i,j - 1,0, check_long_chase);
}
}
else{
if(i < 6)
push(color,7,i,j,i + 1,j,0, check_long_chase);
else{
push(color,7,i,j,i + 1,j,0, check_long_chase);
push(color,7,i,j,i,j + 1,0, check_long_chase);
push(color,7,i,j,i,j - 1,0, check_long_chase);
}
}
return;
}
int get_valid_moves(int color, bool check_long_chase = 1){ //参数:1红;0黑
valid_moves.clear();
for(int i = 1; i <= 10; ++i){
for(int j = 1; j <= 9; ++j){
if(field[i][j]){
if(color == get_color(field[i][j])){
int type = field[i][j] % 10;
switch(type){
case 1: king(i,j,color, check_long_chase); break;
case 2: guard(i,j,color, check_long_chase); break;
case 3: bishop(i,j,color, check_long_chase); break;
case 4: rook(i,j,color, check_long_chase); break;
case 5: knight(i,j,color, check_long_chase); break;
case 6: cannon(i,j,color, check_long_chase); break;
case 7: pawn(i,j,color, check_long_chase); break;
}
}
}
}
}
return 0;
}
int move(int director){ //参数:走子方法代号,返回吃子代号;
int i = 4;
int dgt[4] = {0, 0, 0, 0};
while(i){
--i;
dgt[i] = (director >> ((3 - i) << 2)) & 15;
}
int ret = field[dgt[2]][dgt[3]]; //吃子代号
field[dgt[2]][dgt[3]] = field[dgt[0]][dgt[1]];
field[dgt[0]][dgt[1]] = 0;
return ret;
}
int move_with_info(int director) { //参数:走子方法代号,返回吃子代号;不吃子但是捉子,返回代号20
int i = 4;
int dgt[4] = { 0, 0, 0, 0 };
while (i) {
--i;
dgt[i] = (director >> ((3 - i) << 2)) & 15;
}
int ret = field[dgt[2]][dgt[3]]; //吃子代号
field[dgt[2]][dgt[3]] = field[dgt[0]][dgt[1]];
field[dgt[0]][dgt[1]] = 0;
if (!ret) { //未吃子,判断是否捉子
//保存棋局
int save[11][10];
for (int i = 0; i < 11; ++i) {
for (int j = 1; j < 10; ++j)
save[i][j] = field[i][j];
}
vector<int> save_valid_moves = valid_moves;
valid_moves.clear();
//遍历延伸走子
switch (field[dgt[2]][dgt[3]]) {
case 1: king(dgt[2], dgt[3], 1, 0); break;
case 2: guard(dgt[2], dgt[3], 1, 0); break;
case 3: bishop(dgt[2], dgt[3], 1, 0); break;
case 4: rook(dgt[2], dgt[3], 1, 0); break;
case 5: knight(dgt[2], dgt[3], 1, 0); break;
case 6: cannon(dgt[2], dgt[3], 1, 0); break;
case 7: pawn(dgt[2], dgt[3], 1, 0); break;
case 11: king(dgt[2], dgt[3], 0, 0); break;
case 12: guard(dgt[2], dgt[3], 0, 0); break;
case 13: bishop(dgt[2], dgt[3], 0, 0); break;
case 14: rook(dgt[2], dgt[3], 0, 0); break;
case 15: knight(dgt[2], dgt[3], 0, 0); break;
case 16: cannon(dgt[2], dgt[3], 0, 0); break;
case 17: pawn(dgt[2], dgt[3], 0, 0); break;
}
//遍历延伸走子
while (!valid_moves.empty()) {
int director = valid_moves.back();
valid_moves.pop_back();
if (move(director)) {
ret = 20;
break;
} //不回溯成原始棋局,最后统一回溯
}
//回溯棋局
for (int i = 0; i < 11; ++i) {
for (int j = 1; j < 10; ++j)
field[i][j] = save[i][j];
}
valid_moves = save_valid_moves;
}
return ret;
}
int player_move(int cmd){
int i = 4;
int dgt[4] = {0, 0, 0, 0};
bool b = 0;
if(cmd < 1000)
b = 1;
while(i){
--i;
dgt[i] = cmd % 10;
cmd -= dgt[i];
cmd /= 10;
if(!dgt[i])
dgt[i] = 10;
}
if(b)
dgt[0] = 10;
int color = get_color(field[dgt[0]][dgt[1]]);
int type = field[dgt[0]][dgt[1]] % 10;
int director = (dgt[0] << 12) + (dgt[1] << 8) + (dgt[2] << 4) + dgt[3];
return director;
}