-
Notifications
You must be signed in to change notification settings - Fork 11
/
musesfish_pvs_20210529_fixed.py
executable file
·1130 lines (981 loc) · 46 KB
/
musesfish_pvs_20210529_fixed.py
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
#!/usr/bin/env pypy
# -*- coding: utf-8 -*-
#Updated by Si Miao 2021/05/20
from __future__ import print_function
import re, sys, time
from itertools import count
from collections import namedtuple
import random
from board import board, common_20210529_fixed as common, library
from copy import deepcopy
import readline
B = board.Board()
piece = {'P': 44, 'N': 108, 'B': 23, 'R': 233, 'A': 23, 'C': 101, 'K': 2500}
put = lambda board, i, p: board[:i] + p + board[i+1:]
r = {'R': 2, 'N': 2, 'B': 2, 'A': 2, 'C': 2, 'P': 5}
b = {'r': 2, 'n': 2, 'b': 2, 'a': 2, 'c': 2, 'p': 5}
di = {0: {True: deepcopy(r), False: deepcopy(b)}}
sumall = {
0: {True: sum(di[0][True][key] for key in di[0][True]), False: sum(di[0][False][key] for key in di[0][False])}}
# 子力价值表参考“象眼”
cache = {}
forbidden_moves = set()
kaijuku = deepcopy(library.kaijuku)
def setcache(bo):
if bo not in cache:
cache[bo] = 1
else:
cache[bo] += 1
def resetrbdict():
global r, b, di, sumall
r = {'R': 2, 'N': 2, 'B': 2, 'A': 2, 'C': 2, 'P': 5}
b = {'r': 2, 'n': 2, 'b': 2, 'a': 2, 'c': 2, 'p': 5}
di = {0: {True: deepcopy(r), False: deepcopy(b)}}
sumall = {
0: {True: sum(di[0][True][key] for key in di[0][True]), False: sum(di[0][False][key] for key in di[0][False])}}
pst = deepcopy(common.pst)
discount_factor = common.discount_factor # 1.6
A0, I0, A9, I9 = 12 * 16 + 3, 12 * 16 + 11, 3 * 16 + 3, 3 * 16 + 11
'''
D: 暗车
E: 暗马
F: 暗相
G: 暗士
H: 暗炮
I: 暗车
'''
initial_covered = (
' \n' # 0
' \n' # 1
' \n' # 2
' defgkgfed \n' # 3
' ......... \n' # 4
' .h.....h. \n' # 5
' i.i.i.i.i \n' # 6
' ......... \n' # 7
' ......... \n' # 8
' I.I.I.I.I \n' # 9
' .H.....H. \n' # 10
' ......... \n' # 11
' DEFGKGFED \n' # 12
' \n' # 13
' \n' # 14
' ' # 15
)
# Lists of possible moves for each piece type.
N, E, S, W = -16, 1, 16, -1
directions = {
'P': (N, W, E),
'I': (N, ), #暗兵
'N': (N+N+E, E+N+E, E+S+E, S+S+E, S+S+W, W+S+W, W+N+W, N+N+W),
'E': (N+N+E, E+N+E, W+N+W, N+N+W), #暗马
'B': (2 * N + 2 * E, 2 * S + 2 * E, 2 * S + 2 * W, 2 * N + 2 * W),
'F': (2 * N + 2 * E, 2 * N + 2 * W), #暗相
'R': (N, E, S, W),
'D': (N, E, W), #暗车
'C': (N, E, S, W),
'H': (N, E, S, W), #暗炮
'A': (N+E, S+E, S+W, N+W),
'G': (N+E, N+W), #暗士
'K': (N, E, S, W)
}
uni_pieces = {
'.': '.',
'R': '\033[31m俥\033[0m',
'N': '\033[31m傌\033[0m',
'B': '\033[31m相\033[0m',
'A': '\033[31m仕\033[0m',
'K': '\033[31m帅\033[0m',
'P': '\033[31m兵\033[0m',
'C': '\033[31m炮\033[0m',
'D': '\033[31m暗\033[0m',
'E': '\033[31m暗\033[0m',
'F': '\033[31m暗\033[0m',
'G': '\033[31m暗\033[0m',
'H': '\033[31m暗\033[0m',
'I': '\033[31m暗\033[0m',
'r': '车',
'n': '马',
'b': '象',
'a': '士',
'k': '将',
'p': '卒',
'c': '炮',
'd': '暗',
'e': '暗',
'f': '暗',
'g': '暗',
'h': '暗',
'i': '暗'
}
MATE_LOWER = piece['K'] - (2*piece['R'] + 2*piece['N'] + 2*piece['B'] + 2*piece['A'] + 2*piece['C'] + 5*piece['P'])
MATE_UPPER = piece['K'] + (2*piece['R'] + 2*piece['N'] + 2*piece['B'] + 2*piece['A'] + 2*piece['C'] + 5*piece['P'])
# The table size is the maximum number of elements in the transposition table.
TABLE_SIZE = 1e7
# Constants for tuning search
QS_LIMIT = 219
EVAL_ROUGHNESS = 13
DRAW_TEST = True
THINK_TIME = 1
###############################################################################
# Mapping
# To be more convenient, we initialize the mapping as a global const dictionary
###############################################################################
mapping = {}
average = {0: {}}
# mapping: 暗子到明子映射
# di:
# version: 版本号,用于将来可能的递归(还没实现)
# True: 红方
# 'R', 'N', ... 'P': 暗子中可能的数目
# False: 黑方
# 'r', 'n', ..., 'p': 暗子中可能的数目
# sumall:
# version: 版本号,用于将来可能的递归(还没实现)
# True: 红方可能暗子数量之和 = 红方所有暗子+不确定子+黑方吃掉红方的子。
# False: 黑方可能暗子数量之和 = 黑方所有暗子+不确定子+红方吃掉黑方的子。
# //注意,sumall[version][True] >= 红方所有暗子+不确定子, 因为黑方可能不知道红方吃了黑方什么子
# average:
# version: 版本号,用于将来可能的递归(还没实现)
# True: 红方
# Ture: 不确定子
# pos: 棋盘位置
# False: 正在睡觉的暗子
# False: 黑方
# Ture: 不确定子
# pos: 棋盘位置
# False: 正在睡觉的暗子
###############################################################################
# Chess logic
###############################################################################
class Position(namedtuple('Position', 'board score turn version')):
""" A state of a chess game
board -- a 256 char representation of the board
score -- the board evaluation
"""
def set(self):
self.che = 0
self.che_opponent = 0
self.zu = 0
self.covered = 0
self.covered_opponent = 0
self.endline = 0
self.score_rough = 0
for i in range(51, 204):
if i >> 4 == 3:
if self.board[i] in 'defgrnc':
self.endline += 1
p = self.board[i]
if p in 'RNBAKCP':
self.score_rough += pst[p][i]
elif p in 'DEFGHI':
# self.score_rough += average[self.version][self.turn][False]
self.covered += 1
elif p in 'U':
self.score_rough += average[self.version][self.turn][True][i]
self.covered += 1
elif p in 'rnbakcp':
self.score_rough -= pst[p.upper()][254 - i]
elif p in 'defghi':
# self.score_rough -= average[self.version][not self.turn][False]
self.covered_opponent += 1
elif p in 'u':
self.score_rough -= average[self.version][not self.turn][True][254 - i]
self.covered_opponent += 1
if p == 'R':
self.che += 1
if p == 'r':
self.che_opponent += 1
if p == 'P':
self.zu += 1
return self
def gen_moves(self):
# For each of our pieces, iterate through each possible 'ray' of moves,
# as defined in the 'directions' map. The rays are broken e.g. by
# captures or immediately in case of pieces such as knights.
for i in range(51, 204):
p = self.board[i]
if not p.isupper() or p == 'U': continue
if p == 'K':
for scanpos in range(i - 16, A9, -16):
if self.board[scanpos] == 'k':
yield (i, scanpos)
elif self.board[scanpos] != '.':
break
if p in ('C', 'H'): #明暗炮
for d in directions[p]:
cfoot = 0
for j in count(i+d, d):
q = self.board[j]
if q.isspace(): break
if cfoot == 0 and q == '.': yield (i, j)
elif cfoot == 0 and q != '.': cfoot += 1
elif cfoot == 1 and q.islower(): yield (i, j); break
elif cfoot == 1 and q.isupper(): break;
continue
for d in directions[p]:
for j in count(i+d, d):
q = self.board[j]
# Stay inside the board, and off friendly pieces
if q.isspace() or q.isupper(): break
# 过河的卒/兵才能横着走
if p == 'P' and d in (E, W) and i > 128: break
# j & 15 等价于 j % 16但是更快
elif p == 'K' and (j < 160 or j & 15 > 8 or j & 15 < 6): break
elif p == 'G' and j != 183: break # 暗士, 花心坐标: (11, 7), 11 * 16 + 7 = 183
elif p in ('N', 'E'): # 暗马
n_diff_x = (j - i) & 15
if n_diff_x == 14 or n_diff_x == 2:
if self.board[i + (1 if n_diff_x == 2 else -1)] != '.': break
else:
if j > i and self.board[i + 16] != '.': break
elif j < i and self.board[i - 16] != '.': break
elif p in ('B', 'F') and self.board[i + d // 2] != '.':break
# Move it
yield (i, j)
# Stop crawlers from sliding, and sliding after captures
if p in 'PNBAKIEFG' or q.islower(): break
def rotate(self):
''' Rotates the board, preserving enpassant '''
p = Position(
self.board[-2::-1].swapcase() + " ", -self.score, not self.turn, self.version)
p.set()
return p
@staticmethod
def rotate_new(board, score, turn, version):
p = Position(
board[-2::-1].swapcase() + " ", -score, not turn, version)
p.set()
return p
def nullmove(self):
''' Like rotate, but clears ep and kp '''
return self.rotate()
def move(self, move):
i, j = move
# Copy variables and reset ep and kp
movevalue = self.value(move)
score = self.score + movevalue if movevalue < MATE_UPPER else MATE_UPPER
# Actual move
if self.board[i] in 'RNBAKCP':
board = put(self.board, j, self.board[i])
else:
board = put(self.board, j, 'U')
board = put(board, i, '.')
return Position.rotate_new(board, score, self.turn, self.version)
def mymove_check(self, move, discount_red=True, discount_black=False):
i, j = move
# Copy variables and reset ep and kp
############################################################################
# TODO: Evaluate the score of each move!
# The following line is NOT implemented. However, it is extremely important!
# score = self.score + self.value(move)
############################################################################
# Actual move
# put = lambda board, i, p: board[:i] + p + board[i + 1:]
eat = self.board[j]
dst = None
checkmate = False
if self.board[j] == 'k':
checkmate = True
if self.board[j] in "defghi":
dst = None
if self.turn:
dst = mapping[j]
else:
dst = mapping[254-j]
# 这里是吃暗子的逻辑
# 在这个简易程序中,假设AI执黑,玩家执红。
# 那么AI其实是不知道玩家吃了自己什么暗子的
# 因此在玩家执红吃黑暗子时,黑方的暗子集合并不会更新。
# 本程序设置了discount_red/black开关处理这一逻辑。
if self.turn:
if discount_black:
dst2 = dst.lower()
di[self.version][False][dst2] -= 1
else:
if discount_red:
dst2 = dst.upper()
di[self.version][True][dst2] -= 1
if self.board[i] in "RNBAKCP":
board = put(self.board, j, self.board[i])
else:
if self.turn:
board = put(self.board, j, mapping[i])
dst2 = mapping[i].upper()
di[self.version][True][dst2] -= 1
else:
board = put(self.board, j, mapping[254 - i].upper())
dst2 = mapping[254 - i].lower()
di[self.version][False][dst2] -= 1
board = put(board, i, '.')
sumall[self.version][True] = sum(di[self.version][True][key] for key in di[self.version][True])
sumall[self.version][False] = sum(di[self.version][False][key] for key in di[self.version][False])
return Position.rotate_new(board, self.score, self.turn, self.version), checkmate, eat, dst
def value(self, move):
i, j = move
p, q = self.board[i], self.board[j].upper()
possible_che = 0 if sumall[self.version][self.turn] == 0 else self.covered * di[self.version][self.turn][
'R' if self.turn else 'r']/sumall[self.version][self.turn]
possible_che_opponent = 0 if sumall[self.version][not self.turn] == 0 else self.covered_opponent * di[self.version][not self.turn][
'r' if self.turn else 'R'] / sumall[self.version][not self.turn]
# Actual move
# 这里有一个隐藏的很深的BUG。如果对手走出将帅对饮的一步棋,score应该很高(因为直接赢棋)。但由于减了pst[p][i], 减了自己的皇上,所以代码中的score是接近0的。
# 因此,当对方是老将时应直接返回最大值,不能考虑己方。
if q == 'K':
return MATE_UPPER
if p in 'RNBAKCP':
score = pst[p][j] - pst[p][i]
if p == 'C':
# 以下为沉底炮逻辑
if (i >> 4) != 3 and (j >> 4) == 3 and self.endline <= 2:
if (j == 51 or j == 52) and self.board[53] == 'f' and self.board[54] == 'g':
pass
elif (j == 59 or j == 58) and self.board[57] == 'f' and self.board[56] == 'g':
pass
else:
if self.endline >= 1:
score -= 30
else:
score -= 55
######################################################################################################################
# 2021/05/28 关于score -= 30的解释
# 揭棋的沉底炮比象棋的沉底炮威胁更大,因为揭棋通常没有连环象的防守,容易闷宫,也没有连环士的防守,一旦对手出车,可以瞄准底暗士叫杀,或捉底暗象。
# 此外,揭棋中沉底炮可以牵制暗子,而明棋中只能牵制子力价值较弱的士象。
# 因此,我在board/common.py中,给沉底炮赋予了很高的分数。
# 但是,如果对方底线暗子已出,则牵制力大大减弱。盲目下底炮就不是一个好事情。
# 2021/05/29 改为score -= 55
######################################################################################################################
if (i >> 4) == 3 and (j >> 4) != 3 and self.endline <= 2:
if (i == 51 or i == 52) and self.board[53] == 'f' and self.board[54] == 'g':
pass
elif (i == 59 or i == 58) and self.board[57] == 'f' and self.board[56] == 'g':
pass
else:
if self.endline >= 1:
score += 30
else:
score += 55
# 以下为空头炮逻辑
if i & 15 == 7 and j & 15 != 7:
# 如果走子撤走了空头炮,扣分!
cnt = 0
for scanpos in range(i - 16, A9, -16):
if self.board[scanpos] == 'k':
if cnt == 2:
score -= 20 # 空头炮奖励
if cnt >= 3 or (self.che >= self.che_opponent and self.che > 0):
score -= 30
break
elif self.board[scanpos] != '.':
break
cnt += 1
if i & 15 != 7 and j & 15 == 7:
# 如果走子形成了空头炮,加分!
cnt = 0
# i & 15 != 7很重要,如果没有这句话,AI就不停地直线走来走去赚空头炮积分
for scanpos in range(j - 16, A9, -16):
if self.board[scanpos] == 'k':
if cnt == 2:
score += 20 # 空头炮奖励91 Clones
if cnt >= 3 or (self.che >= self.che_opponent and self.che > 0):
score += 30
break
elif self.board[scanpos] != '.':
break
cnt += 1
if i & 15 == 7 and j & 15 == 7:
if i < j: # 炮往后撤, 可能空头变非空头哦!
kongtou = True
for scanpos in range(j - 16, i, -16):
if self.board[scanpos] != '.':
kongtou = False
break
cnt = 0
for scanpos in range(i - 16, A9, -16):
if self.board[scanpos] == 'k':
if not kongtou:
if cnt == 2:
score -= 20 # 空头炮奖励
if cnt >= 3 or (self.che >= self.che_opponent and self.che > 0):
score -= 30
else:
if cnt < 4 and (cnt + abs(i - j)//16) >= 4:
score += 30
break
elif self.board[scanpos] != '.':
break
cnt += 1
else: # 炮往前走, 可能非空头变空头!
kongtou = True
for scanpos in range(i - 16, j, -16):
if self.board[scanpos] != '.':
kongtou = False
break
cnt = 0
for scanpos in range(j - 16, A9, -16):
if self.board[scanpos] == 'k':
if not kongtou:
if cnt == 2:
score += 20 # 空头炮奖励
if cnt >= 3 or (self.che >= self.che_opponent and self.che > 0):
score += 30
else:
if cnt < 4 and (cnt + abs(i - j) // 16) >= 4:
score -= 30
break
elif self.board[scanpos] != '.':
break
cnt += 1
elif p == 'R':
if self.board[51] not in 'dr' and self.board[54] != 'a' and self.board[71] != 'a':
if j & 15 == 6 and i & 15 != 6:
score += 30 # 如果对方暗车出动,相应侧又没有士的防守,抓紧抢占肋道
if j & 15 != 6 and i & 15 == 6:
score -= 20
if self.board[59] not in 'dr' and self.board[56] != 'a' and self.board[71] != 'a':
if j & 15 == 8 and i & 15 != 8:
score += 30 # 如果对方暗车出动,相应侧又没有士的防守,抓紧抢占肋道
if j & 15 != 8 and i & 15 == 8:
score -= 20
if (i >> 4) == 3 and (j >> 4) != 3 and (self.endline <= 1 or self.score_rough < -150):
if self.endline <= 1:
score += 30
elif self.score_rough < -150:
score += 40
if (i >> 4) != 3 and (j >> 4) == 3 and (self.endline <= 1 or self.score_rough < -150):
if self.endline <= 1:
score -= 30
elif self.score_rough < -150:
score -= 40
if self.board[i-32] == 'i':
score -= (average[self.version][not self.turn][True][270-i] - average[self.version][not self.turn][False])
else:
# 不确定明子的平均价值计算算法:
# 假设某一方可能的暗子是 两车一炮。
# 则在某位置处不确定明子的价值为 (车在该处的价值*2 + 炮在该处的价值)/(2+1)。
# 为了加速计算,这一数值已经被封装到了average这一字典中并预先计算(Pre-compute)。
score = average[self.version][self.turn][True][j] - average[self.version][self.turn][False] + 20 # 相应位置不确定明子的平均价值 - 暗子
if p == 'D':
minus = 30*(possible_che_opponent // 2 + self.che_opponent)
# score -= 10 20210528
score -= minus # 暗车溜出,扣分! 扣的分数和对方剩余车的个数有关
if self.score_rough < -150:
score -= minus//2
if self.che_opponent > 0:
score -= minus//4 * self.che_opponent
if i == 203 and j == 202:
if self.board[j] in 'rnc':
score += pst[self.board[j].upper()][52] # 254-202=52
elif self.board[j] == 'p':
score += minus//2
if i == 195 and j == 196:
if self.board[j] in 'rnc':
score += pst[self.board[j].upper()][58] # 254-196=58
elif self.board[j] == 'p':
score += minus//2
elif p == 'G':
# 1 2 3 4 5 6 7 8 9
# i
# h
# g
# f
# e
# d
# c
# b
# a
# 9 8 7 6 5 4 3 2 1
# 195 - 16x + y
# 对手9路暗车出动, 己方可以考虑出将/出帅助攻。翻开四路暗士, 查看四路肋道车的数量。如果己方车数量大于对方车,鼓励翻动士助攻
def calc():
shi_possibility = 0 if sumall[self.version][not self.turn] == 0 else di[self.version][not self.turn]['a' if self.turn else 'A']/sumall[self.version][not self.turn]
base_possibility = 1
if self.board[56] == 'G':
base_possibility *= (1 - shi_possibility)
if self.board[54] == 'G':
base_possibility *= (1 - shi_possibility)
return base_possibility
if i == 200 and self.board[59] not in 'dr' and self.board[56] != 'a' and self.board[71] != 'a':
cheonleidao = 0
che_opponent_onleidao = 0
for scanpos in range(184, 51, -16):
if self.board[scanpos] == 'R':
cheonleidao += 1
elif self.board[scanpos] == 'r':
che_opponent_onleidao += 1
if cheonleidao > che_opponent_onleidao and possible_che >= possible_che_opponent:
score += (40 * calc())
# 对手1路暗车出动, 己方可以考虑出将/出帅助攻。翻开六路暗士, 查看六路肋道车的数量。如果己方车数量大于对方车,鼓励翻动士助攻
if i == 198 and self.board[51] not in 'dr' and self.board[54] != 'a' and self.board[71] != 'a':
cheonleidao = 0
che_opponent_onleidao = 0
for scanpos in range(182, 51, -16):
if self.board[scanpos] == 'R':
cheonleidao += 1
elif self.board[scanpos] == 'r':
che_opponent_onleidao += 1
if cheonleidao > che_opponent_onleidao and possible_che >= possible_che_opponent:
score += (40 * calc())
elif sumall[self.version][self.turn] > 0 and \
(di[self.version][self.turn]['P' if self.turn else 'p'] * self.covered/sumall[self.version][self.turn] <= 2):
score -= 20 # 如果当前暗子中兵过多,翻士容易翻出窝心兵,不利于防守!
elif p == 'E':
if i == 196 and j == 165 and self.board[149] == 'I': # 对方车从3,7线杀出,翻动暗马保住暗兵
for scanpos in range(133, 51, -16):
if self.board[scanpos] == 'r':
score += average[self.version][self.turn][False]//2
elif self.board[scanpos] != '.':
break
if i == 202 and j == 169 and self.board[153] == 'I':
for scanpos in range(137, 51, -16):
if self.board[scanpos] == 'r':
score += average[self.version][self.turn][False]//2
elif self.board[scanpos] != '.':
break
elif p == 'H':
# 暗炮进四
if i == 164 and j == 100:
if self.board[83] == 'a' or self.board[85] == 'a' or self.board[115] == 'a' or self.board[117] == 'a':
score -= average[self.version][self.turn][False] // 2
else:
count = 0
if self.board[84] in 'hcn':
count += 1
if self.board[99] in 'icn':
count += 1
if self.board[101] in 'icn':
count += 1
if count >= 2 and self.board[86] != 'h':
score += average[self.version][not self.turn][False] // 2
else:
score += average[self.version][not self.turn][False] // 4
if i == 170 and j == 106:
if self.board[89] == 'a' or self.board[91] == 'a' or self.board[121] == 'a' or self.board[123] == 'a':
score -= average[self.version][self.turn][False] // 2
else:
count = 0
if self.board[90] == 'h':
count += 1
if self.board[105] == 'i':
count += 1
if self.board[107] == 'i':
count += 1
if count == 3 or (count == 2 and self.board[90] != 'h') or (self.board[107] == 'i' and self.board[139] in 'rp'):
score += average[self.version][not self.turn][False] // 2
else:
score += average[self.version][not self.turn][False] // 4
# 炮压暗马
if i == 164 and j == 68 and self.board[52] == 'e':
prob = 0 if sumall[self.version][self.turn] == 0 else (di[self.version][self.turn]['P' if self.turn else 'p']/sumall[self.version][self.turn]) # 平均可能卒的个数
coeff = average[self.version][not self.turn][False]
bonus = round(prob * coeff/2)
score += bonus//2
if self.board[53] != '':
score += bonus
if self.board[51] == 'd':
score += bonus
if i == 170 and j == 74 and self.board[58] == 'e':
prob = 0 if sumall[self.version][self.turn] == 0 else (di[self.version][self.turn]['P' if self.turn else 'p'] / sumall[self.version][self.turn]) # 平均可能卒的个数
coeff = average[self.version][not self.turn][False]
bonus = round(prob * coeff / 2)
score += bonus // 2
if self.board[57] != '':
score += bonus
if self.board[59] == 'd':
score += bonus
# 暗炮搏马
if ((i == 164 and j == 52 and self.board[51] == 'd') or (
i == 170 and j == 58 and self.board[59] == 'd')): # TODO: 使用更智能的方式处理博子
if self.che < self.che_opponent:
score -= 50
elif self.che == self.che_opponent:
if (i == 164 and self.board[148] == 'p') or (i == 170 and self.board[154] == 'p') or self.score_rough > 200:
pass
elif (i == 164 and (self.board[151] != 'N' or self.board[135] != 'p')) and (i == 170 and (self.board[159] != 'N' or self.board[143] != 'p')):
score -= 30
elif p == 'I':
if self.board[i - 32] in 'rp': # 原先是RP, 这是个BUG!现解决
score -= average[self.version][self.turn][False]//2
elif self.board[i - 32] == 'n': # 之前是N, 不正确,已更正!
score += 20
else:
score += 5
# Capture
if q.isupper():
k = 254 - j
if q in 'RNBAKCP':
score += pst[q][k]
if q in 'CNRA':
score += pst[q][k] // 3
else:
if q != 'U':
score += average[self.version][not self.turn][False]
# if self.score_rough < -100 or self.score_rough > 100:
# score += average[self.version][not self.turn][False]//2
else:
score += average[self.version][not self.turn][True][k]
# if self.score_rough < -100 or self.score_rough > 100:
# score += average[self.version][not self.turn][True][k]//4
if q == 'D':
addition = 30*(possible_che//2+self.che)
score += addition # 吃对方暗车,加分! 加的分数和己方剩余车的个数相关,如果本方没有车了,那吃个暗车不算太大的收益
if self.score_rough > 150:
score += addition//2
# if self.che_opponent == 0: 20210528
if self.che > 0:
score += addition//4 * self.che
return score
###############################################################################
# Search logic
###############################################################################
# lower <= s(pos) <= upper
Entry = namedtuple('Entry', 'lower upper')
class Searcher:
def __init__(self):
self.tp_score = {}
self.tp_move = {}
self.history = set()
self.nodes = 0
def alphabeta(self, pos, alpha, beta, depth, root=True, nullmove=False):
""" returns r where
s(pos) <= r < gamma if gamma > s(pos)
gamma <= r <= s(pos) if gamma <= s(pos)"""
#print(self.tp_score)
if root:
self.tp_score = {}
self.tp_move = {}
self.nodes += 1
# Depth <= 0 is QSearch. Here any position is searched as deeply as is needed for
# calmness, and from this point on there is no difference in behaviour depending on
# depth, so so there is no reason to keep different depths in the transposition table.
depth = max(depth, 0)
# Sunfish is a king-capture engine, so we should always check if we
# still have a king. Notice since this is the only termination check,
# the remaining code has to be comfortable with being mated, stalemated
# or able to capture the opponent king.
if pos.score <= -MATE_LOWER:
print("depth=%s"%depth, " return from pos.score <= -MATE_LOWER")
return -MATE_UPPER
# Look in the table if we have already searched this position before.
# We also need to be sure, that the stored search was over the same
# nodes as the current search.
entry = self.tp_score.get((pos, depth, root), Entry(-MATE_UPPER, MATE_UPPER))
if entry.lower >= beta and (not root or self.tp_move.get(pos) is not None):
return entry.lower
if entry.upper < alpha:
return entry.upper
# Here extensions may be added
# Such as 'if in_check: depth += 1'
# Generator of moves to search in order.
# This allows us to define the moves, but only calculate them if needed.
# First try not moving at all. We only do this if there is at least one major
# piece left on the board, since otherwise zugzwangs are too dangerous.
if nullmove and depth > 0 and not root and any(c in pos.board for c in 'RNC'):
val = -self.alphabeta(pos.nullmove(), -beta, 1-beta, depth-3, root=False)
if val >= beta and self.alphabeta(pos, alpha, beta, depth - 3, root=False):
return val
# For QSearch we have a different kind of null-move, namely we can just stop
# and not capture anything else.
if depth == 0:
return pos.score
# Then killer move. We search it twice, but the tp will fix things for us.
# Note, we don't have to check for legality, since we've already done it
# before. Also note that in QS the killer must be a capture, otherwise we
# will be non deterministic.
best = -MATE_UPPER
killer = self.tp_move.get(pos)
# Then all the other moves
mvBest = None
moves = sorted(pos.gen_moves(), key=pos.value, reverse=True)
for move in [killer] + moves:
if (move is not None) and pos.board[move[1]] == 'k':
self.tp_move[pos] = move
return MATE_UPPER
for move in [killer] + moves:
if root and move in forbidden_moves:
continue
if (move is not None) and (depth > 0):
if best == -MATE_UPPER:
val = -self.alphabeta(pos.move(move), -beta, -alpha, depth - 1, root=False)
else:
val = -self.alphabeta(pos.move(move), -alpha - 1, -alpha, depth - 1, root=False)
if val > alpha and val < beta:
val = -self.alphabeta(pos.move(move), -beta, -alpha, depth - 1, root=False)
if val > best:
best = val
if val > beta:
mvBest = move
break
if val > alpha:
alpha = val
mvBest = move
if val >= MATE_UPPER:
mvBest = move
best = val
print("depth=%s Early return hits!"%depth)
break
if not mvBest and moves:
mvBest = moves[0]
if mvBest is not None:
# Clear before setting, so we always have a value
# Save the move for pv construction and killer heuristic
if len(self.tp_move) > TABLE_SIZE: self.tp_move.clear()
self.tp_move[pos] = mvBest
# Stalemate checking is a bit tricky: Say we failed low, because
# we can't (legally) move and so the (real) score is -infty.
# At the next depth we are allowed to just return r, -infty <= r < gamma,
# which is normally fine.
# However, what if gamma = -10 and we don't have any legal moves?
# Then the score is actaully a draw and we should fail high!
# Thus, if best < gamma and best < 0 we need to double check what we are doing.
# This doesn't prevent sunfish from making a move that results in stalemate,
# but only if depth == 1, so that's probably fair enough.
# (Btw, at depth 1 we can also mate without realizing.)
if best < alpha and best < 0 and depth > 0:
is_dead = lambda pos: any(pos.value(m) >= MATE_LOWER for m in pos.gen_moves())
if all(is_dead(pos.move(m)) for m in pos.gen_moves()):
in_check = is_dead(pos.nullmove())
best = -MATE_UPPER if in_check else 0
# Clear before setting, so we always have a value
if len(self.tp_score) > TABLE_SIZE: self.tp_score.clear()
# Table part 2
if best >= beta:
self.tp_score[pos, depth, root] = Entry(best, entry.upper)
if best < alpha:
self.tp_score[pos, depth, root] = Entry(entry.lower, best)
return best
def search(self, pos, history=()):
""" Iterative deepening MTD-bi search """
self.nodes = 0
self.calc_average()
print("AI: I think my score is %d" % pos.score_rough)
pos.set()
if DRAW_TEST:
self.history = set(history)
# print('# Clearing table due to new history')
self.tp_score.clear()
# In finished games, we could potentially go far enough to cause a recursion
# limit exception. Hence we bound the ply.
for depth in range(5, 8):
# The inner loop is a binary search on the score of the position.
# Inv: lower <= score <= upper
# 'while lower != upper' would work, but play tests show a margin of 20 plays
# better.
lower, upper = -MATE_UPPER, MATE_UPPER
val = self.alphabeta(pos, lower, upper, depth)
print(depth, val)
yield depth, self.tp_move.get(pos), self.tp_score.get((pos, depth, True), Entry(-MATE_UPPER, MATE_UPPER)).lower
def calc_average(self, version=0):
numr, numb = sum(r[key] for key in r), sum(b[key] for key in b)
averagecoveredr, averagecoveredb = 0, 0
averager, averageb = {}, {}
if numr == 0:
averagecoveredr = 0
for i in range(51, 204):
averager[i] = 0
else:
sumr = 0
for key in r.keys():
sumr += pst["1"][key] * r[key] / discount_factor
averagecoveredr = int(sumr//numr)
for i in range(51, 204):
sumr = 0
for key in r.keys():
sumr += pst[key][i] * r[key]
averager[i] = sumr//numr
if numb == 0:
averagecoveredb = 0
for i in range(51, 204):
averageb[i] = 0
else:
sumb = 0
for key in b.keys():
sumb += pst["1"][key.swapcase()] * b[key] / discount_factor
averagecoveredb = int(sumb//numb)
for i in range(51, 204):
sumb = 0
for key in b.keys():
sumb += pst[key.swapcase()][i] * b[key]
averageb[i] = sumb//numb
self.average = {True: {False: averagecoveredr, True: averager}, False: {False: averagecoveredb, True: averageb}}
average[version] = deepcopy(self.average)
return self.average
###############################################################################
# User interface
###############################################################################
def parse(c):
fil, rank = ord(c[0]) - ord('a'), int(c[1])
return A0 + fil - 16*rank
def render(i):
rank, fil = divmod(i - A0, 16)
return chr(fil + ord('a')) + str(-rank)
def render_tuple(t):
return render(t[0]) + render(t[1])
def print_pos(pos):
chessstr = ''
for i, row in enumerate(pos.board.split()):
joinstr = ''.join(uni_pieces.get(p, p) for p in row)
print(' ', 9 - i, joinstr)
chessstr += (' ' + str(9 - i) + joinstr)
print(' abcdefghi\n\n')
chessstr += ' abcdefghi\n\n\n'
return chessstr
def random_policy(pos):
'''
A test function that generates a random policy
'''
all_moves = list(pos.gen_moves())
stupid_AI_move = random.choice(all_moves)
return stupid_AI_move
def translate_eat(eat, dst, turn, type):
assert turn in {'RED', 'BLACK'} and type in {'CLEARMODE', 'DARKMODE'}
if eat == '.':
return None
if turn == 'BLACK':
eat = eat.swapcase()
if type == 'DARKMODE':
return uni_pieces[eat]
else:
if dst is None: # 吃明子
return uni_pieces[eat]
else: # 吃暗子
dst = uni_pieces[dst]
if turn == 'RED':
dst += "(暗)"
else:
dst += "\033[31m(暗)\033[0m"
return dst
def generate_forbiddenmoves(pos, check_bozi=True):
# 生成禁着
# 这里禁着判断比较简单,如果走了这步棋以后形成的局面在过往局面中超过3次,不允许电脑走。
# 这里的pos是电脑视角
global forbidden_moves
forbidden_moves = set()
pos.set()
moves = pos.gen_moves()
for move in moves:
posnew = pos.move(move)
if cache.get(posnew.board, 0) > 0:
forbidden_moves.add(move)
if check_bozi:
i, j = move
p, q = pos.board[i], pos.board[j].upper()
# Actual move
if p == 'H' and ((i == 164 and j == 52 and pos.board[51] in 'dr') or (
i == 170 and j == 58 and pos.board[59] in 'dr')): # TODO: 使用更智能的方式处理博子
if (i == 164 and pos.board[151] == 'N' and pos.board[135] == 'p') or (
j == 170 and pos.board[159] == 'N' and pos.board[143] == 'p'): # 兵顶马
continue
if (i == 164 and pos.board[148] == 'p') or (i == 170 and pos.board[154] == 'p'):
continue
if pos.score_rough < 160 or pos.che == 0:
forbidden_moves.add(move)
pos.set()
return forbidden_moves
def print_cache():
# 内部调试函数,打印cache
print("print_cache starts!")