-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
255 lines (225 loc) · 9.72 KB
/
GUI.py
File metadata and controls
255 lines (225 loc) · 9.72 KB
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
import sys, pygame, numpy, os
import chess
import Squares
import chessbrain
import time
def main():
pygame.init()
screen = pygame.display.set_mode((848, 848))
pygame.display.set_caption("Chess")
#Size of squares
size = 106
white = (255, 255, 255)
black = (161, 96, 43)
board = chess.Board()
board_squares = chess.SquareSet()
squares = []
# method for drawing chess board obtained from stack overflow, link lost somewhere in history
count = 0
# this fen has no slashes, you must remove slashes for any fen to work, you also must remove the ending material that says "3 - 0" or something
startingFen = 'rnbkqbnrpppppppp8888PPPPPPPPRNBKQBNR'
fenCount = len(startingFen)- 1
for i in range(8):
for j in range(8):
if count % 2 == 0:
# rectangle = pygame.rect.Rect(size*j,size*i,size,size)
rectangle = pygame.draw.rect(screen, white, (size*j,size*i,size,size))
if startingFen[fenCount].isdigit() and int(startingFen[fenCount]) > 0:
squares.append(Squares.Squares(chr((j + 97)) + str(int(8 - i)), "", rectangle, False))
f = str(int(startingFen[fenCount]) - 1)
startingFen = startingFen[:fenCount] + f + startingFen[fenCount + 1:]
if int(startingFen[fenCount]) == 0:
fenCount -= 1
else:
squares.append(Squares.Squares(chr((j + 97)) + str(int(8 - i)), startingFen[fenCount], rectangle, False))
pieceImg = squares[len(squares) - 1].image
screen.blit(pieceImg, (size*j, size*i))
fenCount -= 1
else:
rectangle = pygame.draw.rect(screen, white, (size*j,size*i,size,size))
# rectangle = pygame.rect.Rect(size*j,size*i,size,size)
if startingFen[fenCount].isdigit() and int(startingFen[fenCount]) > 0:
squares.append(Squares.Squares(chr((j + 97)) + str(int(8 - i)), "", rectangle, False))
f = str(int(startingFen[fenCount]) - 1)
startingFen = startingFen[:fenCount] + f + startingFen[fenCount + 1:]
if int(startingFen[fenCount]) == 0:
fenCount -= 1
else:
squares.append(Squares.Squares(chr((j + 97)) + str(int(8 - i)), startingFen[fenCount], rectangle, False))
pieceImg = squares[len(squares) - 1].image
screen.blit(pieceImg, (size*j, size*i))
fenCount -= 1
count +=1
count-=1
movedThing = None
x, y = 0, 0
while True:
screen = drawBoard(screen, squares)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
for i in squares:
if i.rectangle.collidepoint(event.pos) and i.image != None:
i.dragging = True
mouse_x, mouse_y = event.pos
offset_x = i.posx - mouse_x
offset_y = i.posy - mouse_y
x, y = event.pos[0], event.pos[1]
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
for i in range(len(squares)):
if squares[i].dragging:
squares[i].dragging = False
print(event.pos)
for x in range(len(squares)):
if squares[x].rectangle.collidepoint(event.pos) and squares[x] != squares[i]:
# We use a try except because it will raise ValueError on illegal move or on Game Over
try:
print(squares[i].pos + squares[x].pos)
playHumanMove(board, squares, i, x)
playAIMove(board, squares)
except (ValueError, TypeError, NameError) as e:
if board.is_game_over() or board.is_stalemate():
print("Game Over!")
else:
print("Invalid Move. \n{0}".format(e))
elif event.type == pygame.MOUSEMOTION:
for i in squares:
if i.image == None:
continue
if i.dragging:
mouse_x, mouse_y = event.pos
x = mouse_x + offset_x
y = mouse_y + offset_y
screen.blit(i.image, (x, y))
# place the piece that is currently being moved.
for i in squares:
if i.dragging:
screen.blit(i.image, (x, y))
# loop through squares and place each piece accordingly each update.
pygame.display.update()
def checkCastle(board, squares, move):
try:
# King side white castle
if move == 'e1g1':
found = False
print('castled')
for rook in squares:
if rook.pos == 'h1':
for f1 in squares:
if f1.pos == 'f1' and rook.piece == 'r':
f1.image = rook.image
rook.image = None
found = True
if found:
break
# Queen side white castle
if move == 'e1c1':
found = False
print('castled')
for rook in squares:
if rook.pos == 'a1' and rook.piece == 'r':
for d1 in squares:
if d1.pos == 'd1':
d1.image = rook.image
rook.image = None
found = True
if found:
break
# Queen side black castle
if move == 'e8c8':
found = False
print('castled')
for rook in squares:
if rook.pos == 'a8' and rook.piece == 'R':
for d1 in squares:
if d1.pos == 'd8':
d1.image = rook.image
rook.image = None
found = True
if found:
break
# King side black castle
if move == 'e8g8':
found = False
print('castled')
for rook in squares:
if rook.pos == 'h8' and rook.piece == 'R':
for f8 in squares:
if f8.pos == 'f8':
f8.image = rook.image
rook.image = None
found = True
if found:
break
except:
pass
return squares
def drawBoard(screen, squares):
white = (255, 255, 255)
black = (161, 96, 43)
index = 0
count = 0
for i in range(8):
for j in range(8):
if count % 2 == 0:
pygame.draw.rect(screen, white, squares[index].rectangle)
if squares[index].image != None and squares[index].dragging == False:
screen.blit(squares[index].image, (squares[index].posx, squares[index].posy))
else:
pygame.draw.rect(screen, black, squares[index].rectangle)
if squares[index].image != None and squares[index].dragging == False:
screen.blit(squares[index].image, (squares[index].posx, squares[index].posy))
index += 1
count += 1
count -= 1
return screen
def playAIMove(board, squares):
start_time = time.time()
move = chessbrain.getMove(board, 3, 3, True, True, 'BLACK', -numpy.Infinity, numpy.Infinity)
print(move, chessbrain.searched, str(time.time() - start_time))
chessbrain.searched = 0
# chessbrain.searched = 0
# Check if castle
squares = checkCastle(board, squares, str(move))
board.push(chess.Move.from_uci(str(move)))
if str(move)[len(move) - 1] == 'q':
found = False
for pos in squares:
for newPos in squares:
f = str(pos.pos + newPos.pos + 'q')
if f == str(move):
newPos.redoImg('Q')
pos.image = None
found = True
break
if found:
break
else:
found = False
for pos in squares:
for newPos in squares:
f = str(pos.pos + newPos.pos)
if f == str(move):
newPos.image = pos.image
pos.image = None
found = True
break
if found:
break
def playHumanMove(board, squares, i, x):
squares = checkCastle(board, squares, squares[i].pos + squares[x].pos)
# Check to make sure is legal move using generate_legal_moves not legal_moves.
if chess.Move.from_uci(squares[i].pos + squares[x].pos + 'q') in list(board.generate_legal_moves()):
print("Promotion!")
# Have to move the piece using from_uci otherwise library does not recongnize as legal
board.push(chess.Move.from_uci(squares[i].pos + squares[x].pos + 'q'))
# Reset the image variable to new correct image
squares[x].redoImg('q')
squares[i].image = None
else:
board.push_san(squares[i].pos + squares[x].pos)
squares[x].image = squares[i].image
squares[i].image = None
main()