-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess-problem-solver.c
More file actions
1435 lines (1298 loc) · 50.4 KB
/
Copy pathchess-problem-solver.c
File metadata and controls
1435 lines (1298 loc) · 50.4 KB
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 例:6k1/6pp/8/8/8/8/6PP/5RK1 w - - 0 1
// 2R5/1p1r1ppk/1b4p1/1p1n4/Q7/5BP1/P4P1P/B1Q3K1 w - - 0 1
// 4k3/3pn1K1/8/8/8/2R1R3/8/8 w - - 0 1 1手詰(白のルークを一番上に)
// k2R4/r7/8/8/8/8/8/8 w - - 0 1
// 4k3/5B2/8/6B1/8/8/8/2Q2RK1 w - - 0 1 確認する
// 4k3/4P3/4KP2/7p/8/8/8/8 w - - 0 1 ポーンによるmate in 1 確認済
// k7/1PP5/PP6/8/8/8/8/7K w - - 0 1 ポーンによるチェックメイト←これはステイルメイトになる
// 2Kq4/1PP1PP2/8/8/8/8/8/k7 w - - 0 1 ポーンによるチェックメイト → そもそも1手づめではない
// pkp5/p1p5/1R6/8/8/8/8/7K w - - 0 1 ルークによるチェックメイト 確認済
// pkp5/ppp5/2N5/8/8/8/8/7K w - - 0 1 ナイトによるチェックメイト 確認済
// pkp5/pp6/3B4/8/8/8/8/7K w - - 0 1 ビショップによるチェックメイト 確認済
// pkp5/pp6/3Q4/8/8/8/8/7K w - - 0 1 クイーン(斜め)によるチェックメイト 確認済
// pkp5/p1p5/1Q6/8/8/8/8/7K w - - 0 1 クイーン(縦横)によるチェックメイト 確認済
// 8/5K2/8/6Nk/8/5N1P/8/3B4 w - - 0 1
// nk6/1p1q4/p7/P3R3/8/6B1/8/1K6 w - - 0 1 3手詰 ←黒側のキングが2つの駒から狙われている場合、両方から逃げることのできる一手があるが、それを選ばずに片方から逃げることしかできていない場合がある
// 1n1k2q1/1Q2p3/4p1B1/6N1/8/8/8/3K4 w - - 0 1 1手詰めの盤面ではない
// 1n1k4/1Q2p3/4p1B1/6N1/8/8/8/3K4 w - - 0 1 1手詰めの盤面
// r1b1kb1r/5ppp/8/6B1/B7/8/5PPP/3R3K w - - 0 1 1手詰めの盤面
// 4k3/8/2q5/5N2/8/3R4/8/4K3 w - - 0 1 3手詰めの盤面??
// 8/8/6p1/4N2k/5K1p/7P/8/8 w - - 0 1 3手詰めの盤面??
// チェックメイトとステイルメイトの違いは、チェックメイトはキングが攻撃されている状態で、移動できるマスがない状態を指すのに対し、
// ステイルメイトはキングが攻撃されていない状態で、移動できるマスがない状態を指す。
// 3R4/4K3/5p2/5p2/8/3BkNQ1/8/8 w - - 0 1
// 8/8/4QP2/K1k5/8/8/3B4/8 w - - 0 1 一手詰めであるが、チェックメイトの判定がなされない。
// チェックされているかどうかの確認を白のターンの最後で行い。チェックメイトの判定も行うようにする。
// 8/5K2/8/6Nk/8/5N1P/8/3B4 w - - 0 1
// 1n1k2q1/1Q2p3/4p1B1/6N1/8/8/8/3K4 w - - 0 1 1手詰
// b3K2b/r1R2p1r/4k3/1N1n4/2BP1Q2/8/8/8 w - - 0 1 3手詰?
// 評価関数について:探索の最短手数を評価値にして、チェックメイトまでの手数をそのまま評価値の値とする。(値が小さいほど良い)
// 黒の駒の移動を表示させてしまう原因:そもそも、コード自体が1手詰にしか対応していなかった。
// 3手詰以上の探索が行えるように修正し、チェックメイトまでの手をすべて表示させた。
// 表示した手を思った通りになぞることができなかった。黒がチェックメイトを避けるような動きをするため(ダイレクトメイトにしたため)
// findWinningMoves関数を修正し、黒がどのような手を打っても、白がチェックメイトに到達できるようにしたつもり(最短手の表示をなぞるように修正したがうまくいかなかった)
// ※ ステイルメイト判定を改善版 ※
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#define BOARD_SIZE 8
#define FEN_BUFFER_SIZE 100
#define DEPTH_MAX 3
#define MAX_MOVES 100
typedef enum
{
EMPTY,
KING,
ROOK,
KNIGHT,
BISHOP,
QUEEN,
PAWN
} PieceType;
typedef struct
{
int row;
int col;
} Position;
typedef struct
{
Position start;
Position end;
} Move;
typedef struct
{
Move moves[MAX_MOVES];
int count;
} MoveList;
typedef struct
{
PieceType type;
bool isWhite;
} Piece;
Piece board[BOARD_SIZE][BOARD_SIZE];
// キング位置のキャッシュ
Position whiteKingPosCache = {-1, -1};
Position blackKingPosCache = {-1, -1};
// グローバル設定
typedef struct {
bool debugMode;
int maxDepth;
} GameOptions;
GameOptions gameOptions = {
.debugMode = false,
.maxDepth = DEPTH_MAX
};
// prototype declaration
void printBoard();
void parseCommandLineArgs(int argc, char *argv[]);
void printUsage(const char *programName);
bool isStalemate(bool isWhite);
bool hasValidMoves(bool isWhite);
// --- 追加: キングの位置を盤面から探索する関数 ---
Position findKing(bool isWhite)
{
// キャッシュから返す
if (isWhite && whiteKingPosCache.row != -1) {
return whiteKingPosCache;
}
if (!isWhite && blackKingPosCache.row != -1) {
return blackKingPosCache;
}
// キャッシュにない場合は探索
for (int r = 0; r < BOARD_SIZE; r++)
{
for (int c = 0; c < BOARD_SIZE; c++)
{
if (board[r][c].type == KING && board[r][c].isWhite == isWhite)
{
Position pos = {r, c};
// キャッシュを更新
if (isWhite) {
whiteKingPosCache = pos;
} else {
blackKingPosCache = pos;
}
return pos;
}
}
}
return (Position){-1, -1};
}
bool isUnderAttack(Position targetPos, bool isWhiteAttacker);
bool isSamePosition(Position p1, Position p2)
{
return p1.row == p2.row && p1.col == p2.col;
}
// ナイトの動きが正しいかチェック
bool isKnightMove(Position startPos, Position endPos)
{
int rowDiff = abs(startPos.row - endPos.row);
int colDiff = abs(startPos.col - endPos.col);
return (rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2);
}
// ルークの移動が正しいかチェック(駒を飛び越えない)
bool isRookMove(Position startPos, Position endPos)
{
if (startPos.row == endPos.row)
{
// 横方向の移動
int colStep = (endPos.col - startPos.col) > 0 ? 1 : -1;
for (int col = startPos.col + colStep; col != endPos.col; col += colStep)
{
if (board[startPos.row][col].type != EMPTY)
{
return false; // 移動経路に駒がある
}
}
return true;
}
else if (startPos.col == endPos.col)
{
// 縦方向の移動
int rowStep = (endPos.row - startPos.row) > 0 ? 1 : -1;
for (int row = startPos.row + rowStep; row != endPos.row; row += rowStep)
{
if (board[row][startPos.col].type != EMPTY)
{
return false; // 移動経路に駒がある
}
}
return true;
}
return false;
}
// ビショップの移動が正しいかチェック(駒を飛び越えない)
bool isBishopMove(Position startPos, Position endPos)
{
int rowDiff = abs(startPos.row - endPos.row);
int colDiff = abs(startPos.col - endPos.col);
if (rowDiff == colDiff)
{
int rowStep = (endPos.row - startPos.row) > 0 ? 1 : -1;
int colStep = (endPos.col - startPos.col) > 0 ? 1 : -1;
for (int i = 1; i < rowDiff; i++)
{
if (board[startPos.row + i * rowStep][startPos.col + i * colStep].type != EMPTY)
{
return false; // 移動経路に駒がある
}
}
return true;
}
return false;
}
// クイーンの移動が正しいかチェック(ルークとビショップの両方の移動経路チェック)
bool isQueenMove(Position startPos, Position endPos)
{
return isRookMove(startPos, endPos) || isBishopMove(startPos, endPos);
}
// ポーンの動きが正しいかチェック(前進と斜め攻撃をサポート)
bool isPawnMove(Position startPos, Position endPos, bool isWhite)
{
int direction = isWhite ? -1 : 1; // 駒の進行方向を設定
// 前進1マス
// 前進は白と黒が異なる
if (endPos.row - startPos.row == direction && startPos.col == endPos.col && board[endPos.row][endPos.col].type == EMPTY)
{
return true;
}
// 開始位置からの2マス前進(初手のみ)
if (isWhite && startPos.row == 6 && endPos.row == 4 && startPos.col == endPos.col && board[endPos.row][endPos.col].type == EMPTY && board[5][startPos.col].type == EMPTY)
{
return true;
}
if (!isWhite && startPos.row == 1 && endPos.row == 3 && startPos.col == endPos.col && board[endPos.row][endPos.col].type == EMPTY && board[2][startPos.col].type == EMPTY)
{
return true;
}
// 斜め攻撃(敵の駒がある場合のみ)
if (endPos.row - startPos.row == direction && abs(startPos.col - endPos.col) == 1 && board[endPos.row][endPos.col].type != EMPTY && board[endPos.row][endPos.col].isWhite != isWhite)
{
return true;
}
return false;
}
// キングの動きが正しいかチェック
bool isKingMove(Position startPos, Position endPos)
{
return abs(startPos.row - endPos.row) <= 1 && abs(startPos.col - endPos.col) <= 1;
}
// 駒の移動が有効かどうかを判定する関数
// 移動可能なマスの探索を行う
// 白と黒のポーンが進めるマスの定義について、それぞれ逆方向にしか移動できない
bool isValidMove(Position startPos, Position endPos, bool isWhiteTurn)
{
Piece piece = board[startPos.row][startPos.col];
if (piece.isWhite != isWhiteTurn)
{
return false;
}
if (piece.type == EMPTY)
{
return false; // 駒がない
}
// 移動先に味方の駒がある場合、移動できない
Piece targetPiece = board[endPos.row][endPos.col];
if (targetPiece.type != EMPTY && targetPiece.isWhite == piece.isWhite)
{
return false; // 移動先に味方の駒がある
}
// 駒ごとの移動判定
switch (piece.type)
{
case ROOK:
if (!isRookMove(startPos, endPos))
return false;
break;
case KNIGHT:
if (!isKnightMove(startPos, endPos))
return false;
break;
case BISHOP:
if (!isBishopMove(startPos, endPos))
return false;
break;
case QUEEN:
if (!isQueenMove(startPos, endPos))
return false;
break;
case PAWN:
if (!isPawnMove(startPos, endPos, piece.isWhite))
return false;
break;
case KING:
// キングの場合は移動後にチェックされていないか確認
if (!isKingMove(startPos, endPos))
return false;
// 仮に移動してみて、そのマスが攻撃されていないか確認
{
Piece backup = board[endPos.row][endPos.col];
Piece fromBackup = board[startPos.row][startPos.col];
board[endPos.row][endPos.col] = fromBackup;
board[startPos.row][startPos.col] = (Piece){EMPTY, false};
// キャッシュを一時的に更新
Position oldKingCache = fromBackup.isWhite ? whiteKingPosCache : blackKingPosCache;
if (fromBackup.isWhite) {
whiteKingPosCache = endPos;
} else {
blackKingPosCache = endPos;
}
bool safe = !isUnderAttack(endPos, !fromBackup.isWhite);
// キャッシュを元に戻す
if (fromBackup.isWhite) {
whiteKingPosCache = oldKingCache;
} else {
blackKingPosCache = oldKingCache;
}
board[startPos.row][startPos.col] = fromBackup;
board[endPos.row][endPos.col] = backup;
return safe;
}
default:
break;
}
// キングの安全性チェックはmin-max探索で行うため、ここでは駒の動きのみ検証
return true;
}
// 駒が特定の位置を攻撃できるかを判定(キングの安全性チェックなし)
bool canAttackSquare(Position attackerPos, Position targetPos, PieceType pieceType, bool isWhite)
{
// 移動先に味方の駒がある場合は攻撃できない
if (board[targetPos.row][targetPos.col].type != EMPTY &&
board[targetPos.row][targetPos.col].isWhite == isWhite) {
return false;
}
switch (pieceType)
{
case ROOK:
return isRookMove(attackerPos, targetPos);
case KNIGHT:
return isKnightMove(attackerPos, targetPos);
case BISHOP:
return isBishopMove(attackerPos, targetPos);
case QUEEN:
return isQueenMove(attackerPos, targetPos);
case PAWN:
// ポーンの攻撃は斜め前方のみ
{
int direction = isWhite ? -1 : 1;
return (targetPos.row - attackerPos.row == direction &&
abs(targetPos.col - attackerPos.col) == 1);
}
case KING:
return isKingMove(attackerPos, targetPos);
default:
return false;
}
}
// 特定の座標が敵の攻撃範囲にあるかを判定する関数(最適化版)
bool isUnderAttack(Position targetPos, bool isWhiteAttacker)
{
// デバッグ用:処理回数をカウント
static int attackCheckCount = 0;
attackCheckCount++;
if (gameOptions.debugMode && attackCheckCount < 10) {
printf("DEBUG: isUnderAttack check #%d\n", attackCheckCount);
}
for (int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
Piece piece = board[row][col];
// 敵の駒の場合、その駒がキングの位置を攻撃できるか確認する
if (piece.type != EMPTY && piece.isWhite == isWhiteAttacker)
{
Position attackerPos = {row, col};
if (canAttackSquare(attackerPos, targetPos, piece.type, isWhiteAttacker))
{
if (gameOptions.debugMode && attackCheckCount < 10) {
printf("DEBUG: 攻撃発見 (%d,%d)->(%d,%d)\n", row, col, targetPos.row, targetPos.col);
}
return true; // 攻撃されている
}
}
}
}
return false; // 攻撃されていない
}
// キングがチェックされているかを判定する関数
bool isKingInCheck(Position kingPos, bool isWhiteKing)
{
return isUnderAttack(kingPos, !isWhiteKing);
}
// ※ 新追加: プレイヤーに有効な手があるかチェック ※
bool hasValidMoves(bool isWhite)
{
static int callCount = 0;
callCount++;
if (gameOptions.debugMode && callCount < 5) {
printf("DEBUG: hasValidMoves (call=%d, isWhite=%d) 開始\n", callCount, isWhite);
}
for (int startRow = 0; startRow < BOARD_SIZE; startRow++)
{
for (int startCol = 0; startCol < BOARD_SIZE; startCol++)
{
Piece piece = board[startRow][startCol];
if (piece.type != EMPTY && piece.isWhite == isWhite)
{
Position startPos = {startRow, startCol};
for (int endRow = 0; endRow < BOARD_SIZE; endRow++)
{
for (int endCol = 0; endCol < BOARD_SIZE; endCol++)
{
Position endPos = {endRow, endCol};
if (isValidMove(startPos, endPos, isWhite))
{
if (gameOptions.debugMode && callCount < 5) {
printf("DEBUG: 有効な手発見 (%d,%d)->(%d,%d)\n", startRow, startCol, endRow, endCol);
}
return true; // 有効な手がある
}
}
}
}
}
}
if (gameOptions.debugMode && callCount < 5) {
printf("DEBUG: 有効な手なし\n");
}
return false; // 有効な手がない
}
// ※ 新追加: ステイルメイト判定関数 ※
bool isStalemate(bool isWhite)
{
Position kingPos = findKing(isWhite);
if (gameOptions.debugMode) {
printf("=== DEBUG isStalemate ===\n");
printf("プレイヤー: %s, キング位置: (%d, %d)\n", isWhite ? "白" : "黒", kingPos.row, kingPos.col);
}
// チェックされている場合はステイルメイトではない
if (isKingInCheck(kingPos, isWhite))
{
if (gameOptions.debugMode) {
printf("結果: false (チェックされているため)\n");
printf("========================\n");
}
return false;
}
// 有効な手があるかチェック
bool hasLegalMoves = hasValidMoves(isWhite);
if (gameOptions.debugMode) {
printf("有効な手があるか: %s\n", hasLegalMoves ? "あり" : "なし");
printf("結果: %s\n", !hasLegalMoves ? "true (ステイルメイト)" : "false");
printf("========================\n");
}
return !hasLegalMoves; // 有効な手がない場合はステイルメイト
}
// チェックしている駒を見つける関数
Position findAttacker(Position targetPos, bool isWhiteAttacker)
{
for (int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
Piece piece = board[row][col];
if (piece.type != EMPTY && piece.isWhite == isWhiteAttacker)
{
Position attackerPos = {row, col};
if (canAttackSquare(attackerPos, targetPos, piece.type, isWhiteAttacker))
{
return attackerPos;
}
}
}
}
return (Position){-1, -1};
}
// 攻撃者の数をカウント
int countAttackers(Position targetPos, bool isWhiteAttacker)
{
int count = 0;
for (int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
Piece piece = board[row][col];
if (piece.type != EMPTY && piece.isWhite == isWhiteAttacker)
{
Position attackerPos = {row, col};
if (canAttackSquare(attackerPos, targetPos, piece.type, isWhiteAttacker))
{
count++;
if (count >= 2) return count; // 2つ以上あれば十分
}
}
}
}
return count;
}
// 攻撃者を取れるかチェック(キング以外の駒で)
bool canCaptureAttacker(Position attackerPos, bool defenderIsWhite)
{
for (int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
Piece piece = board[row][col];
if (piece.type != EMPTY && piece.isWhite == defenderIsWhite)
{
Position defenderPos = {row, col};
if (isValidMove(defenderPos, attackerPos, defenderIsWhite))
{
return true;
}
}
}
}
return false;
}
// 攻撃をブロックできるかチェック(ナイト以外の長距離攻撃のみ)
bool canBlockAttack(Position kingPos, Position attackerPos, bool defenderIsWhite)
{
Piece attacker = board[attackerPos.row][attackerPos.col];
// ナイト、ポーン、キングの攻撃はブロック不可
if (attacker.type == KNIGHT || attacker.type == PAWN || attacker.type == KING)
{
return false;
}
// 攻撃経路上のマスを計算
int rowStep = 0, colStep = 0;
if (attackerPos.row != kingPos.row)
rowStep = (kingPos.row - attackerPos.row) > 0 ? 1 : -1;
if (attackerPos.col != kingPos.col)
colStep = (kingPos.col - attackerPos.col) > 0 ? 1 : -1;
// 攻撃者とキングの間のマスをチェック
int r = attackerPos.row + rowStep;
int c = attackerPos.col + colStep;
while (r != kingPos.row || c != kingPos.col)
{
Position blockPos = {r, c};
// このマスにブロックできる駒があるか
for (int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
Piece piece = board[row][col];
// キング以外の味方の駒
if (piece.type != EMPTY && piece.type != KING && piece.isWhite == defenderIsWhite)
{
Position defenderPos = {row, col};
if (isValidMove(defenderPos, blockPos, defenderIsWhite))
{
return true;
}
}
}
}
r += rowStep;
c += colStep;
}
return false;
}
// キングがチェックメイトかを確認する関数(最適化版)
bool isCheckmate(Position kingPos, bool isWhiteKing)
{
if (gameOptions.debugMode) {
printf("=== DEBUG isCheckmate ===\n");
printf("キング位置: (%d, %d), 白キング: %s\n", kingPos.row, kingPos.col, isWhiteKing ? "yes" : "no");
}
// 0. キングが取られた場合(盤上に存在しない)はチェックメイト
if (kingPos.row == -1 || kingPos.col == -1)
{
if (gameOptions.debugMode) {
printf("結果: true (キングが取られた)\n");
printf("========================\n");
}
return true;
}
// 1. チェックされていない場合はチェックメイトではない
if (!isKingInCheck(kingPos, isWhiteKing))
{
if (gameOptions.debugMode) {
printf("結果: false (チェックされていない)\n");
printf("========================\n");
}
return false;
}
if (gameOptions.debugMode) {
printf("キングはチェックされています。\n");
}
// 2. キングの逃げ場所をチェック(最大8方向のみ)
int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 0; i < 8; i++)
{
int nr = kingPos.row + dr[i];
int nc = kingPos.col + dc[i];
if (nr >= 0 && nr < BOARD_SIZE && nc >= 0 && nc < BOARD_SIZE)
{
Position newPos = {nr, nc};
if (isValidMove(kingPos, newPos, isWhiteKing))
{
if (gameOptions.debugMode) {
printf("結果: false (逃げ場所あり: %d,%d)\n", nr, nc);
printf("========================\n");
}
return false; // 逃げ場所がある
}
}
}
// 3. ダブルチェックの場合、キングが逃げられなければチェックメイト
int attackerCount = countAttackers(kingPos, !isWhiteKing);
if (attackerCount >= 2)
{
if (gameOptions.debugMode) {
printf("結果: true (ダブルチェックで逃げ場所なし)\n");
printf("========================\n");
}
return true; // ダブルチェックでキングが逃げられない
}
// 4. 単一の攻撃者の場合、取るかブロックできるかチェック
Position attackerPos = findAttacker(kingPos, !isWhiteKing);
if (attackerPos.row == -1)
{
// 攻撃者が見つからない(論理エラー)
return false;
}
// 攻撃者を取れるかチェック
if (canCaptureAttacker(attackerPos, isWhiteKing))
{
if (gameOptions.debugMode) {
printf("結果: false (攻撃者を取れる)\n");
printf("========================\n");
}
return false;
}
// 攻撃をブロックできるかチェック
if (canBlockAttack(kingPos, attackerPos, isWhiteKing))
{
if (gameOptions.debugMode) {
printf("結果: false (攻撃をブロック可能)\n");
printf("========================\n");
}
return false;
}
if (gameOptions.debugMode) {
printf("結果: true (チェックメイト)\n");
printf("========================\n");
}
return true; // チェックメイト
}
// 駒を移動させる関数
void movePiece(Position startPos, Position endPos)
{
Piece piece = board[startPos.row][startPos.col];
Piece captured = board[endPos.row][endPos.col];
// キングが取られた場合はキャッシュを無効化
if (captured.type == KING) {
if (captured.isWhite) {
whiteKingPosCache = (Position){-1, -1};
} else {
blackKingPosCache = (Position){-1, -1};
}
}
board[endPos.row][endPos.col] = piece;
board[startPos.row][startPos.col] = (Piece){EMPTY, false};
// キングが移動した場合はキャッシュを更新
if (piece.type == KING) {
if (piece.isWhite) {
whiteKingPosCache = endPos;
} else {
blackKingPosCache = endPos;
}
}
}
// FEN記法からボードを初期化する関数
bool initializeBoardFromFEN(const char *fen)
{
int row = 0;
int col = 0;
int i = 0;
// ボードの初期化
for (int r = 0; r < BOARD_SIZE; r++)
{
for (int c = 0; c < BOARD_SIZE; c++)
{
board[r][c].type = EMPTY;
}
}
while (row < BOARD_SIZE)
{
if (col >= BOARD_SIZE)
{
row++;
col = 0;
if (fen[i] == '/')
{
i++;
}
continue;
}
char pieceChar = fen[i++];
if (isdigit(pieceChar))
{
int emptySquares = pieceChar - '0';
col += emptySquares;
}
else
{
Piece piece;
switch (pieceChar)
{
case 'K':
piece.type = KING;
piece.isWhite = true;
whiteKingPosCache = (Position){row, col};
break;
case 'Q':
piece.type = QUEEN;
piece.isWhite = true;
break;
case 'R':
piece.type = ROOK;
piece.isWhite = true;
break;
case 'B':
piece.type = BISHOP;
piece.isWhite = true;
break;
case 'N':
piece.type = KNIGHT;
piece.isWhite = true;
break;
case 'P':
piece.type = PAWN;
piece.isWhite = true;
break;
case 'k':
piece.type = KING;
piece.isWhite = false;
blackKingPosCache = (Position){row, col};
break;
case 'q':
piece.type = QUEEN;
piece.isWhite = false;
break;
case 'r':
piece.type = ROOK;
piece.isWhite = false;
break;
case 'b':
piece.type = BISHOP;
piece.isWhite = false;
break;
case 'n':
piece.type = KNIGHT;
piece.isWhite = false;
break;
case 'p':
piece.type = PAWN;
piece.isWhite = false;
break;
default:
return false; // 無効なFEN文字
}
board[row][col++] = piece;
}
if (pieceChar == '\0' || pieceChar == ' ')
break; // ボード記述の終わり
}
// 残りのFEN文字列は今回は無視 (手番、キャスリング権など)
return true;
}
// チェス盤の表示
void printBoard()
{
printf(" a b c d e f g h\n");
printf(" ---------------\n");
for (int row = 0; row < BOARD_SIZE; row++)
{
printf("%d|", 8 - row);
for (int col = 0; col < BOARD_SIZE; col++)
{
switch (board[row][col].type)
{
case KING:
printf(board[row][col].isWhite ? "K " : "k ");
break;
case QUEEN:
printf(board[row][col].isWhite ? "Q " : "q ");
break;
case ROOK:
printf(board[row][col].isWhite ? "R " : "r ");
break;
case KNIGHT:
printf(board[row][col].isWhite ? "N " : "n ");
break;
case BISHOP:
printf(board[row][col].isWhite ? "B " : "b ");
break;
case PAWN:
printf(board[row][col].isWhite ? "P " : "p ");
break;
case EMPTY:
printf(". ");
break;
default:
printf("? ");
break;
}
}
printf("\n");
}
printf(" ---------------\n");
}
// 単純な移動可能性チェック(キング安全性なし)
bool canMoveToSquare(Position startPos, Position endPos, bool isWhiteTurn)
{
Piece piece = board[startPos.row][startPos.col];
if (piece.isWhite != isWhiteTurn || piece.type == EMPTY) {
return false;
}
// 移動先に味方の駒がある場合、移動できない
Piece targetPiece = board[endPos.row][endPos.col];
if (targetPiece.type != EMPTY && targetPiece.isWhite == piece.isWhite) {
return false;
}
// 駒ごとの移動判定(キング安全性チェックなし)
switch (piece.type) {
case ROOK:
return isRookMove(startPos, endPos);
case KNIGHT:
return isKnightMove(startPos, endPos);
case BISHOP:
return isBishopMove(startPos, endPos);
case QUEEN:
return isQueenMove(startPos, endPos);
case PAWN:
return isPawnMove(startPos, endPos, piece.isWhite);
case KING:
return isKingMove(startPos, endPos);
default:
return false;
}
}
// チェックメイトを達成できる手を表示する関数
bool canWinInOneMove(bool isWhiteTurn) // 今の手番の色で1手詰みがあるか調べます。
{
for (int startRow = 0; startRow < BOARD_SIZE; startRow++)
{
for (int startCol = 0; startCol < BOARD_SIZE; startCol++)
{
Piece piece = board[startRow][startCol];
if (piece.type != EMPTY && piece.isWhite == isWhiteTurn)
{
Position startPos = {startRow, startCol};
for (int endRow = 0; endRow < BOARD_SIZE; endRow++)
{
for (int endCol = 0; endCol < BOARD_SIZE; endCol++)
{
Position endPos = {endRow, endCol};
if (canMoveToSquare(startPos, endPos, isWhiteTurn))
{
Piece capturedPiece = board[endPos.row][endPos.col];
movePiece(startPos, endPos);
Position newEnemyKingPos = findKing(!isWhiteTurn);
bool checkmate = isCheckmate(newEnemyKingPos, !isWhiteTurn);
board[startPos.row][startPos.col] = piece;
board[endPos.row][endPos.col] = capturedPiece;
if (checkmate)
{
printf("勝利できる一手: (%d, %d) -> (%d, %d)\n", startRow, startCol, endRow, endCol);
return true;
}
}
}
}
}
}
}
return false;
}
// 駒の種類を文字列で取得
const char* getPieceName(PieceType type)
{
switch (type) {
case KING: return "K";
case QUEEN: return "Q";
case ROOK: return "R";
case BISHOP: return "B";
case KNIGHT: return "N";
case PAWN: return "P";
default: return "?";
}
}
// 座標をチェス記法に変換(例: (0,0) -> "a8")
void posToChessNotation(Position pos, char *buf)
{
buf[0] = 'a' + pos.col;
buf[1] = '8' - pos.row;
buf[2] = '\0';
}
// 詰将棋用のmin-max探索関数
// isWhiteTurn: 現在の手番
// depth: 残り探索深度
// moveList: 手順を記録するリスト
// isAttacker: 攻撃側(白)の手番かどうか(詰将棋用)
//
// 戻り値:
// 正の値: 詰みまでの手数
// -1: 詰みなし
// 0: ステイルメイト(引き分け)
int findWinningMoves(bool isWhiteTurn, int depth, MoveList *moveList)
{
if (depth == 0)
return -1;
// 白の手番: 最短で詰める手を選ぶ(minimizer)
// 黒の手番: 最も詰みを遅らせる手を選ぶ(maximizer、ただし詰みを逃れられない場合)
bool isAttackerTurn = isWhiteTurn; // 白が攻撃側と仮定
int bestSteps = isAttackerTurn ? -1 : -1; // 最良の手数
Move bestMove = {{-1, -1}, {-1, -1}};
MoveList bestMoveList = {.count = 0};
bool foundAnyMate = false; // 少なくとも1つの詰み手順が見つかったか
for (int startRow = 0; startRow < BOARD_SIZE; startRow++)
{
for (int startCol = 0; startCol < BOARD_SIZE; startCol++)
{
Piece piece = board[startRow][startCol];
if (piece.type != EMPTY && piece.isWhite == isWhiteTurn)
{
Position startPos = {startRow, startCol};
for (int endRow = 0; endRow < BOARD_SIZE; endRow++)
{