-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
1963 lines (1556 loc) · 56.2 KB
/
ai.js
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
/*
This file is part of Metro Dark Chess
Copyright (C)2012-2013 Chien-Yu Chen <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
"use strict";
// 設置測試數據
function setTestChessData()
{
// data from the printDemoChessData() .
/*
gChesses = new Array( "卒", "士", "包", "仕", "兵", "陣", "馬", "炮", "象", "陣", "帥", "相", "馬", "卒", "傌", "兵", "兵", "傌", "兵", "車", "將", "卒", "仕", "包", "兵", "士", "卒", "車", "卒", "象", "炮", "相" );
gChessStates = new Array( 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 0, 0, 0, 0, -1, 1, 1, -1, 0, -1, 0, -1, -1, 0, 1, 1, -1, -1, 0, 0, -1, -1 );
gChesses = new Array( "仕", "兵", "相", "相", "兵", "兵", "象", "陣", "包", "士", "兵", "士", "象", "傌", "卒", "車", "卒", "帥", "炮", "炮", "仕", "兵", "馬", "卒", "車", "陣", "將", "卒", "卒", "包", "傌", "馬" );
gChessStates = new Array( 0, -1, -1, -1, 1, -1, -1, -1, 0, 0, -1, 0, 0, 1, -1, -1, 0, 1, -1, 1, 0, -1, -1, 1, 0, -1, 0, -1, 0, -1, 1, 0 );
*/
}
// 進階AI找出此次走法
function moveByAdvanceAI( chesses, chessStates, camp )
{
printDebug( "----------- moveByAdvanceAI -----------" );
var chessData = copyChessData( getNowChessData() );
var eatPrices = getInitPrices();
var eatenPrices = getInitPrices();
var firstMoves = new Array( NOT_FOUND, NOT_FOUND );
var n = giPricesLength - 1; // 模擬幾回合的攻防
initSim(); // 清除前次的模擬紀錄
simAllWay( chessData, camp, eatPrices, eatenPrices, firstMoves, n, n, 1 );
var bestMoves = getBestSimMoves( chessData, camp ); // find the best move from last sim .
printDebug( getCampName( camp ) );
printDebug( "MedAI:" + bestMoves[0] + "->" + bestMoves[1] );
//printDebug( "<hr>" );
if ( bestMoves[0] != NOT_FOUND && bestMoves[1] != NOT_FOUND )
{
return bestMoves;
}
else
{
return moveByAI( chesses, chessStates, camp );
}
}
// 取得camp陣營的normalEat其中最大的price
function getBestNormalEatPrice( chessData, camp )
{
var bestPrice = INIT_PRICE;
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
if ( chessData.chessStates[i] == OPEN &&
getCamp( chessData.chesses[i] ) == camp )
{
var moveData = getEmptyMoveData( i );
if ( findNormalEat( chessData, moveData, PRICE_FIRST_PRINCIPLE, 0 ) )
{
if ( bestPrice < moveData.price )
{
bestPrice = moveData.price;
}
}
}
}
return bestPrice;
}
// return n: 當前局面最佳走法是吃價值為n的敵方棋子
// NOT_FOUND: 當前局面最佳走法不是吃棋。
function eatByBestWay( chessData, camp )
{
var highestPrice = NOT_FOUND;
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
if ( chessData.chessStates[i] == OPEN &&
getCamp( chessData.chesses[i] ) == camp )
{
printDebug( chessData.chesses[i] + ":" );
var chess = chessData.chesses[i];
var eatPrice = existSimEatChance( chessData, i, chess );
var price = getPrice( chess );
printDebug( eatPrice + "," );
if ( eatPrice > highestPrice )
{
highestPrice = eatPrice;
}
}
}
return highestPrice;
}
// find the best way to walk or eat, then do it .
function walkOrEatByBestWay( chessData, camp, eatenPrices, n )
{
var allOpenMoveData = setAllOpenMoveData( chessData, camp );
var bestIndex = getBestMoveDataIndex( allOpenMoveData );
if ( bestIndex != NOT_FOUND )
{
var sourceIndex = allOpenMoveData[bestIndex].sourceIndex;
var destIndex = allOpenMoveData[bestIndex].destIndex;
if ( getCamp( chessData.chesses[sourceIndex] ) != camp )
{
printError( "錯誤: 誤判陣營" );
return false;
}
printDebug( " [敵行動:" + sourceIndex + "->" + destIndex + ": " + "敵方:" + camp );
if ( move( chessData, destIndex, sourceIndex, camp ) )
{
printDebug( " 移]" );
return true;
}
else if ( eat( chessData, destIndex, sourceIndex, camp ) )
{
var eatenChess = chessData.chesses[sourceIndex];
recordPrice( eatenPrices, getPrice( eatenChess ), n );
printDebug( " 被吃" + eatenChess + "]" );
return true;
}
else
{
printDebug( "]" );
}
}
return false;
}
// 找出此次最佳的走法的第一步
function getBestSimMoves( chessData, camp )
{
var bestPirces = getInitPrices();
var bestMoves = getInitMoves();
var eatBetter = false; // 吃的比被吃的優
var bestIndex = NOT_FOUND;
printDebug( "首選:" );
for ( var i = 0; i < gSimCount; i ++ )
{
var sEatText = getChessesByPrices( gSimEatPrices[i], camp );
var sEatenText = getChessesByPrices( gSimEatenPrices[i], camp );
if ( sEatText != null || sEatenText != null )
{
printDebug( "第" + i + "種走法:" + gSimMoves[i] );
}
if ( sEatText != null )
{
printDebug( "去吃:" + sEatText );
}
if ( sEatenText != null )
{
printDebug( "被吃:" + sEatenText );
}
// 若被吃得比吃得好 , 就不考慮了
if ( comparePrices( gSimEatPrices[i], gSimEatenPrices[i] ) != B_IS_BETTER )
{
if ( comparePrices( bestPirces, gSimEatPrices[i] ) == B_IS_BETTER )
{
bestPirces = copyPrices( gSimEatPrices[i] );
bestMoves = copyMoves( gSimMoves[i] );
bestIndex = i;
printDebug( "有比較好的走法:" + bestIndex );
}
eatBetter = true;
}
}
// 吃的的沒有比被吃的優, 那就從被吃的中找最少的了(前提是已經沒有可以棋子可以翻了)
if ( !eatBetter && noCloseChessNow( chessData ) )
{
bestPirces = getBiggestPrices(); // 因為要找最小,所以隨便給個初始值
printDebug( "次選:" );
for ( var i = 0; i < gSimCount; i ++ )
{
// 找被吃中最不優的
// 若有兩組以上被吃的權值相同,就比較它們吃的權值哪個較高
if ( ( comparePrices( bestPirces, gSimEatenPrices[i] ) == A_IS_BETTER ) ||
( comparePrices( bestPirces, gSimEatenPrices[i] ) == A_B_ARE_SAME &&
comparePrices( gSimEatPrices[bestIndex], gSimEatPrices[i] ) == B_IS_BETTER ) )
{
bestPirces = copyPrices( gSimEatenPrices[i] );
bestMoves = copyMoves( gSimMoves[i] );
bestIndex = i;
}
}
}
if ( bestIndex != NOT_FOUND )
{
log( "全部 " + gSimCount + " 走法 , " + "選擇第" + bestIndex + "種走法" );
printDebug( "最佳:" + bestIndex + "\n" + gSimEatPrices[bestIndex] );
printDebug( "" + gSimEatenPrices[bestIndex] );
}
else
{
printDebug( "沒找到走法" );
}
return bestMoves;
}
// 存入模擬結果
function saveSim( eatPrices, eatenPrices, moves )
{
if ( gSimCount < giSimLength )
{
printDebug( "[紀錄第" + gSimCount + "種走法]:" + moves );
gSimEatPrices[gSimCount] = copyPrices( eatPrices );
gSimEatenPrices[gSimCount] = copyPrices( eatenPrices );
gSimMoves[gSimCount] = copyMoves( moves );
gSimCount++;
}
}
// 初始化所有模擬記錄
function initSim()
{
gSimCount = 0;
for ( var i = 0; i < giSimLength; i ++ )
{
gSimEatPrices[i] = getInitPrices();
gSimEatenPrices[i] = getInitPrices();
gSimMoves[i] = getInitMoves();
}
}
// 遞迴方式模擬出n步內所有走法(翻棋除外)
function simAllWay( chessData, camp, eatPrices, eatenPrices, firstMoves, n, initN, iRecursionCount )
{
var nowRound = initN - n; // 目前進行的回合
printDebug( iRecursionCount + " [第" + nowRound + "回合: 剩" + ( giSimLength - gSimCount ) + "個位置可放模擬紀錄] " );
if ( n > 0 )
{
for ( var i = 0; i < INDEX_LENGTH && gSimCount < giSimLength; i ++ )
{
var sourceIndex = i;
if ( destIndex == NOT_FOUND ||
chessData.chessStates[sourceIndex] != OPEN ||
getCamp( chessData.chesses[sourceIndex] ) != camp )
{
continue;
}
var neighborIndexs;
var moveLength = INDEX_LENGTH; // 砲要檢查所有位置
var isCannon = getRank( chessData.chesses[sourceIndex] ) == RANK_CANNON ? true : false;
if ( !isCannon )
{
neighborIndexs = getNeighborIndexs( sourceIndex );
moveLength = 4; // 其餘棋子只需檢察四個鄰近方位
}
//printDebug( "_"+chessData.chesses[sourceIndex] + moveLength + "_" );
for ( var j = 0; j < moveLength; j ++ )
{
var destIndex = isCannon ? j : neighborIndexs[j];
if ( chessData.chessStates[destIndex] == CLOSE )
{
continue;
}
var canMove = false;
var canEat = false;
var innerChessData = copyChessData( chessData );
var innerEatPrices = copyPrices( eatPrices );
var innerEatenPrices = copyPrices( eatenPrices );
var debugString = "";
if ( move( innerChessData, destIndex, sourceIndex, camp ) )
{
debugString = " 移";
canMove = true;
}
else if ( eat( innerChessData, destIndex, sourceIndex, camp ) )
{
var eatChess = innerChessData.chesses[sourceIndex];
recordPrice( innerEatPrices, getPrice( eatChess ), nowRound );
debugString = " 吃" + eatChess;
canEat = true;
}
if ( canMove || canEat )
{
// 紀錄第一步就好
if ( n == initN )
{
firstMoves[0] = sourceIndex;
firstMoves[1] = destIndex;
}
printDebug( " [" + nowRound + "rd 我方:" + sourceIndex + "->" + destIndex + debugString + "]" );
walkOrEatByBestWay( innerChessData, getAnotherCamp( camp ), innerEatenPrices, nowRound );
if ( n > 0 && gSimCount < giSimLength )
{
simAllWay( innerChessData, camp, innerEatPrices, innerEatenPrices, firstMoves, n - 1, initN, iRecursionCount + 1 );
}
saveSim( innerEatPrices, innerEatenPrices, firstMoves );
}
}
}
}
else
{
saveSim( eatPrices, eatenPrices, firstMoves );
printDebug( "][OVER]" );
}
}
// 假設當前 index 這位子的棋子是 chess , 那麼是否有可以吃的對象
// 若有便回傳可吃棋子的權值,若沒有則回傳NOT_FOUND
function existSimEatChance( chessData, index, chess )
{
var simChessData = copyChessData( chessData );
var camp = getCamp( chess );
// 指定 index 位置是 OPEN狀態的 chess .
simChessData.chesses[index] = chess;
simChessData.chessStates[index] = OPEN;
var neighborIndexs = getNeighborIndexs( index );
var isCannon = ( getRank( chess ) == RANK_CANNON );
var moveLength = isCannon ? INDEX_LENGTH : 4;
for ( var j = 0; j < moveLength; j ++ )
{
var destIndex = isCannon ? j : neighborIndexs[j];
if ( canEat( simChessData, j, destIndex, camp ) )
{
return getPrice( simChessData.chesses[j] );
}
}
return NOT_FOUND;
}
// 假設當前 index 這位子的棋子是 chess , 那麼是否有立即被吃的可能
function existSimEatenChance( chessData, index, chess )
{
var simChessData = copyChessData( chessData );
var camp = getCamp( chess );
var enemyCamp = getAnotherCamp( camp ); // 敵方陣營
// 指定 index 位置是 OPEN狀態的 chess .
simChessData.chesses[index] = chess;
simChessData.chessStates[index] = OPEN;
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
if ( canEat( simChessData, index, i, enemyCamp ) )
{
return true;
}
}
return false;
}
// 侵略性移動棋子
function findInvasiveWalk( chessData, moveData )
{
var index = moveData.sourceIndex;
var priority = INVASIVE_MOVE;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
var chess = chessData.chesses[index];
var camp = getCamp( chess );
var enemyCamp = getAnotherCamp( camp ); // 敵方陣營
var existReachIndex = false; // 是否存在可到達的位置
var neighborIndexs = getNeighborIndexs( index );
var bestDistance = INDEX_LENGTH;
var bestIndex = NOT_FOUND;
var bestDirection = INIT_DIRECTION;
var bestPrice = INIT_PRICE;
initReachArea();
setReachArea( chessData, chess, index )
//printReachArea();
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
if ( !isAround( i, index ) && // 鄰近要用其它來判斷
canReach( i ) &&
chessData.chessStates[i] == OPEN &&
getCamp( chessData.chesses[i] ) == enemyCamp &&
AeatB( chess, chessData.chesses[i] ) )
{
//printDebug( "目標是" + chessData.chesses[i] + ": " );
var tempPrice = getPrice( chessData.chesses[i] );
var tempDistance = getIndexDistance( index, i );
var tempDirection = getIndexDirection( index, i );
var tempIndex = neighborIndexs[tempDirection];
if ( ( bestDistance > tempDistance ) ||
( bestDistance == tempDistance && randomChoice() ) )
{
if ( allowIndex( tempIndex ) &&
( canMove( chessData, tempIndex, index, camp ) ||
canEat( chessData, tempIndex, index, camp ) ) &&
!existSimEatenChance( chessData, tempIndex, chess ) ) // 移動到該處沒有立即危險
{
//printDebug( "目標是" + chessData.chesses[i] + ": " );
//printDebug( " 距離是" + tempDistance );
bestDistance = tempDistance;
bestDirection = tempDirection;
//printDebug( " 方向是" + bestDirection );
bestIndex = tempIndex;
bestPrice = tempPrice;
existReachIndex = true;
}
}
}
}
if ( existReachIndex )
{
printDebug( "~~~~~找到啦:位置:" + bestIndex + " , 方向:" + bestDirection );
moveData.destIndex = bestIndex;
moveData.priority = priority;
moveData.price = bestPrice;
found = true;
}
return found;
}
// 此次可到達 index
function canReach( index )
{
return ( gReachArea[index] == true );
}
function printReachArea()
{
for ( var i = 0; i < gReachArea.length; i ++ )
{
printDebug( gReachArea[i] + "." );
}
}
// 初始化gReachArea
function initReachArea()
{
for ( var i = 0; i < gReachArea.length; i ++ )
{
gReachArea[i] = false;
}
}
// 從sourceIndex出發,將可以走到的位置都紀錄在reachArea
function setReachArea( chessData, chess, sourceIndex )
{
var neighborIndexs = getNeighborIndexs( sourceIndex );
for ( var i = 0; i < 4; i ++ )
{
var index = neighborIndexs[i];
if ( index == NOT_FOUND )
{
continue;
}
else if ( canReach( index ) ) // 已經標記過的就不走
{
continue;
}
else if ( chessData.chessStates[index] == CLOSE ) // 有覆蓋棋子也沒辦法走
{
continue;
}
else if ( chessData.chessStates[index] == OPEN &&
!AeatB( chess, chessData.chesses[index] ) ) // 若有無法吃的開起棋子也要繞道
{
continue;
}
gReachArea[index] = true;
setReachArea( chessData, chess, index );
}
}
// 目前蓋住的敵方棋子都沒辦法吃 rank 等級的己方棋子
function noOpenDangerous( chessData, rank, camp )
{
var knownDangerousCount = 0; // 已經翻開或被吃的可吃我方rank的棋子數
var allDangerousCount = 0; // 所有可吃我方rank的棋子數
var enemyCamp = getAnotherCamp( camp ); // 敵方陣營
if ( rank == RANK_GENERAL )
{
allDangerousCount = 5; // 將怕五個兵
}
else
{
allDangerousCount = ( RANK_GENERAL - rank ) * 2 + 1;
}
for ( var i = 0; i < gEatenBlockQueue.length; i ++ )
{
if ( chessData.gEatenBlockQueue[i] == OPEN &&
getCamp( chessData.chesses[i] ) == enemyCamp &&
getRank( chessData.chesses[i] ) > rank )
{
knownDangerousCount ++;
}
}
var blackSize = getQueueSize( gEatenBlockQueue, INIT_EATEN_VALUE );
var redSize = getQueueSize( gEatenRedQueue, INIT_EATEN_VALUE );
if ( enemyCamp == BLACK )
{
// 黑方被吃棋子
for ( var i = 0; i < blackSize; i ++ )
{
if ( getRank( getChess( gEatenBlockQueue[i], BLACK ) ) > rank )
{
knownDangerousCount ++;
}
}
}
else
{
// 紅方被吃棋子
for ( var i = 0; i < redSize; i ++ )
{
if ( getRank( getChess( gEatenRedQueue[i], RED ) ) > rank )
{
knownDangerousCount ++;
}
}
}
return ( allDangerousCount == knownDangerousCount );
}
// 防禦性翻開棋子 ex. 將的斜角
function findDefensiveOpen( chessData, moveData, camp )
{
var index = moveData.sourceIndex;
var priority = DEFENSIVE_OPEN;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
var existGeneralDef = false;
var price = INIT_PRICE;
var enemyCamp = getAnotherCamp( camp ); // 敵方陣營
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
var indexs = getBevelNeighborIndexs( index );
for ( var j = 0; j < 4; j ++ )
{
if ( indexs[j] != NOT_FOUND &&
chessData.chessStates[indexs[j]] == OPEN &&
getRank( chessData.chesses[indexs[j]] ) == RANK_GENERAL &&
getCamp( chessData.chesses[indexs[j]] ) == camp &&
!cannonCanEaten( chessData, index, camp ) &&
!cannonCanEat( chessData, index, enemyCamp ) )
{
if ( existGeneralDef && randomChoice() )
{
continue;
}
existGeneralDef = true;
price = getPrice( chessData.chesses[indexs[j]] );
}
}
}
if ( existGeneralDef )
{
moveData.destIndex = index;
moveData.priority = priority;
moveData.price = price;
found = true;
}
return found;
}
// 侵略性翻開棋子 ex. 炮旁邊, 相以上希望翻出炮來跳吃 .
function findInvasiveOpen( chessData, moveData, camp )
{
var index = moveData.sourceIndex;
var priority = INVASIVE_OPEN;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
var neighborIndexs = getNeighborIndexs( index );
var enemyCamp = getAnotherCamp( camp ); // 敵方陣營
var existCannon = false; // 鄰近位置有炮
var existJumpEat = false; // 存在跳吃機會
var existDangerous = false; // 鄰近位置有較大的敵方棋子
var jumpEatPrice = INIT_PRICE;
// 檢查鄰近位置是否有炮
for ( var i = 0; i < 4; i ++ )
{
var enemyIndex = neighborIndexs[i];
// 也檢查此處鄰近有無相以上的敵軍
if ( enemyIndex != NOT_FOUND &&
chessData.chessStates[enemyIndex] == OPEN &&
getCamp( chessData.chesses[enemyIndex] ) == enemyCamp &&
getRank( chessData.chesses[enemyIndex] ) == RANK_CANNON &&
!existSimEatenChance( chessData, enemyIndex, getChess( RANK_MINISTER, camp ) ) )
{
if ( existCannon && randomChoice() )
{
continue;
}
existCannon = true;
}
}
// 檢查是否有相士將在炮的射程內 .
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
var enemyIndex = neighborIndexs[i];
// 也檢查此處若翻開是砲,有無立即被吃危險
if ( enemyIndex != NOT_FOUND &&
chessData.chessStates[i] == OPEN &&
getCamp( chessData.chesses[i] ) == enemyCamp &&
getRank( chessData.chesses[i] ) >= RANK_MINISTER &&
existOneInterval( chessData, i, index ) &&
!cannonCanEaten( chessData, enemyIndex, camp ) )
{
var tempPrice = getPrice( chessData.chesses[i] );
if ( jumpEatPrice <= tempPrice )
{
jumpEatPrice = tempPrice;
}
existJumpEat = true;
}
}
// 檢查鄰近位置是否有較大的敵方棋子
for ( var i = 0; i < 4; i ++ )
{
var enemyIndex = neighborIndexs[i];
if ( enemyIndex != NOT_FOUND &&
chessData.chessStates[enemyIndex] == OPEN &&
getCamp( chessData.chesses[enemyIndex] ) == enemyCamp &&
getRank( chessData.chesses[enemyIndex] ) > RANK_CAR )
{
existDangerous = true;
}
}
if ( existCannon && !existDangerous )
{
moveData.destIndex = index;
moveData.priority = priority;
moveData.price = jumpEatPrice;
found = true;
}
else if ( existCannon && !existDangerous )
{
moveData.destIndex = index;
moveData.priority = priority;
moveData.price = getPrice( getChess( RANK_CANNON, camp ) );
found = true;
}
return found;
}
// 防禦性移動棋子
function findDefensiveMove( chessData, moveData )
{
var index = moveData.sourceIndex;
var priority = DEFENSIVE_MOVE;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
return found;
}
// 檢查index是否是死胡同
function noWayOut( chessData, index )
{
var neighborIndexs = getNeighborIndexs( index );
var wayCount = 0; // 出口數量
for ( var i = 0; i < 4; i ++ )
{
var nIndex = neighborIndexs[i];
if ( nIndex != NOT_FOUND &&
chessData.chessStates[nIndex] == EATEN )
{
return false;
}
}
return true;
}
// 棋子有被吃危險,嘗試逃脫
function findNormalEscape( chessData, moveData )
{
var index = moveData.sourceIndex;
var priority = NORMAL_ESCAPE;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
var price = getPrice( chessData.chesses[index] );
var camp = getCamp( chessData.chesses[index] );
var enemyCamp = getAnotherCamp( camp );
var bestPrice = price;
var bestIndex = NOT_FOUND;
// 有被吃危險的最佳作法
var dangerousBestPrice = INIT_PRICE;
var dangerousBestIndex = NOT_FOUND;
var dangerousFound = false;
var targetIndex = NOT_FOUND;
var deadWay = true; // 檢查是否走了後會無路可逃
for ( var i = 0; i < INDEX_LENGTH; i ++ )
{
// 若可能被鄰居吃的時候
if ( canEat( chessData, index, i, enemyCamp ) )
{
//printDebug( " 可能被吃: " );
targetIndex = i;
var tempMoveData2 = newMoveData( index );
var tempMoveData3 = newMoveData( index );
var beClear = false;
var enemyPrice = getPrice( chessData.chesses[i] );
// 若己方有棋子可以直接吃那顆具威脅的敵方棋子,那就不用逃脫了。
if ( existSimEatenChance( chessData, i, chessData.chesses[i] ) )
{
printDebug( " DEAT " );
}
// 對方若吃,便會被我方吃 。
else if ( enemyPrice >= price &&
existSimEatenChance( chessData, index, chessData.chesses[i] ) )
{
printDebug( " BOT " );
// tell other function there exist a chess with price in balance of terror .
}
// 先找有無逃脫可能
else if ( findWalk( chessData, tempMoveData2, NOT_ASSIGNED ) )
{
var tempDestIndex = tempMoveData2.destIndex;
var tempDeadWay = noWayOut( chessData, tempDestIndex );
// 有活路的走法就紀錄起來
if ( !tempDeadWay )
{
deadWay = false;
}
// 此走法沒活力,且之前有活路,那就不用考慮了
else if ( !deadWay )
{
continue;
}
if ( bestPrice < price )
{
bestIndex = tempDestIndex;
bestPrice = price;
found = true;
}
}
// 再找自殺吃棋的機會
else if ( findNormalEat( chessData, tempMoveData3, EAT_FIRST_PRINCIPLE, 1 ) )
{
if ( ( bestPrice < tempMoveData3.price ) ||
( bestPrice == tempMoveData3.price && randomChoice() ) )
{
bestIndex = tempMoveData3.destIndex;
bestPrice = tempMoveData3.price;
found = true;
}
}
}
}
if ( found )
{
moveData.priority = priority;
moveData.price = bestPrice;
moveData.destIndex = bestIndex;
moveData.targetIndex = targetIndex;
}
return found;
}
// 嘗試吃棋子
function findNormalEat( chessData, moveData, principle, n )
{
var index = moveData.sourceIndex;
var priority = NORMAL_EAT;
var found = false;
if ( higherPriority( moveData.priority, priority ) )
{
return false; // 若之前已有更高的移動權值,直接跳出。
}
var rank = getRank( chessData.chesses[index] );
var camp = getCamp( chessData.chesses[index] );
var price = getPrice( chessData.chesses[index] );
var enemyCamp = getAnotherCamp( camp );
var bestPrice = INIT_PRICE;
var bestIndex = NOT_FOUND;
var existProtectToEat = false; // 為了保護己方棋子不被吃而吃對方
var neighborIndexs;
var moveLength = INDEX_LENGTH; // 砲要檢查所有位置
var isCannon = getRank( chessData.chesses[index] ) == RANK_CANNON ? true : false;
if ( !isCannon )
{
neighborIndexs = getNeighborIndexs( index );
moveLength = 4; // 其餘棋子只需檢察四個鄰近方位
}
for ( var j = 0; j < moveLength; j ++ )
{
var destIndex = isCannon ? j : neighborIndexs[j];
var tempChessData = copyChessData( chessData );
var enemyPrice = getPrice( chessData.chesses[destIndex] );
// 模擬吃之後的情形
if ( eat( tempChessData, destIndex, index, camp ) )
{
// 此被吃棋B是否有機會可以直接吃我方某棋A,若有則檢查A權值是否比B大
var tempEnemyPrice = existSimEatChance( chessData, destIndex, chessData.chesses[destIndex] );
//printDebug( " D:" + destIndex );
if ( tempEnemyPrice != NOT_FOUND )
{
enemyPrice = tempEnemyPrice;
existProtectToEat = true;
//printDebug( ">" + bestPrice + enemyPrice );
}
// 檢查吃之後會不會有立即危險
var beSafe = safeState( tempChessData, destIndex ); //XX
//var eatenPrice = price;