-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
158 lines (134 loc) · 5.24 KB
/
play.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
from chess import AI
import PySimpleGUI as sg
import os
blank = os.path.join('assets/blank.png')
bbishop = os.path.join('assets/bbishop.png')
wbishop = os.path.join('assets/wbishop.png')
bpawn = os.path.join('assets/bpawn.png')
wpawn = os.path.join('assets/wpawn.png')
bknight = os.path.join('assets/bknight.png')
wknight = os.path.join('assets/wknight.png')
brook = os.path.join('assets/brook.png')
wrook = os.path.join('assets/wrook.png')
bqueen = os.path.join('assets/bqueen.png')
wqueen = os.path.join('assets/wqueen.png')
bking = os.path.join('assets/bking.png')
wking = os.path.join('assets/wking.png')
images = {'b': bbishop, 'B': wbishop, 'p': bpawn, 'P': wpawn, 'n': bknight, 'N': wknight,
'r': brook, 'R': wrook, 'k': bking, 'K': wking, 'q': bqueen, 'Q': wqueen, '.': blank}
def render_square(image, location):
if (location[0] + location[1]) % 2:
color = '#B58863'
else:
color = '#F0D9B5'
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', color), pad=(0, 0), key=location)
def redraw_board(window, board):
for r in range(8):
for c in range(8):
color = '#B58863' if (r + c) % 2 else '#F0D9B5'
piece_image = images[board[r][c]]
elem = window[(r, c)]
elem.Update(button_color=('white', color), image_filename=piece_image)
def play_as_white(debug=False, move_time=5):
ai = AI(move_time=move_time)
board = ai.state.board
board_layout = []
for r in range(8):
row = []
for c in range(8):
piece_image = images[board[r][c]]
row.append(render_square(piece_image, location=(r, c)))
board_layout.append(row)
if debug:
layout = [[sg.Column(board_layout), sg.Column([[sg.RButton('Why did you\n do that?', key='print')]])]]
else:
layout = board_layout
window = sg.Window('Chess').Layout(layout)
move = (None, None)
while True:
button, value = window.Read()
if button == 'print':
ai.print_decision_process()
elif button == sg.WINDOW_CLOSED:
break
else:
if not move[0]:
move = (button, None)
elif not move[1]:
move = (move[0], button)
message = ai.register_move(move)
if message == 'Success':
board = ai.state.board
redraw_board(window, board)
window.refresh()
message, move = ai.make_move()
if message == 'Success':
board = ai.state.board
redraw_board(window, board)
window.refresh()
elif message == 'Check mate!':
board = ai.state.board
redraw_board(window, board)
window.refresh()
sg.Popup(message, keep_on_top=True, font=('Arial', 15, 'bold'))
break
else:
sg.Popup(message, keep_on_top=True, font=('Arial', 15, 'bold'))
break
else:
sg.Popup(message, keep_on_top=True, font=('Arial', 15, 'bold'))
move = (None, None)
def play(debug=False, move_time=5):
ai_white = AI(move_time=move_time)
board = ai_white.state.board
board_layout = []
for r in range(8):
row = []
for c in range(8):
piece_image = images[board[r][c]]
row.append(render_square(piece_image, location=(r, c)))
board_layout.append(row)
if debug:
layout = [[sg.Column(board_layout), sg.Column([[sg.RButton('Print key', key='print')]])]]
else:
layout = board_layout
window = sg.Window('Chess').Layout(layout)
window.read()
ai_white.make_move()
board = ai_white.state.board
redraw_board(window, board)
window.refresh()
ai_black = AI(key=ai_white.state.get_key(), move_time=move_time)
while True:
button, value = window.Read(timeout=10)
if button == 'print':
print(ai_black.state.get_key())
elif button == sg.WINDOW_CLOSED:
break
message, move = ai_black.make_move()
if message == 'Success':
board = ai_black.state.board
message = ai_white.register_move(move)
if message == 'Success':
redraw_board(window, board)
window.Refresh()
else:
print('Black tried to cheat', ai_white.state.get_key(), ai_black.state.get_key())
break
else:
sg.Popup(message, keep_on_top=True, font=('Arial', 15, 'bold'))
break
message, move = ai_white.make_move()
if message == 'Success':
board = ai_white.state.board
message = ai_black.register_move(move)
if message == 'Success':
redraw_board(window, board)
window.Refresh()
else:
print('White tried to cheat', ai_black.state.get_key(), ai_white.state.get_key())
break
else:
sg.Popup(message, keep_on_top=True, font=('Arial', 15, 'bold'))
break
play_as_white(move_time=5)