-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplay_against_ai.py
77 lines (67 loc) · 2.54 KB
/
play_against_ai.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
from XQlightPy.position import Position
from XQlightPy.search import Search
from XQlightPy.cchess import move2Iccs,Iccs2move
import numpy as np
uni_pieces = {4+8:'车', 3+8:'马', 2+8:'相', 1+8:'仕', 0+8:'帅', 6+8:'兵', 5+8:'炮',
4+16:'俥', 3+16:'傌', 2+16:'象', 1+16:'士', 0+16:'将', 6+16:'卒', 5+16:'砲', 0:'.'}
def print_board(pos):
print()
for i, row in enumerate(np.asarray(pos.squares).reshape(16,16)[3:3+10,3:3+9]):
print(' ', 9 - i, ''.join(uni_pieces.get(p, p) for p in row))
print(' abcdefghi\n\n')
search_time_ms = 5000
pos = Position()
pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1")
search = Search(pos, 16)
choice = input("你想要: \n\t1. 执红先行\n\t2. 执黑后行\n\t 请选择:\n")
assert(choice in ["1","2"])
mov = None
if choice == "2":
mov = search.searchMain(64, search_time_ms) # 搜索3秒钟
pos.makeMove(mov)
while True:
print_board(pos)
# 人来下棋
if mov:
print("电脑的上一步:",move2Iccs(mov).replace("-","").lower())
hintmov = search.searchMain(64, 10) # 搜索10毫秒,给出例子
while True:
user_step = input("请输入你的行棋步子,比如 " + move2Iccs(hintmov).replace("-","").lower() + " \n" + \
"悔棋请输入 shameonme :\n").upper()
if user_step == "shameonme".upper():
mov = None
pos.undoMakeMove()
pos.undoMakeMove()
break
if len(user_step) == 4:
user_step = user_step[:2] + "-" + user_step[2:]
try:
user_move = Iccs2move(user_step)
assert(pos.legalMove(user_move))
except:
print("你的行棋不合法,请重新输入")
continue
pos.makeMove(user_move)
break
winner = pos.winner()
if winner is not None:
if winner == 0:
print("红方胜利!行棋结束")
elif winner == 1:
print("黑方胜利!行棋结束")
elif winner == 2:
print("和棋!行棋结束")
break
if user_step != "shameonme".upper():
# 电脑下棋
mov = search.searchMain(64, search_time_ms) # 搜索3秒钟
pos.makeMove(mov)
winner = pos.winner()
if winner is not None:
if winner == 0:
print("红方胜利!行棋结束")
elif winner == 1:
print("黑方胜利!行棋结束")
elif winner == 2:
print("和棋!行棋结束")
break