-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcannon-chess.html
More file actions
1412 lines (1243 loc) · 54.2 KB
/
Copy pathcannon-chess.html
File metadata and controls
1412 lines (1243 loc) · 54.2 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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cannon Chess | 带炮国象</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background: linear-gradient(145deg, #648884, #746c94);;
margin: 0;
padding: 20px;
min-height: 100vh;
box-sizing: border-box;
}
#header {
background-color: rgba(255, 255, 255, 0.4);
padding: 1px;
border-radius: 20px;
width: 100%;
max-width: 900px;
text-align: center;
margin-bottom: 20px;
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.2);
text-shadow: 1px 0 15px rgba(255, 255, 255, 0.8);
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 1.8rem;
}
/* 主容器 */
#main-container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-start;
gap: 30px;
width: 100%;
max-width: 900px;
}
#status {
font-size: 1.2em;
margin-bottom: 15px;
font-weight: bold;
color: #d32f2f;
}
/* 棋盘样式 */
#board {
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
width: min(70vw, 70vh, 600px);
height: min(70vw, 70vh, 600px);
border: 6px solid #64636b;
box-shadow: 0 0 8px 8px rgba(0, 0, 0, 0.5);
flex-shrink: 0;
}
.square {
display: flex;
justify-content: center;
align-items: center;
font-size: min(4.5vw, 4.5vh);
cursor: pointer;
position: relative;
}
.light {
background-color: #93b19d;
}
.dark {
background-color: #739485;
}
/* 棋子样式 */
.piece {
width: 75%;
height: 75%;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: inherit;
font-weight: bold;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
transition: transform 0.2s ease;
}
.piece:hover {
transform: scale(1.05);
}
.piece.white {
background: linear-gradient(145deg, #f0f0f0, #b3b3b3);
color: #555;
}
.piece.black {
background: linear-gradient(145deg, #4b4b4b, #161616);
color: #aaa;
}
.piece.advanced {
box-shadow: 0 0 10px 2px rgba(255, 215, 0, 0.8);
}
.piece.white.advanced {
background: linear-gradient(145deg, #fff8dc, #fff7c7);
}
.piece.black.advanced {
background: linear-gradient(145deg, #4b4b1a, #2e2e01);
}
/* 走子动画 */
.piece.moving {
transition: all 0.3s ease-in-out;
z-index: 1000;
position: relative;
}
/* 上一步走子标记 */
.last-move-from {
background-color: rgba(255, 255, 0, 0.3) !important;
}
.last-move-to {
background-color: rgba(255, 255, 0, 0.5) !important;
}
.selected {
box-shadow: inset 0 0 0 3px rgba(178, 190, 0, 0.74);
}
.legal-move::after {
content: "";
position: absolute;
width: 30%;
height: 30%;
border-radius: 50%;
background-color: rgba(164, 235, 72, 0.5);
}
/* 被将军格子提示 */
.square.in-check {
animation: pulse 0.8s infinite alternate !important;
box-shadow: inset 0 0 0 3px rgba(255, 0, 0, 0.5);
}
@keyframes pulse {
from { box-shadow: inset 0 0 0 4px rgba(255, 0, 0, 0.5); }
to { box-shadow: inset 0 0 0 4px rgba(255, 84, 84, 0.7); }
}
/* 侧栏 */
#side-bar {
display: flex;
flex-direction: column;
background-color: rgba(255, 255, 255, 0.2);
padding: 20px;
border-radius: 15px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
min-width: 200px;
}
.controls {
display: flex;
flex-direction: column;
margin-top: 0;
gap: 15px;
}
#buttons {
display: flex;
justify-content: center;
}
button {
padding: 8px 15px;
margin: 0 5px;
font-size: 1em;
cursor: pointer;
background-color: #4c6faf;
color: rgb(189, 189, 189);
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a09b;
}
/* 切换开关样式 */
.toggle-container {
display: flex;
align-items: center;
margin: 0 10px;
}
.toggle-label {
margin: 0 10px;
color: #f0f0f0;
font-weight: bold;
}
.toggle-switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #4c6faf;
}
input:checked + .slider:before {
transform: translateX(26px);
}
/* 移动端响应式设计 */
@media screen and (max-width: 768px) {
#main-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
width: 100%;
max-width: 900px;
margin: 0 auto;
}
#board {
width: 90vw;
height: 90vw;
max-width: 400px;
max-height: 400px;
margin-bottom: 0;
}
#side-bar {
width: 90%;
max-width: 400px;
box-sizing: border-box;
}
.controls {
flex-direction: column;
align-items: center;
}
h1 {
font-size: 1.5rem;
}
.toggle-container {
flex-direction: row;
justify-content: center;
}
}
</style>
</head>
<body>
<div id="header">
<h1>Cannon Chess | 带炮国象</h1>
<div id="status">白方回合</div>
</div>
<div id="main-container">
<div id="board"></div>
<div id="side-bar">
<div class="controls">
<div id="buttons">
<button id="restart">重新开始</button>
<button id="undo">悔棋</button>
</div>
<div class="toggle-container">
<span class="toggle-label">双人</span>
<label class="toggle-switch">
<input type="checkbox" id="aiMode">
<span class="slider"></span>
</label>
<span class="toggle-label">人机</span>
</div>
</div>
<div id="notes">
<h4>说明</h4>
<ol>
<li>原主教Bishop变为炮Cannon,移动同后Queen,吃子同中国象棋的"炮"</li>
<li>取消王车易位(Castling)和吃过路兵(En Passent)的特殊规则</li>
<li>兵Pawn至对方底线升变为高级兵Advanced Pawn, 走法同王King</li>
<li>其它规则同国际象棋</li>
</ol>
</div>
</div>
</div>
<script>
// 游戏常量
const BOARD_SIZE = 8;
const START_FEN = "rncqkcnr/pppppppp/8/8/8/8/PPPPPPPP/RNCQKCNR w KQkq - 0 1";
// 棋子符号
const PIECE_SYMBOLS = {
'P': '♙', 'N': '♘', 'C': '♗', 'R': '♖', 'Q': '♕', 'K': '♔', 'A': '♟',
'p': '♙', 'n': '♘', 'c': '♗', 'r': '♖', 'q': '♕', 'k': '♔', 'a': '♟'
};
// 游戏状态
let board = [];
let turn = 'w';
let selectedSquare = null;
let legalMoves = [];
let history = [];
let isCheck = false;
let MAX_DEPTH = 3; // AI搜索深度
let isAIMode = true; // 是否为AI模式
let positionHistory = []; // 历史棋盘状态
let gameOver = false;
let gemeResult = '';
// 初始化游戏
function initGame() {
parseFEN(START_FEN);
// 重置选择状态
selectedSquare = null;
legalMoves = [];
isCheck = false;
positionHistory = []; // 清空局面历史
let gameOver = false;
let gemeResult = '';
renderBoard();
updateStatus();
}
// 解析FEN字符串
function parseFEN(fen) {
const parts = fen.split(' ');
const rows = parts[0].split('/');
board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
turn = parts[1];
for (let r = 0; r < BOARD_SIZE; r++) {
let c = 0;
for (const ch of rows[r]) {
if (/\d/.test(ch)) {
c += parseInt(ch);
} else {
board[r][c] = ch;
c++;
}
}
}
history = [];
isCheck = false;
}
// 强制重绘函数
function forceRedraw(element) {
void element.offsetHeight; // 触发重绘
}
// 渲染棋盘
function renderBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
// 获取当前走棋方的王的位置
const currentKingPos = findKing(turn);
const isCurrentKingInCheck = isKingInCheck(turn);
// 获取上一步的移动
const lastMove = history.length > 0 ? history[history.length - 1] : null;
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const square = document.createElement('div');
square.className = `square ${(r + c) % 2 === 0 ? 'light' : 'dark'}`;
square.dataset.row = r;
square.dataset.col = c;
// 标记上一步的起始位置
if (lastMove && lastMove.from.row === r && lastMove.from.col === c) {
square.classList.add('last-move-from');
}
// 标记上一步的目标位置
if (lastMove && lastMove.to.row === r && lastMove.to.col === c) {
square.classList.add('last-move-to');
}
// 高亮选中的棋子
if (selectedSquare && selectedSquare.row === r && selectedSquare.col === c) {
square.classList.add('selected');
}
// 标记合法移动
if (legalMoves.some(move => move.row === r && move.col === c)) {
square.classList.add('legal-move');
}
square.classList.remove('in-check');
// 标记被将军的王(当前走棋方的王)
if (isCurrentKingInCheck && currentKingPos &&
r === currentKingPos.row && c === currentKingPos.col) {
square.classList.add('in-check');
}
// 添加棋子
const piece = board[r][c];
if (piece) {
const pieceElement = document.createElement('div');
const isAdvanced = piece.toLowerCase() === 'a';
pieceElement.className = `piece ${(piece === piece.toUpperCase()) ? 'white' : 'black'}${isAdvanced ? ' advanced' : ''}`;
pieceElement.textContent = PIECE_SYMBOLS[piece];
square.appendChild(pieceElement);
}
square.addEventListener('click', () => handleSquareClick(r, c));
boardElement.appendChild(square);
}
}
// 重绘棋盘
forceRedraw(boardElement);
}
// 处理棋盘点击
function handleSquareClick(row, col) {
const piece = board[row][col];
// 如果是AI模式且轮到黑方,不允许人类玩家操作
if (isAIMode && turn === 'b') {
return;
}
// 如果没有选中棋子,且点击的是当前方的棋子,则选中它
if (!selectedSquare) {
if (piece && (piece === piece.toUpperCase()) === (turn === 'w')) {
selectedSquare = { row, col };
legalMoves = calculateLegalMoves(row, col);
renderBoard();
}
return;
}
// 如果点击的是已选中的棋子,取消选择
if (selectedSquare.row === row && selectedSquare.col === col) {
selectedSquare = null;
legalMoves = [];
renderBoard();
return;
}
// 检查是否是合法移动
const isLegal = legalMoves.some(move => move.row === row && move.col === col);
if (isLegal) {
// 执行移动
const move = {
from: { ...selectedSquare },
to: { row, col },
piece: board[selectedSquare.row][selectedSquare.col],
captured: board[row][col]
};
// 获取移动的棋子元素
const fromSquare = document.querySelector(`[data-row="${selectedSquare.row}"][data-col="${selectedSquare.col}"]`);
const toSquare = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
const pieceElement = fromSquare.querySelector('.piece');
// 添加移动动画
if (pieceElement) {
pieceElement.classList.add('moving');
// 计算目标位置
const fromRect = fromSquare.getBoundingClientRect();
const toRect = toSquare.getBoundingClientRect();
const deltaX = toRect.left - fromRect.left;
const deltaY = toRect.top - fromRect.top;
// 设置动画
pieceElement.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
// 动画结束后更新棋盘
setTimeout(() => {
history.push(move);
board[row][col] = board[selectedSquare.row][selectedSquare.col];
board[selectedSquare.row][selectedSquare.col] = null;
// 检查升变
if (move.piece.toLowerCase() === 'p') {
const isWhite = move.piece === move.piece.toUpperCase();
if ((isWhite && row === 0) || (!isWhite && row === 7)) {
board[row][col] = isWhite ? 'A' : 'a';
}
}
// 记录当前局面
const currentPosition = getPositionFingerprint();
positionHistory.push(currentPosition);
// 检查是否形成三次重复
if (isThreefoldRepetition()) {
gameOver = true;
gameResult = "Three Fold Repetition(三次重复局面),和棋";
updateStatus();
return;
}
// 切换回合
turn = turn === 'w' ? 'b' : 'w';
// 检查新回合方是否有合法移动
const opponentColor = turn === 'w' ? 'b' : 'w';
const hasLegalMove = hasAnyLegalMove();
if (!hasLegalMove) {
isCheck = isKingInCheck(turn);
gameOver = true;
if (isCheck) {
gameResult = `Checkmate!${opponentColor === 'w' ? '白方' : '黑方'}获胜!`;
} else {
gameResult = "Stalemate(无子可动),和棋";
}
updateStatus();
return;
}
// 检查对方是否被将军
isCheck = isKingInCheck(turn === 'w' ? 'b' : 'w');
selectedSquare = null;
legalMoves = [];
// 重置动画样式
pieceElement.classList.remove('moving');
pieceElement.style.transform = '';
renderBoard();
updateStatus();
// 如果是AI模式且轮到黑方,让AI移动
if (isAIMode && turn === 'b') {
setTimeout(() => {
makeAIMove();
}, 500);
}
}, 300); // 与CSS动画时间相匹配
}
} else if (piece && (piece === piece.toUpperCase()) === (turn === 'w')) {
// 选择另一个己方棋子
selectedSquare = { row, col };
legalMoves = calculateLegalMoves(row, col);
renderBoard();
}
}
// 检查当前回合方是否有任何合法移动
function hasAnyLegalMove() {
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const piece = board[r][c];
if (piece && (piece === piece.toUpperCase()) === (turn === 'w')) {
if (calculateLegalMoves(r, c).length > 0) {
return true;
}
}
}
}
return false;
}
// 计算所有合法移动
function calculateLegalMoves(row, col) {
const piece = board[row][col];
if (!piece) return [];
const color = piece === piece.toUpperCase() ? 'w' : 'b';
const rawMoves = calculateRawMoves(row, col);
const legal = [];
// 过滤会导致被将军的走法
for (const move of rawMoves) {
// 临时移动
const captured = board[move.row][move.col];
board[move.row][move.col] = piece;
board[row][col] = null;
if (!isKingInCheck(color)) {
legal.push(move);
}
// 恢复
board[row][col] = piece;
board[move.row][move.col] = captured;
}
return legal;
}
// 计算原始移动(不考虑将军)
function calculateRawMoves(row, col) {
const piece = board[row][col];
if (!piece) return [];
const isWhite = piece === piece.toUpperCase();
const type = piece.toLowerCase();
const moves = [];
switch (type) {
case 'p': // 兵
const direction = isWhite ? -1 : 1;
// 前进一步
if (isValidSquare(row + direction, col) && !board[row + direction][col]) {
moves.push({ row: row + direction, col });
// 初始位置可前进两步
const startRow = isWhite ? 6 : 1;
if (row === startRow && !board[row + 2 * direction][col]) {
moves.push({ row: row + 2 * direction, col });
}
}
// 斜吃
for (const dc of [-1, 1]) {
const newCol = col + dc;
if (isValidSquare(row + direction, newCol)) {
const target = board[row + direction][newCol];
if (target && (target === target.toUpperCase()) !== isWhite) {
moves.push({ row: row + direction, col: newCol });
}
}
}
break;
case 'a': // 高级兵(走法与王相同)
for (const [dr, dc] of [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]) {
const newRow = row + dr;
const newCol = col + dc;
if (isValidSquare(newRow, newCol)) {
const target = board[newRow][newCol];
if (!target || (target === target.toUpperCase()) !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
}
}
break;
case 'n': // 马
for (const [dr, dc] of [[2,1],[2,-1],[-2,1],[-2,-1],[1,2],[1,-2],[-1,2],[-1,-2]]) {
const newRow = row + dr;
const newCol = col + dc;
if (isValidSquare(newRow, newCol)) {
const target = board[newRow][newCol];
if (!target || (target === target.toUpperCase()) !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
}
}
break;
case 'c': // 炮
// 移动部分(直线不限距离,不能越子)
for (const [dr, dc] of [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]) {
let newRow = row + dr;
let newCol = col + dc;
while (isValidSquare(newRow, newCol)) {
if (!board[newRow][newCol]) {
moves.push({ row: newRow, col: newCol });
} else {
break;
}
newRow += dr;
newCol += dc;
}
}
// 吃子部分(必须隔一个炮架)
for (const [dr, dc] of [[1,0],[-1,0],[0,1],[0,-1]]) {
let foundScreen = false;
let newRow = row + dr;
let newCol = col + dc;
while (isValidSquare(newRow, newCol)) {
if (board[newRow][newCol]) {
if (!foundScreen) {
foundScreen = true;
} else {
const target = board[newRow][newCol];
if ((target === target.toUpperCase()) !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
break;
}
}
newRow += dr;
newCol += dc;
}
}
break;
case 'r': // 车
case 'q': // 后
const directions = type === 'r'
? [[1,0],[-1,0],[0,1],[0,-1]]
: [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]];
for (const [dr, dc] of directions) {
let newRow = row + dr;
let newCol = col + dc;
while (isValidSquare(newRow, newCol)) {
const target = board[newRow][newCol];
if (!target) {
moves.push({ row: newRow, col: newCol });
} else {
if ((target === target.toUpperCase()) !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
break;
}
newRow += dr;
newCol += dc;
}
}
break;
case 'k': // 王
for (const [dr, dc] of [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]) {
const newRow = row + dr;
const newCol = col + dc;
if (isValidSquare(newRow, newCol)) {
const target = board[newRow][newCol];
if (!target || (target === target.toUpperCase()) !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
}
}
break;
}
return moves;
}
// 获取局面指纹
function getPositionFingerprint() {
// 简化的局面表示:棋子位置+回合方
let fingerprint = turn + "|";
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
fingerprint += board[r][c] || "-";
}
fingerprint += "/";
}
return fingerprint;
}
// 是否形成三次重复局面
function isThreefoldRepetition() {
if (positionHistory.length < 3) return false;
const currentPosition = positionHistory[positionHistory.length - 1];
let count = 0;
// 统计当前局面出现的次数
for (let i = 0; i < positionHistory.length; i++) {
if (positionHistory[i] === currentPosition) {
count++;
if (count >= 3) {
return true;
}
}
}
return false;
}
// 检查坐标是否有效
function isValidSquare(row, col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
// 检查指定颜色的王是否被将军
function isKingInCheck(color) {
const kingPos = findKing(color);
if (!kingPos) return false;
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
const piece = board[r][c];
if (piece && (piece === piece.toUpperCase() ? 'w' : 'b') !== color) {
const moves = calculateRawMoves(r, c);
if (moves.some(move => move.row === kingPos.row && move.col === kingPos.col)) {
return true;
}
}
}
}
return false;
}
// 查找指定颜色的王
function findKing(color) {
const king = color === 'w' ? 'K' : 'k';
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
if (board[r][c] === king) {
return { row: r, col: c };
}
}
}
return null;
}
// 更新状态显示
function updateStatus() {
const statusElement = document.getElementById('status');
let status = '';
if (gameOver) {
status = gameResult;
statusElement.style.color = '#d32f2f'; // 红色文字显示结果
} else {
status = `${turn === 'w' ? '白方' : '黑方'}回合`;
// 检查当前走棋方是否被将军
if (isKingInCheck(turn)) {
status += " (CHECK!)";
statusElement.style.color = '#d32f2f'; // 红色文字
} else {
statusElement.style.color = '#333'; // 黑色文字
}
}
statusElement.textContent = status;
}
// 悔棋
function undoMove() {
if (history.length === 0) return;
const lastMove = history.pop();
board[lastMove.from.row][lastMove.from.col] = lastMove.piece;
board[lastMove.to.row][lastMove.to.col] = lastMove.captured;
turn = turn === 'w' ? 'b' : 'w';
// 更新将军状态
const opponentColor = turn === 'w' ? 'b' : 'w';
isCheck = isKingInCheck(opponentColor);
selectedSquare = null;
legalMoves = [];
renderBoard();
updateStatus();
}
// AI部分
// 棋子基础价值
const PIECE_VALUES = {
'p': 10, 'n': 35, 'c': 35, 'r': 55, 'q': 90, 'k': 1000,
'a': 30 // 高级兵的价值
};
// 动态棋子价值
function getDynamicPieceValue(pieceType, gamePhase) {
// 根据游戏阶段调整棋子价值
const phaseMultiplier = {
'p': 1, // 兵价值保持不变
'n': 0.8 + gamePhase * 0.3, // 马在残局价值更高
'c': 1.2 - gamePhase * 0.4, // 炮在开局价值更高
'r': 1, // 车价值保持不变
'q': 1, // 后价值保持不变
'k': 1, // 王价值保持不变
'a': 1 // 高级兵价值保持不变
};
return PIECE_VALUES[pieceType] * phaseMultiplier[pieceType];
}
// 计算游戏阶段(基于剩余棋子数量)
function calculateGamePhase() {
let totalPieces = 0;
for (let r = 0; r < BOARD_SIZE; r++) {
for (let c = 0; c < BOARD_SIZE; c++) {
if (board[r][c]) {
totalPieces++;
}
}
}
// 初始32个棋子,当棋子数量减少时,gamePhase从0增加到1,28个以上为0,8个以下为1
return Math.max(0, Math.min(1, (28 - totalPieces) / 24));
}
// 位置价值表(用于评估棋子位置)
const POSITION_VALUES = {
'p': [ // 兵
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[2, 3, 3, 4, 4, 3, 3, 2],
[3, 4, 4, 5, 5, 4, 4, 3],
[5, 5, 5, 6, 6, 5, 5, 4],
[6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7],
[9, 9, 9, 9, 9, 9, 9, 9]
],
'a': [ // 高级兵
[-2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -2, -2],
[-1, -1, -1, -1, -1, -1, -1, -1],
[ 1, 1, 2, 2, 2, 2, 1, 1],
[ 2, 3, 3, 4, 4, 3, 3, 2],
[ 3, 4, 4, 5, 5, 4, 4, 3]
],
'n': [ // 马
[-5, -4, -3, -3, -3, -3, -4, -5],
[-4, -2, 0, 0, 0, 0, -2, -4],
[-3, 0, 1, 2, 2, 1, 0, -3],
[-3, 0, 2, 3, 3, 2, 0, -3],
[-3, 0, 2, 3, 3, 2, 0, -3],
[-3, 0, 1, 2, 2, 1, 0, -3],
[-4, -2, 0, 0, 0, 0, -2, -4],
[-5, -4, -3, -3, -3, -3, -4, -5]
],
'c': [ // 炮
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 3, 3, 2, 2, 2],
[ 3, 3, 3, 4, 4, 3, 3, 3],
[ 2, 2, 2, 3, 3, 2, 2, 2],
[ 1, 1, 1, 1, 1, 1, 1, 1],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[-1, -1, -1, -1, -1, -1, -1, -1]
],
'r': [ // 车
[ 0, 0, 1, 2, 2, 1, 0, 0],
[ 0, 1, 2, 3, 3, 2, 1, 0],
[ 1, 2, 3, 4, 4, 3, 2, 1],
[ 2, 3, 4, 5, 5, 4, 3, 2],
[ 2, 3, 4, 5, 5, 4, 3, 2],
[ 1, 2, 3, 4, 4, 3, 2, 1],
[ 0, 1, 2, 3, 3, 2, 1, 0],
[ 0, 0, 1, 2, 2, 1, 0, 0]
],
'q': [ // 后
[-2, -1, -1, -1, -1, -1, -1, -2],
[-1, 0, 0, 0, 0, 0, 0, -1],
[-1, 0, 1, 1, 1, 1, 0, -1],
[-1, 0, 1, 2, 2, 1, 0, -1],
[-1, 0, 1, 2, 2, 1, 0, -1],
[-1, 0, 1, 1, 1, 1, 0, -1],
[-1, 0, 0, 0, 0, 0, 0, -1],
[-2, -1, -1, -1, -1, -1, -1, -2]
],
'k': [ // 王
[ 4, 5, 4, 3, 3, 4, 5, 4],
[ 4, 4, 4, 3, 3, 4, 4, 4],
[ 3, 3, 3, 2, 2, 3, 3, 3],
[ 2, 2, 2, 1, 1, 2, 2, 2],
[ 1, 1, 1, 0, 0, 1, 1, 1],
[ 0, 0, 0, -1, -1, 0, 0, 0],
[-1, -1, -1, -2, -2, -1, -1, -1],
[-2, -2, -2, -3, -3, -2, -2, -2]
]
};
// 评估局面分数
function evaluateBoard() {
let score = 0;
const gamePhase = calculateGamePhase();
// 中心控制评估
const centerControl = {
'w': 0,
'b': 0
};
//遍历棋盘