回答編集履歴

1

質問者様の指摘を反映させました

2018/07/09 06:05

投稿

tatamyiwathy
tatamyiwathy

スコア1039

test CHANGED
@@ -1,3 +1,603 @@
1
+ 2018/07/09 追記しました
2
+
3
+ ---
4
+
5
+ 追記ここから
6
+
7
+
8
+
9
+ is_siege関数が囲まれているかを判定する関数となっています。
10
+
11
+
12
+
13
+
14
+
15
+ ```c
16
+
17
+ #include <stdio.h>
18
+
19
+
20
+
21
+ #define HORIZONTAL_LINE 11 //横
22
+
23
+ #define VERTICAL_LINE 11 //縦
24
+
25
+ #define EMPTY 0
26
+
27
+ #define FU 1
28
+
29
+ #define TO 2
30
+
31
+ #define KABE 3
32
+
33
+
34
+
35
+
36
+
37
+ #define UE 0
38
+
39
+ #define SHITA 1
40
+
41
+ #define HIDARI 2
42
+
43
+ #define MIGI 3
44
+
45
+
46
+
47
+
48
+
49
+ //グローバル変数
50
+
51
+ int table[VERTICAL_LINE][HORIZONTAL_LINE] = {
52
+
53
+ { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
54
+
55
+ { 3, 0, 1, 0, 0, 0, 0, 0, 1, 2, 3 },
56
+
57
+ { 3, 0, 2, 0, 1, 2, 1, 0, 1, 2, 3 },
58
+
59
+ { 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3 },
60
+
61
+ { 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
62
+
63
+ { 3, 2, 1, 0, 0, 1, 1, 0, 0, 0, 3 },
64
+
65
+ { 3, 1, 0, 0, 0, 2, 2, 1, 0, 0, 3 },
66
+
67
+ { 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 3 },
68
+
69
+ { 3, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3 },
70
+
71
+ { 3, 0, 1, 2, 1, 0, 1, 2, 2, 2, 3 },
72
+
73
+ { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
74
+
75
+ };
76
+
77
+
78
+
79
+ struct stack {
80
+
81
+ int koma;
82
+
83
+ int x;
84
+
85
+ int y;
86
+
87
+ };
88
+
89
+
90
+
91
+ #define STACK_SIZE 9
92
+
93
+ static struct stack s_stack[STACK_SIZE];
94
+
95
+ static int s_stack_top;
96
+
97
+
98
+
99
+
100
+
101
+ void init_stack(void) {
102
+
103
+ s_stack_top = 0;
104
+
105
+ }
106
+
107
+
108
+
109
+ void push(int koma, int x, int y) {
110
+
111
+ if (s_stack_top >= STACK_SIZE) {
112
+
113
+ return;
114
+
115
+ }
116
+
117
+ s_stack[s_stack_top].koma = koma;
118
+
119
+ s_stack[s_stack_top].x = x;
120
+
121
+ s_stack[s_stack_top].y = y;
122
+
123
+ s_stack_top++;
124
+
125
+ }
126
+
127
+
128
+
129
+ int pop(struct stack* s)
130
+
131
+ {
132
+
133
+ if (s_stack_top < 1) {
134
+
135
+ return 0;
136
+
137
+ }
138
+
139
+
140
+
141
+ s_stack_top--;
142
+
143
+ s->koma = s_stack[s_stack_top].koma;
144
+
145
+ s->x = s_stack[s_stack_top].x;
146
+
147
+ s->y = s_stack[s_stack_top].y;
148
+
149
+ return 1;
150
+
151
+ }
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+ void disp_table(void)
160
+
161
+ {
162
+
163
+ int i, j;
164
+
165
+ putchar(' ');
166
+
167
+ for (i = 0; i < HORIZONTAL_LINE; i++)
168
+
169
+ {
170
+
171
+ putchar(i + 0x30);
172
+
173
+ }
174
+
175
+ putchar('\n');
176
+
177
+ for (j = 0; j < VERTICAL_LINE; j++)
178
+
179
+ {
180
+
181
+ putchar(j + 0x30);
182
+
183
+ for (i = 0; i < HORIZONTAL_LINE; i++)
184
+
185
+ {
186
+
187
+ switch (table[j][i])
188
+
189
+ {
190
+
191
+ case KABE:
192
+
193
+ putchar('*');
194
+
195
+ break;
196
+
197
+ case FU:
198
+
199
+ putchar('F');
200
+
201
+ break;
202
+
203
+ case TO:
204
+
205
+ putchar('T');
206
+
207
+ break;
208
+
209
+ default:
210
+
211
+ putchar(' ');
212
+
213
+ break;
214
+
215
+ }
216
+
217
+ }
218
+
219
+ putchar('\n');
220
+
221
+ }
222
+
223
+ }
224
+
225
+
226
+
227
+ // 駒のつながりが途切れた先にあるものを調べる
228
+
229
+ int koma_probe(int koma, int x, int y, int d)
230
+
231
+ {
232
+
233
+ int placed;
234
+
235
+ if (table[y][x] == EMPTY)
236
+
237
+ {
238
+
239
+ return EMPTY;
240
+
241
+ }
242
+
243
+ if (table[y][x] == KABE)
244
+
245
+ {
246
+
247
+ return KABE;
248
+
249
+ }
250
+
251
+ if (table[y][x] != koma)
252
+
253
+ {
254
+
255
+ return table[y][x];
256
+
257
+ }
258
+
259
+
260
+
261
+ placed = 0;
262
+
263
+ switch (d)
264
+
265
+ {
266
+
267
+ case UE:
268
+
269
+ placed = koma_probe(koma, x, y - 1, d);
270
+
271
+ break;
272
+
273
+ case SHITA:
274
+
275
+ placed = koma_probe(koma, x, y + 1, d);
276
+
277
+ break;
278
+
279
+ case HIDARI:
280
+
281
+ placed = koma_probe(koma, x - 1, y, d);
282
+
283
+ break;
284
+
285
+ case MIGI:
286
+
287
+ placed = koma_probe(koma, x + 1, y, d);
288
+
289
+ break;
290
+
291
+ }
292
+
293
+ return placed;
294
+
295
+ }
296
+
297
+
298
+
299
+
300
+
301
+ // komaをたどりながらEMPTYを探す
302
+
303
+ // EMPTYを検出した=囲まれてないので取れない
304
+
305
+ // 0 - EMPTYが無かった
306
+
307
+ // 1 - EMPTYを見つけた
308
+
309
+ int empty_probe(int ff_table[][HORIZONTAL_LINE], int koma, int x, int y) {
310
+
311
+ if (ff_table[y][x] == koma) {
312
+
313
+ push(koma, x, y);
314
+
315
+ ff_table[y][x] = -1; //検査済み
316
+
317
+
318
+
319
+ }
320
+
321
+ else if (ff_table[y][x] == EMPTY) {
322
+
323
+ // EMPTYを見つけたので探査を打ち切る
324
+
325
+ return 1;
326
+
327
+ }
328
+
329
+ else {
330
+
331
+ // EMPTYではない何かがあったので探査を打ち切る
332
+
333
+ return 0;
334
+
335
+ }
336
+
337
+
338
+
339
+ // 上下左右方向に探査を続ける
340
+
341
+ if (empty_probe(ff_table, koma, x, y - 1)) {
342
+
343
+ return 1;
344
+
345
+ }
346
+
347
+ else if (empty_probe(ff_table, koma, x, y + 1)) {
348
+
349
+ return 1;
350
+
351
+ }
352
+
353
+ else if (empty_probe(ff_table, koma, x - 1, y)) {
354
+
355
+ return 1;
356
+
357
+ }
358
+
359
+ else if (empty_probe(ff_table, koma, x + 1, y)) {
360
+
361
+ return 1;
362
+
363
+ }
364
+
365
+ return 0;
366
+
367
+ }
368
+
369
+
370
+
371
+
372
+
373
+ // 囲まれているか調べる
374
+
375
+ int is_siege(int koma, int x, int y)
376
+
377
+ {
378
+
379
+ int i, j;
380
+
381
+
382
+
383
+ // 作業用にコピーする
384
+
385
+ int ff_table[VERTICAL_LINE][HORIZONTAL_LINE];
386
+
387
+ for (j = 0;j < VERTICAL_LINE;j++) {
388
+
389
+ for (i = 0;i < HORIZONTAL_LINE;i++) {
390
+
391
+ ff_table[j][i] = table[j][i];
392
+
393
+ }
394
+
395
+ }
396
+
397
+
398
+
399
+ init_stack();
400
+
401
+ return !empty_probe(ff_table, koma, x, y);
402
+
403
+ }
404
+
405
+
406
+
407
+
408
+
409
+ //x,yの位置にある駒が取れるか?
410
+
411
+ // 0 - とれない
412
+
413
+ // 1 - 取れる
414
+
415
+ int is_capture(int x, int y)
416
+
417
+ {
418
+
419
+ int koma = table[y][x];
420
+
421
+ int ue = koma_probe(koma, x, y - 1, UE);
422
+
423
+ int shita = koma_probe(koma, x, y + 1, SHITA);
424
+
425
+ int hidari = koma_probe(koma, x - 1, y, HIDARI);
426
+
427
+ int migi = koma_probe(koma, x + 1, y, MIGI);
428
+
429
+
430
+
431
+ int aite;
432
+
433
+ if (koma == FU)
434
+
435
+ {
436
+
437
+ aite = TO;
438
+
439
+ }
440
+
441
+ else
442
+
443
+ {
444
+
445
+ aite = FU;
446
+
447
+ }
448
+
449
+
450
+
451
+ //上下左右方向にaiteで挟まれてたら取れる
452
+
453
+ if (ue == aite && shita == aite)
454
+
455
+ {
456
+
457
+ return 1;
458
+
459
+ }
460
+
461
+ if (hidari == aite && migi == aite)
462
+
463
+ {
464
+
465
+ return 1;
466
+
467
+ }
468
+
469
+
470
+
471
+ // コマが囲まれてたら取れる
472
+
473
+ if (is_siege(koma, x, y)) {
474
+
475
+ return 1;
476
+
477
+ }
478
+
479
+
480
+
481
+ return 0;
482
+
483
+ }
484
+
485
+
486
+
487
+ void disp_result(int x, int y)
488
+
489
+ {
490
+
491
+ int koma = table[y][x];
492
+
493
+ char *name[2] = { "FU", "TO" };
494
+
495
+ if (is_capture(x, y))
496
+
497
+ {
498
+
499
+ printf("%d,%dの%sは取れる\n", x, y, name[koma - 1]);
500
+
501
+ }
502
+
503
+ else
504
+
505
+ {
506
+
507
+ printf("%d,%dの%sは取れない\n", x, y, name[koma - 1]);
508
+
509
+ }
510
+
511
+ }
512
+
513
+
514
+
515
+ int main(void)
516
+
517
+ {
518
+
519
+ disp_table();
520
+
521
+ disp_result(2, 2);
522
+
523
+ disp_result(5, 2);
524
+
525
+ disp_result(1, 5);
526
+
527
+ disp_result(3, 9);
528
+
529
+ disp_result(5, 6);
530
+
531
+ disp_result(6, 6);
532
+
533
+ disp_result(8, 9);
534
+
535
+ disp_result(9, 1);
536
+
537
+ }
538
+
539
+
540
+
541
+ ```
542
+
543
+
544
+
545
+ 実行結果
546
+
547
+
548
+
549
+ ```
550
+
551
+ 0123456789:
552
+
553
+ 0***********
554
+
555
+ 1* F FT*
556
+
557
+ 2* T FTF FT*
558
+
559
+ 3* F *
560
+
561
+ 4*F *
562
+
563
+ 5*TF FF *
564
+
565
+ 6*F TTF *
566
+
567
+ 7* F *
568
+
569
+ 8* F F *
570
+
571
+ 9* FTF FTTT*
572
+
573
+ :***********
574
+
575
+ 2,2のTOは取れる
576
+
577
+ 5,2のTOは取れる
578
+
579
+ 1,5のTOは取れる
580
+
581
+ 3,9のTOは取れる
582
+
583
+ 5,6のTOは取れる
584
+
585
+ 6,6のTOは取れない
586
+
587
+ 8,9のTOは取れない
588
+
589
+ 9,1のTOは取れない
590
+
591
+ ```
592
+
593
+
594
+
595
+ 追記ここまで
596
+
597
+
598
+
599
+ ---
600
+
1
601
  is_capture関数で指定位置の駒を取れるか調べます。
2
602
 
3
603