-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIplay.py
285 lines (251 loc) · 13.7 KB
/
UIplay.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
import pygame
import sys
import copy
import random
from game import move_action2move_id, Game, Board
from mcts import MCTSPlayer
import time
from config import CONFIG
# if CONFIG['use_frame'] == 'paddle':
# from paddle_net import PolicyValueNet
if CONFIG['use_frame'] == 'pytorch':
from pytorch_net import PolicyValueNet
else:
print('暂不支持您选择的框架')
class Human:
def __init__(self):
self.agent = 'HUMAN'
def get_action(self, move):
# move从鼠标点击事件触发
# print('当前是player2在操作')
# print(board.current_player_color)
move = move_action2move_id[move]
# move = random.choice(board.availables)
return move
def set_player_ind(self, p):
self.player = p
# if CONFIG['use_frame'] == 'paddle':
# policy_value_net = PolicyValueNet(model_file='current_policy.model')
if CONFIG['use_frame'] == 'pytorch':
policy_value_net = PolicyValueNet(model_file='current_policy.pkl')
else:
print('暂不支持您选择的框架')
# 初始化pygame
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load('bgm/yinzi.ogg')
pygame.mixer.music.set_volume(0.03)
pygame.mixer.music.play(loops=-1, start=0.0)
size = width, height = 700, 700
bg_image = pygame.image.load('imgs/board.png') #图片位置
bg_image = pygame.transform.smoothscale(bg_image, size)
clock = pygame.time.Clock()
fullscreen = False
# 创建指定大小的窗口
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("中国象棋")
# 加载一个列表进行图像的绘制
# 列表表示的棋盘初始化,红子在上,黑子在下,禁止对该列表进行编辑,使用时必须使用深拷贝
board_list_init = [['红车', '红马', '红象', '红士', '红帅', '红士', '红象', '红马', '红车'],
['一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一'],
['一一', '红炮', '一一', '一一', '一一', '一一', '一一', '红炮', '一一'],
['红兵', '一一', '红兵', '一一', '红兵', '一一', '红兵', '一一', '红兵'],
['一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一'],
['一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一'],
['黑兵', '一一', '黑兵', '一一', '黑兵', '一一', '黑兵', '一一', '黑兵'],
['一一', '黑炮', '一一', '一一', '一一', '一一', '一一', '黑炮', '一一'],
['一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一', '一一'],
['黑车', '黑马', '黑象', '黑士', '黑帅', '黑士', '黑象', '黑马', '黑车']]
# 加载棋子被选中的图片
fire_image = pygame.transform.smoothscale(pygame.image.load("imgs/fire.png").convert_alpha(), (width // 10, height // 10))
fire_image.set_alpha(200)
# 制作一个从字符串到pygame.surface对象的映射
str2image = {
'红车': pygame.transform.smoothscale(pygame.image.load("imgs/hongche.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红马': pygame.transform.smoothscale(pygame.image.load("imgs/hongma.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红象': pygame.transform.smoothscale(pygame.image.load("imgs/hongxiang.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红士': pygame.transform.smoothscale(pygame.image.load("imgs/hongshi.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红帅': pygame.transform.smoothscale(pygame.image.load("imgs/hongshuai.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红炮': pygame.transform.smoothscale(pygame.image.load("imgs/hongpao.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'红兵': pygame.transform.smoothscale(pygame.image.load("imgs/hongbing.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑车': pygame.transform.smoothscale(pygame.image.load("imgs/heiche.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑马': pygame.transform.smoothscale(pygame.image.load("imgs/heima.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑象': pygame.transform.smoothscale(pygame.image.load("imgs/heixiang.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑士': pygame.transform.smoothscale(pygame.image.load("imgs/heishi.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑帅': pygame.transform.smoothscale(pygame.image.load("imgs/heishuai.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑炮': pygame.transform.smoothscale(pygame.image.load("imgs/heipao.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
'黑兵': pygame.transform.smoothscale(pygame.image.load("imgs/heibing.png").convert_alpha(), (width // 10 - 10, height // 10 - 10)),
}
str2image_rect = {
'红车': str2image['红车'].get_rect(),
'红马': str2image['红马'].get_rect(),
'红象': str2image['红象'].get_rect(),
'红士': str2image['红士'].get_rect(),
'红帅': str2image['红帅'].get_rect(),
'红炮': str2image['红炮'].get_rect(),
'红兵': str2image['红兵'].get_rect(),
'黑车': str2image['黑车'].get_rect(),
'黑马': str2image['黑马'].get_rect(),
'黑象': str2image['黑象'].get_rect(),
'黑士': str2image['黑士'].get_rect(),
'黑帅': str2image['黑帅'].get_rect(),
'黑炮': str2image['黑炮'].get_rect(),
'黑兵': str2image['黑兵'].get_rect(),
}
# 根据棋盘列表获得最新位置
# 返回一个由image和rect对象组成的列表
x_ratio = 80
y_ratio = 72
x_bais = 30
y_bais = 25
def board2image(board):
return_image_rect = []
for i in range(10):
for j in range(9):
if board[i][j] == '红车':
str2image_rect['红车'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红车'])
return_image_rect.append((str2image['红车'], str2image_rect_copy))
elif board[i][j] == '红马':
str2image_rect['红马'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红马'])
return_image_rect.append((str2image['红马'], str2image_rect_copy))
elif board[i][j] == '红兵':
str2image_rect['红兵'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红兵'])
return_image_rect.append((str2image['红兵'], str2image_rect_copy))
elif board[i][j] == '红象':
str2image_rect['红象'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红象'])
return_image_rect.append((str2image['红象'], str2image_rect_copy))
elif board[i][j] == '红炮':
str2image_rect['红炮'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红炮'])
return_image_rect.append((str2image['红炮'], str2image_rect_copy))
elif board[i][j] == '红士':
str2image_rect['红士'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红士'])
return_image_rect.append((str2image['红士'], str2image_rect_copy))
elif board[i][j] == '红帅':
str2image_rect['红帅'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红帅'])
return_image_rect.append((str2image['红帅'], str2image_rect_copy))
elif board[i][j] == '红兵':
str2image_rect['红兵'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['红兵'])
return_image_rect.append((str2image['红兵'], str2image_rect_copy))
elif board[i][j] == '黑车':
str2image_rect['黑车'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑车'])
return_image_rect.append((str2image['黑车'], str2image_rect_copy))
elif board[i][j] == '黑马':
str2image_rect['黑马'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑马'])
return_image_rect.append((str2image['黑马'], str2image_rect_copy))
elif board[i][j] == '黑兵':
str2image_rect['黑兵'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑兵'])
return_image_rect.append((str2image['黑兵'], str2image_rect_copy))
elif board[i][j] == '黑象':
str2image_rect['黑象'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑象'])
return_image_rect.append((str2image['黑象'], str2image_rect_copy))
elif board[i][j] == '黑炮':
str2image_rect['黑炮'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑炮'])
return_image_rect.append((str2image['黑炮'], str2image_rect_copy))
elif board[i][j] == '黑士':
str2image_rect['黑士'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑士'])
return_image_rect.append((str2image['黑士'], str2image_rect_copy))
elif board[i][j] == '黑帅':
str2image_rect['黑帅'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑帅'])
return_image_rect.append((str2image['黑帅'], str2image_rect_copy))
elif board[i][j] == '黑兵':
str2image_rect['黑兵'].center = (j * x_ratio + x_bais, i * y_ratio + y_bais)
str2image_rect_copy = copy.deepcopy(str2image_rect['黑兵'])
return_image_rect.append((str2image['黑兵'], str2image_rect_copy))
return return_image_rect
fire_rect = fire_image.get_rect()
fire_rect.center = (0 * x_ratio + x_bais, 3 * y_ratio + y_bais)
# 加载两个玩家,AI对AI,或者AI对human
board=Board()
# 开始的玩家
start_player = 1
player1 = MCTSPlayer(policy_value_net.policy_value_fn,
c_puct=5,
n_playout=2000,
is_selfplay=0)
player2 = Human()
board.init_board(start_player)
p1, p2 = 1, 2
player1.set_player_ind(1)
player2.set_player_ind(2)
players = {p1: player1, p2: player2}
# 切换玩家
swicth_player = True
draw_fire = False
move_action = ''
first_button = False
while True:
# 填充背景
screen.blit(bg_image, (0, 0))
for image, image_rect in board2image(board=board.state_deque[-1]):
screen.blit(image, image_rect)
if draw_fire:
screen.blit(fire_image, fire_rect)
# 更新界面
pygame.display.update()
# 不高于60帧
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN: #按下按键
# print("[MOUSEBUTTONDOWN]", event.pos, event.button)
mouse_x, mouse_y = event.pos
if not first_button:
for i in range(10):
for j in range(9):
if abs(80 * j + 30 - mouse_x) < 30 and abs(72 * i + 25 - mouse_y) < 30:
first_button = True
start_i_j = j, i
fire_rect.center = (start_i_j[0] * x_ratio + x_bais, start_i_j[1] * y_ratio + y_bais)
# screen.blit(fire_image, fire_rect)
break
elif first_button:
for i in range(10):
for j in range(9):
if abs(80 * j + 30 - mouse_x) < 30 and abs(72 * i + 25 - mouse_y) < 30:
first_button = False
end_i_j = j, i
move_action = str(start_i_j[1]) + str(start_i_j[0]) + str(end_i_j[1]) + str(end_i_j[0])
# screen.blit(fire_image, fire_rect)
if swicth_player:
current_player = board.get_current_player_id() # 红子对应的玩家id
player_in_turn = players[current_player] # 决定当前玩家的代理
if player_in_turn.agent == 'AI':
start_time = time.time()
move = player_in_turn.get_action(board) # 当前玩家代理拿到动作
print('耗时:', time.time() - start_time)
board.do_move(move) # 棋盘做出改变
swicth_player = True
draw_fire = False
elif player_in_turn.agent == 'HUMAN':
draw_fire = True
swicth_player = False
if len(move_action) == 4:
move = player_in_turn.get_action(move_action) # 当前玩家代理拿到动作
board.do_move(move) # 棋盘做出改变
swicth_player = True
move_action = ''
draw_fire = False
end, winner = board.game_end()
if end:
if winner != -1:
print("Game end. Winner is", players[winner])
else:
print("Game end. Tie")
sys.exit()