forked from suyin445/chessin5d_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessin5d_pygame.py
463 lines (421 loc) · 20.9 KB
/
chessin5d_pygame.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
import pygame
import os
import chessin5d
import copy
import sys
sys.path.append(r'D:\Project\AI\SuZero')
import analyzer
IMAGE = {i[:-4]: pygame.image.load(os.path.join('chesspng', i)) for i in os.listdir('.\chesspng')}
IMAGE_RESIZED = {}
for i in IMAGE:
if i.find('boardf') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (205, 205))
elif i.find('over') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (120, 60))
elif i.find('diy') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (90, 40))
elif i.find('change') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (90, 40))
elif i.find('not_move_') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (90, 40))
elif i.find('recall') != -1:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (50, 40))
elif i == 'background':
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (1300, 800))
elif i == 'stalemate':
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (120, 60))
else:
IMAGE_RESIZED[i] = pygame.transform.scale(IMAGE[i], (25, 25))
pygame.init()
class Chessin5dUI:
def __init__(self, image, window_size = None):
if window_size is None:
window_size = (1300, 800)
self.window_size = window_size
self.image = image
self.window = pygame.display.set_mode(self.window_size)
self.chessin5d = chessin5d.State()
self.startxy = (25, 90)
self.boardlist = {}
self.position = [0,1]
self.chosen = None
self.available = {}
self.reverse = False
self.diy = False
self.board_change = False
self.diy_chosen = None
self.notmove = False
self.history = []
self.suzero_use = False
pygame.display.set_caption('老抽冷筱华')
pygame.display.set_icon(self.image['lxh'])
if self.suzero_use:
self.suzero = analyzer.Analyzer()
result = self.suzero.start_analyze(5)
for each in result:
print(each[0], ':', each[1])
def draw_window(self):
self.window.blit(self.image['background'], (0, 0))
self.window.blit(self.image['stalemate'], (900, 20))
if self.chessin5d.end:
font = pygame.font.SysFont('FangSong', 40, bold=True)
if self.chessin5d.winner == 2:
text = font.render('黑胜', True, (243, 243, 0), None)
elif self.chessin5d.winner == 3:
text = font.render('白胜', True, (243, 243, 0), None)
elif self.chessin5d.winner == 1:
text = font.render('平局', True, (243, 243, 0), None)
self.window.blit(text, (700, 25))
if self.history:
self.window.blit(self.image['can_recall'], (1100, 20))
else:
self.window.blit(self.image['cannot_recall'], (1100, 20))
if self.diy:
self.window.blit(self.image['diy_on'], (50, 20))
if self.board_change:
self.window.blit(self.image['board_change_on'], (150, 20))
else:
self.window.blit(self.image['board_change_off'], (150, 20))
else:
self.window.blit(self.image['diy_off'], (50, 20))
if self.chessin5d.end_turn:
self.window.blit(self.image['can_over'], (550, 20))
else:
self.window.blit(self.image['cannot_over'], (550, 20))
if self.notmove:
self.window.blit(self.image['not_move_on'], (1200, 20))
else:
self.window.blit(self.image['not_move_off'], (1200, 20))
for i in self.boardlist:
self.boardlist[i].show()
font = pygame.font.SysFont('FangSong', 20, bold=True)
for x in range(5):
text = font.render(str(self.position[0] + x), True, (243, 243, 0), None)
self.window.blit(text, (120 + 250 * x, 310))
for y in range(3):
text = font.render(str(self.position[1] - y), True, (243, 243, 0), None)
self.window.blit(text, (8, 179 + 250 * y))
pygame.display.update()
def run(self):
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
chesstype = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
self.mouse()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_f:
if self.diy:
chesstype = 0
else:
if self.chessin5d.end_turn:
self.history.append(copy.deepcopy(self.chessin5d))
self.step([0,-1,0,0,0,0,0,0])
elif event.key == pygame.K_w:
if self.reverse:
self.position[1] -= 1
else:
self.position[1] += 1
elif event.key == pygame.K_s:
if self.reverse:
self.position[1] += 1
else:
self.position[1] -= 1
elif event.key == pygame.K_a:
if self.reverse:
self.position[0] += 1
else:
self.position[0] -= 1
elif event.key == pygame.K_d:
if self.reverse:
self.position[0] -= 1
else:
self.position[0] += 1
elif event.key == pygame.K_0:
chesstype = 0
elif event.key == pygame.K_1:
chesstype = 1
elif event.key == pygame.K_2:
chesstype = 2
elif event.key == pygame.K_3:
chesstype = 3
elif event.key == pygame.K_4:
chesstype = 4
elif event.key == pygame.K_5:
chesstype = 5
elif event.key == pygame.K_6:
chesstype = 6
self.boardlist = {}
if self.diy:
self.chosen = self.diy_chosen[:4] if self.diy_chosen else None
if self.diy_chosen and chesstype is not None:
tmp = self.diy_chosen
self.history.append(copy.deepcopy(self.chessin5d))
if chesstype == 0:
self.chessin5d.state[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = 0
else:
self.chessin5d.state[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = tmp[4] * 6 + chesstype
self.diy_chosen = None
if tuple(tmp[:2]) in self.chessin5d.not_moved:
if tmp[2:4] in self.chessin5d.not_moved[tuple(tmp[:2])]:
if len(self.chessin5d.not_moved[tuple(tmp[:2])]) == 1:
self.chessin5d.not_moved.pop(tuple(tmp[:2]))
else:
self.chessin5d.not_moved[tuple(tmp[:2])].remove(tmp[2:4])
self.show_board()
self.draw_window()
pygame.quit()
def board_set(self, coordinate, board, xy, chosen_chess, available, not_move, board_available, now_position):
if board != None:
self.boardlist[coordinate] = Chessboard(
self.window, board, pygame.Rect(xy[0],xy[1],200,200), self.image,
chosen_chess, available, board_available, not_move, now_position)
def show_board(self):
for x in range(5):
for y in range(3):
chosen_chess, available, not_move = None, None, None
now_position = [self.position[1] - y, self.position[0] + x]
board_available = 0
if tuple(now_position) in self.chessin5d.not_moved and self.notmove:
not_move = self.chessin5d.not_moved[tuple(now_position)]
if now_position[0] not in self.chessin5d.available_timeline:
board_available += 2
if divmod(now_position[1], 2)[1] == 1:
board_available += 1
if self.chosen is not None:
if now_position == self.chosen[:2]:
chosen_chess = self.chosen[2:]
if tuple(now_position) in self.available and not self.diy:
available = self.available[tuple(now_position)]
self.board_set((x,y), self.chessin5d.getboard(*now_position),
(self.startxy[0] + 250 * x, self.startxy[1] + 250 * y),
chosen_chess, available, not_move, board_available, now_position)
def mouse(self):
mouse_botton = pygame.mouse.get_pressed()
if self.diy:
self.boardlist = {}
chess_coordinate = self.getchess(pygame.mouse.get_pos())
if chess_coordinate == 'diy':
self.chessin5d.reset_aftermove()
self.diy = False
self.board_change = False
self.diy_chosen = None
self.chosen = None
elif chess_coordinate == 'board_change':
self.board_change = not self.board_change
elif chess_coordinate == 'not_move':
self.notmove = not self.notmove
elif chess_coordinate == 'recall':
if self.history:
self.chessin5d = self.history.pop()
self.boardlist = {}
elif chess_coordinate in ['stalemate', None]:
return False
else:
if self.board_change:
if mouse_botton[0]: # 增加棋盘
if chess_coordinate[0] in self.chessin5d.state:
if chess_coordinate[1] == len(self.chessin5d.state[chess_coordinate[0]]):
self.history.append(copy.deepcopy(self.chessin5d))
self.chessin5d.state[chess_coordinate[0]].append(
self.chessin5d.state[chess_coordinate[0]][chess_coordinate[1] - 1])
else:
self.history.append(copy.deepcopy(self.chessin5d))
if chess_coordinate[0] < 0:
self.chessin5d.whiteline += 1
self.chessin5d.state[-self.chessin5d.whiteline] = [None] * (chess_coordinate[1])
self.chessin5d.state[-self.chessin5d.whiteline].append(
[[0 for i in range(8)] for j in range(8)])
else:
self.chessin5d.blackline += 1
self.chessin5d.state[self.chessin5d.blackline] = [None] * (chess_coordinate[1])
self.chessin5d.state[self.chessin5d.blackline].append(
[[0 for i in range(8)] for j in range(8)])
elif mouse_botton[2]: # 删除棋盘
if chess_coordinate[0] in self.chessin5d.state:
if chess_coordinate[1] == len(self.chessin5d.state[chess_coordinate[0]]) - 1:
def popline(chessin5d,line):
self.history.append(copy.deepcopy(self.chessin5d))
chessin5d.state.pop(line)
if line < 0:
chessin5d.whiteline -= 1
print(chessin5d.whiteline)
else:
chessin5d.blackline -= 1
print(chessin5d.blackline)
if chess_coordinate[1] == 0:
if chess_coordinate[0] != 0:
popline(self.chessin5d, chess_coordinate[0])
elif self.chessin5d.state[chess_coordinate[0]][chess_coordinate[1] - 1] is None:
popline(self.chessin5d, chess_coordinate[0])
else:
self.history.append(copy.deepcopy(self.chessin5d))
self.chessin5d.state[chess_coordinate[0]].pop()
else:
board = self.chessin5d.getboard(*chess_coordinate[:2])
if self.notmove:
if mouse_botton[2]:
if tuple(chess_coordinate[:2]) in self.chessin5d.not_moved:
if chess_coordinate[2:] in self.chessin5d.not_moved[tuple(chess_coordinate[:2])]:
self.history.append(copy.deepcopy(self.chessin5d))
if len(self.chessin5d.not_moved[tuple(chess_coordinate[:2])]) == 1:
self.chessin5d.not_moved.pop(tuple(chess_coordinate[:2]))
else:
self.chessin5d.not_moved[tuple(chess_coordinate[:2])].remove(chess_coordinate[2:])
elif mouse_botton[0]:
checklist = [1,3,6,7,9,12]
if tuple(chess_coordinate[:2]) in self.chessin5d.not_moved:
if chess_coordinate[2:] not in self.chessin5d.not_moved[tuple(chess_coordinate[:2])] and\
board[chess_coordinate[2]][chess_coordinate[3]] in checklist:
self.history.append(copy.deepcopy(self.chessin5d))
self.chessin5d.not_moved[tuple(chess_coordinate[:2])].append(chess_coordinate[2:])
else:
if board is not None and board[chess_coordinate[2]][chess_coordinate[3]] in checklist:
if board[chess_coordinate[2]][chess_coordinate[3]] != 0:
self.history.append(copy.deepcopy(self.chessin5d))
self.chessin5d.not_moved[tuple(chess_coordinate[:2])] = [chess_coordinate[2:]]
else:
if board is None:
self.diy_chosen = None
return False
if mouse_botton[2]: # 白棋
self.diy_chosen = chess_coordinate
self.diy_chosen.append(1)
elif mouse_botton[0]: # 黑棋
self.diy_chosen = chess_coordinate
self.diy_chosen.append(0)
else:
if mouse_botton[2]:
if self.chosen is not None:
self.unchoose()
if mouse_botton[0]:
chess_coordinate = self.getchess(pygame.mouse.get_pos())
if chess_coordinate is None:
if self.chosen is not None:
self.unchoose()
elif chess_coordinate == 'over':
if self.chessin5d.end_turn:
self.history.append(copy.deepcopy(self.chessin5d))
self.step([0, -1, 0, 0, 0, 0, 0, 0])
elif chess_coordinate == 'diy':
self.diy = True
elif chess_coordinate == 'not_move':
self.notmove = not self.notmove
elif chess_coordinate == 'board_change':
return False
elif chess_coordinate == 'recall':
if self.history:
self.chessin5d = self.history.pop()
self.boardlist = {}
elif chess_coordinate == 'stalemate':
self.history.append(copy.deepcopy(self.chessin5d))
self.step([0, -1, -1, 0, 0, 0, 0, 0])
else:
self.handle(chess_coordinate)
def getchess(self, xy):
if xy[0] > 50 and xy[0] < 140 and xy[1] > 20 and xy[1] < 60:
return 'diy'
if xy[0] > 550 and xy[0] < 670 and xy[1] > 20 and xy[1] < 80:
return 'over'
if xy[0] > 150 and xy[0] < 240 and xy[1] > 20 and xy[1] < 60:
return 'board_change'
if xy[0] > 1200 and xy[0] < 1290 and xy[1] > 20 and xy[1] < 60:
return 'not_move'
if xy[0] > 1100 and xy[0] < 1150 and xy[1] > 20 and xy[1] < 60:
return 'recall'
if xy[0] > 900 and xy[0] < 1020 and xy[1] > 20 and xy[1] < 80:
return 'stalemate'
board_x = divmod(xy[0] - self.startxy[0], 250)
board_y = divmod(xy[1] - self.startxy[1], 250)
if board_x[1] >= 200 or board_y[1] >= 200:
return None
board_x, chess_x = tuple(board_x)
board_y, chess_y = tuple(board_y)
chess_x = chess_x // 25
chess_y = chess_y // 25
return [self.position[1] - board_y, self.position[0] + board_x, chess_y, chess_x]
def handle(self,chess_coordinate):
if self.chosen is None:
self.chosen = chess_coordinate
self.choose()
else:
if tuple(chess_coordinate[:2]) in self.available:
if chess_coordinate[2:] in self.available[tuple(chess_coordinate[:2])]:
self.history.append(copy.deepcopy(self.chessin5d))
if not self.step([*self.chosen, *chess_coordinate]):
self.history.pop()
self.unchoose()
def choose(self):
temp = self.chessin5d.get_available(self.chosen)
if temp is None:
self.unchoose()
else:
for i in temp:
board_co = tuple(i[:2])
if board_co not in self.available:
self.available[board_co] = []
self.available[board_co].append(i[2:])
self.boardlist = {}
def unchoose(self):
self.chosen = None
self.available = {}
self.boardlist = {}
def step(self, action):
bool_ = self.chessin5d.onemove(action)
if self.suzero_use:
self.suzero.step(action)
result = self.suzero.start_analyze(5)
for each in result:
print(each[0], ':', each[1])
return bool_
class Chessboard:
def __init__(self, window, board_state, position_rect, image, chosen_chess = None, available = None, board_available = 0, not_move = None, now_position = None):
self.window = window
self.board_state = board_state
self.position_rect = position_rect
self.position = (self.position_rect.x, self.position_rect.y)
self.chosen_chess = chosen_chess
self.board_image = []
self.image = image
self.available = [] if available is None else available
self.board_available = board_available
self.not_move = [] if not_move is None else not_move
self.now_position = now_position
self.chesspng = ['black_king','black_queen','black_rook','black_knight','black_bishop',
'black_pawn','white_king','white_queen','white_rook','white_knight','white_bishop','white_pawn']
for i in range(8):
temp1 = []
for j in range(8):
temp1.append((self.chess_type(self.board_state[i][j]),
pygame.Rect(self.position[0] + 25* j, self.position[1] + 25* i, 25, 25)))
self.board_image.append(temp1)
def chess_type(self, chessid):
if chessid == 0:
return 'blank'
chessid -= 1
return self.chesspng[chessid]
def show(self):
boardframe_list = ['boardframe_black', 'boardframe_white', 'boardframe_black_not_available',
'boardframe_white_not_available']
rect = [i - 3 for i in self.position]
self.window.blit(self.image['boardframe_black'], rect)
self.window.blit(self.image[boardframe_list[self.board_available]], rect)
for i in range(8):
for j in range(8):
image, rect = self.board_image[i][j]
if image != 'blank':
image = self.image[image]
rect = (rect.x, rect.y)
self.window.blit(image, rect)
if [i, j] == self.chosen_chess:
self.window.blit(self.image['frame_chosen'], rect)
elif [i,j] in self.available:
self.window.blit(self.image['frame_available'], rect)
elif [i,j] in self.not_move:
self.window.blit(self.image['frame_not_move'], rect)
a = Chessin5dUI(image= IMAGE_RESIZED)
a.run()