回答編集履歴
1
質問者様の指摘を反映させました
answer
CHANGED
@@ -1,3 +1,303 @@
|
|
1
|
+
2018/07/09 追記しました
|
2
|
+
---
|
3
|
+
追記ここから
|
4
|
+
|
5
|
+
is_siege関数が囲まれているかを判定する関数となっています。
|
6
|
+
|
7
|
+
|
8
|
+
```c
|
9
|
+
#include <stdio.h>
|
10
|
+
|
11
|
+
#define HORIZONTAL_LINE 11 //横
|
12
|
+
#define VERTICAL_LINE 11 //縦
|
13
|
+
#define EMPTY 0
|
14
|
+
#define FU 1
|
15
|
+
#define TO 2
|
16
|
+
#define KABE 3
|
17
|
+
|
18
|
+
|
19
|
+
#define UE 0
|
20
|
+
#define SHITA 1
|
21
|
+
#define HIDARI 2
|
22
|
+
#define MIGI 3
|
23
|
+
|
24
|
+
|
25
|
+
//グローバル変数
|
26
|
+
int table[VERTICAL_LINE][HORIZONTAL_LINE] = {
|
27
|
+
{ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
|
28
|
+
{ 3, 0, 1, 0, 0, 0, 0, 0, 1, 2, 3 },
|
29
|
+
{ 3, 0, 2, 0, 1, 2, 1, 0, 1, 2, 3 },
|
30
|
+
{ 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3 },
|
31
|
+
{ 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
|
32
|
+
{ 3, 2, 1, 0, 0, 1, 1, 0, 0, 0, 3 },
|
33
|
+
{ 3, 1, 0, 0, 0, 2, 2, 1, 0, 0, 3 },
|
34
|
+
{ 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 3 },
|
35
|
+
{ 3, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3 },
|
36
|
+
{ 3, 0, 1, 2, 1, 0, 1, 2, 2, 2, 3 },
|
37
|
+
{ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
|
38
|
+
};
|
39
|
+
|
40
|
+
struct stack {
|
41
|
+
int koma;
|
42
|
+
int x;
|
43
|
+
int y;
|
44
|
+
};
|
45
|
+
|
46
|
+
#define STACK_SIZE 9
|
47
|
+
static struct stack s_stack[STACK_SIZE];
|
48
|
+
static int s_stack_top;
|
49
|
+
|
50
|
+
|
51
|
+
void init_stack(void) {
|
52
|
+
s_stack_top = 0;
|
53
|
+
}
|
54
|
+
|
55
|
+
void push(int koma, int x, int y) {
|
56
|
+
if (s_stack_top >= STACK_SIZE) {
|
57
|
+
return;
|
58
|
+
}
|
59
|
+
s_stack[s_stack_top].koma = koma;
|
60
|
+
s_stack[s_stack_top].x = x;
|
61
|
+
s_stack[s_stack_top].y = y;
|
62
|
+
s_stack_top++;
|
63
|
+
}
|
64
|
+
|
65
|
+
int pop(struct stack* s)
|
66
|
+
{
|
67
|
+
if (s_stack_top < 1) {
|
68
|
+
return 0;
|
69
|
+
}
|
70
|
+
|
71
|
+
s_stack_top--;
|
72
|
+
s->koma = s_stack[s_stack_top].koma;
|
73
|
+
s->x = s_stack[s_stack_top].x;
|
74
|
+
s->y = s_stack[s_stack_top].y;
|
75
|
+
return 1;
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
void disp_table(void)
|
81
|
+
{
|
82
|
+
int i, j;
|
83
|
+
putchar(' ');
|
84
|
+
for (i = 0; i < HORIZONTAL_LINE; i++)
|
85
|
+
{
|
86
|
+
putchar(i + 0x30);
|
87
|
+
}
|
88
|
+
putchar('\n');
|
89
|
+
for (j = 0; j < VERTICAL_LINE; j++)
|
90
|
+
{
|
91
|
+
putchar(j + 0x30);
|
92
|
+
for (i = 0; i < HORIZONTAL_LINE; i++)
|
93
|
+
{
|
94
|
+
switch (table[j][i])
|
95
|
+
{
|
96
|
+
case KABE:
|
97
|
+
putchar('*');
|
98
|
+
break;
|
99
|
+
case FU:
|
100
|
+
putchar('F');
|
101
|
+
break;
|
102
|
+
case TO:
|
103
|
+
putchar('T');
|
104
|
+
break;
|
105
|
+
default:
|
106
|
+
putchar(' ');
|
107
|
+
break;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
putchar('\n');
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
// 駒のつながりが途切れた先にあるものを調べる
|
115
|
+
int koma_probe(int koma, int x, int y, int d)
|
116
|
+
{
|
117
|
+
int placed;
|
118
|
+
if (table[y][x] == EMPTY)
|
119
|
+
{
|
120
|
+
return EMPTY;
|
121
|
+
}
|
122
|
+
if (table[y][x] == KABE)
|
123
|
+
{
|
124
|
+
return KABE;
|
125
|
+
}
|
126
|
+
if (table[y][x] != koma)
|
127
|
+
{
|
128
|
+
return table[y][x];
|
129
|
+
}
|
130
|
+
|
131
|
+
placed = 0;
|
132
|
+
switch (d)
|
133
|
+
{
|
134
|
+
case UE:
|
135
|
+
placed = koma_probe(koma, x, y - 1, d);
|
136
|
+
break;
|
137
|
+
case SHITA:
|
138
|
+
placed = koma_probe(koma, x, y + 1, d);
|
139
|
+
break;
|
140
|
+
case HIDARI:
|
141
|
+
placed = koma_probe(koma, x - 1, y, d);
|
142
|
+
break;
|
143
|
+
case MIGI:
|
144
|
+
placed = koma_probe(koma, x + 1, y, d);
|
145
|
+
break;
|
146
|
+
}
|
147
|
+
return placed;
|
148
|
+
}
|
149
|
+
|
150
|
+
|
151
|
+
// komaをたどりながらEMPTYを探す
|
152
|
+
// EMPTYを検出した=囲まれてないので取れない
|
153
|
+
// 0 - EMPTYが無かった
|
154
|
+
// 1 - EMPTYを見つけた
|
155
|
+
int empty_probe(int ff_table[][HORIZONTAL_LINE], int koma, int x, int y) {
|
156
|
+
if (ff_table[y][x] == koma) {
|
157
|
+
push(koma, x, y);
|
158
|
+
ff_table[y][x] = -1; //検査済み
|
159
|
+
|
160
|
+
}
|
161
|
+
else if (ff_table[y][x] == EMPTY) {
|
162
|
+
// EMPTYを見つけたので探査を打ち切る
|
163
|
+
return 1;
|
164
|
+
}
|
165
|
+
else {
|
166
|
+
// EMPTYではない何かがあったので探査を打ち切る
|
167
|
+
return 0;
|
168
|
+
}
|
169
|
+
|
170
|
+
// 上下左右方向に探査を続ける
|
171
|
+
if (empty_probe(ff_table, koma, x, y - 1)) {
|
172
|
+
return 1;
|
173
|
+
}
|
174
|
+
else if (empty_probe(ff_table, koma, x, y + 1)) {
|
175
|
+
return 1;
|
176
|
+
}
|
177
|
+
else if (empty_probe(ff_table, koma, x - 1, y)) {
|
178
|
+
return 1;
|
179
|
+
}
|
180
|
+
else if (empty_probe(ff_table, koma, x + 1, y)) {
|
181
|
+
return 1;
|
182
|
+
}
|
183
|
+
return 0;
|
184
|
+
}
|
185
|
+
|
186
|
+
|
187
|
+
// 囲まれているか調べる
|
188
|
+
int is_siege(int koma, int x, int y)
|
189
|
+
{
|
190
|
+
int i, j;
|
191
|
+
|
192
|
+
// 作業用にコピーする
|
193
|
+
int ff_table[VERTICAL_LINE][HORIZONTAL_LINE];
|
194
|
+
for (j = 0;j < VERTICAL_LINE;j++) {
|
195
|
+
for (i = 0;i < HORIZONTAL_LINE;i++) {
|
196
|
+
ff_table[j][i] = table[j][i];
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
init_stack();
|
201
|
+
return !empty_probe(ff_table, koma, x, y);
|
202
|
+
}
|
203
|
+
|
204
|
+
|
205
|
+
//x,yの位置にある駒が取れるか?
|
206
|
+
// 0 - とれない
|
207
|
+
// 1 - 取れる
|
208
|
+
int is_capture(int x, int y)
|
209
|
+
{
|
210
|
+
int koma = table[y][x];
|
211
|
+
int ue = koma_probe(koma, x, y - 1, UE);
|
212
|
+
int shita = koma_probe(koma, x, y + 1, SHITA);
|
213
|
+
int hidari = koma_probe(koma, x - 1, y, HIDARI);
|
214
|
+
int migi = koma_probe(koma, x + 1, y, MIGI);
|
215
|
+
|
216
|
+
int aite;
|
217
|
+
if (koma == FU)
|
218
|
+
{
|
219
|
+
aite = TO;
|
220
|
+
}
|
221
|
+
else
|
222
|
+
{
|
223
|
+
aite = FU;
|
224
|
+
}
|
225
|
+
|
226
|
+
//上下左右方向にaiteで挟まれてたら取れる
|
227
|
+
if (ue == aite && shita == aite)
|
228
|
+
{
|
229
|
+
return 1;
|
230
|
+
}
|
231
|
+
if (hidari == aite && migi == aite)
|
232
|
+
{
|
233
|
+
return 1;
|
234
|
+
}
|
235
|
+
|
236
|
+
// コマが囲まれてたら取れる
|
237
|
+
if (is_siege(koma, x, y)) {
|
238
|
+
return 1;
|
239
|
+
}
|
240
|
+
|
241
|
+
return 0;
|
242
|
+
}
|
243
|
+
|
244
|
+
void disp_result(int x, int y)
|
245
|
+
{
|
246
|
+
int koma = table[y][x];
|
247
|
+
char *name[2] = { "FU", "TO" };
|
248
|
+
if (is_capture(x, y))
|
249
|
+
{
|
250
|
+
printf("%d,%dの%sは取れる\n", x, y, name[koma - 1]);
|
251
|
+
}
|
252
|
+
else
|
253
|
+
{
|
254
|
+
printf("%d,%dの%sは取れない\n", x, y, name[koma - 1]);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
int main(void)
|
259
|
+
{
|
260
|
+
disp_table();
|
261
|
+
disp_result(2, 2);
|
262
|
+
disp_result(5, 2);
|
263
|
+
disp_result(1, 5);
|
264
|
+
disp_result(3, 9);
|
265
|
+
disp_result(5, 6);
|
266
|
+
disp_result(6, 6);
|
267
|
+
disp_result(8, 9);
|
268
|
+
disp_result(9, 1);
|
269
|
+
}
|
270
|
+
|
271
|
+
```
|
272
|
+
|
273
|
+
実行結果
|
274
|
+
|
275
|
+
```
|
276
|
+
0123456789:
|
277
|
+
0***********
|
278
|
+
1* F FT*
|
279
|
+
2* T FTF FT*
|
280
|
+
3* F *
|
281
|
+
4*F *
|
282
|
+
5*TF FF *
|
283
|
+
6*F TTF *
|
284
|
+
7* F *
|
285
|
+
8* F F *
|
286
|
+
9* FTF FTTT*
|
287
|
+
:***********
|
288
|
+
2,2のTOは取れる
|
289
|
+
5,2のTOは取れる
|
290
|
+
1,5のTOは取れる
|
291
|
+
3,9のTOは取れる
|
292
|
+
5,6のTOは取れる
|
293
|
+
6,6のTOは取れない
|
294
|
+
8,9のTOは取れない
|
295
|
+
9,1のTOは取れない
|
296
|
+
```
|
297
|
+
|
298
|
+
追記ここまで
|
299
|
+
|
300
|
+
---
|
1
301
|
is_capture関数で指定位置の駒を取れるか調べます。
|
2
302
|
|
3
303
|
|