-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (163 loc) · 5.66 KB
/
main.py
File metadata and controls
184 lines (163 loc) · 5.66 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
import chess
import chess.pgn
import svg
import pygame
import io
import math
from lichess import *
from flashcards import *
import os.path
board_size = 500
from_square = None
to_square = None
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode((board_size, board_size))
clock = pygame.time.Clock()
board, best_move = next_problem()
history = []
flipped = not board.turn
incorrect = False
correct_timer = 0
failed = False
review = False
move_sfx = pygame.mixer.Sound(os.path.join('chess_move.wav'))
def render(fill=None, lastmove=None):
if fill is None:
fill = dict()
global pygame_surface
svg_string = svg.board(
board=board,
size=board_size,
coordinates=False,
flipped=flipped,
fill=fill,
lastmove=lastmove
)
pygame_surface = pygame.image.load(io.BytesIO(svg_string.encode()))
def last_move():
try:
return board.peek()
except IndexError:
return None
pygame_surface = None
render(lastmove=last_move())
def try_move():
global from_square, to_square, correct_timer, incorrect, failed, review
try:
move = board.find_move(from_square, to_square)
if move == best_move or (board.is_kingside_castling(move) and board.is_kingside_castling(best_move)) or (board.is_queenside_castling(move) and board.is_queenside_castling(best_move)):
if not failed:
answer(board, 1)
board.push(move)
pygame.mixer.Sound.play(move_sfx)
render(lastmove=last_move())
if failed:
review = True
else:
correct_timer = 10
else:
if not failed:
answer(board, 0)
board.push(move)
pygame.mixer.Sound.play(move_sfx)
incorrect = True
failed = True
render(fill={to_square: "#c9343080", from_square: "#c9343080"})
from_square = None
to_square = None
except ValueError:
from_square = to_square
to_square = None
stats = 1
total = len(playerdata)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
file, rank = map(lambda a: math.floor(a*8/board_size), pygame.mouse.get_pos())
pos = rank*8 + (7 - file) if flipped else (7-rank)*8 + file
if from_square is None:
from_square = pos
elif not incorrect and not correct_timer and not review and not history:
to_square = pos
try_move()
if event.type == pygame.MOUSEBUTTONUP:
file, rank = map(lambda a: math.floor(a * 8 / board_size), pygame.mouse.get_pos())
pos = rank*8 + (7 - file) if flipped else (7 - rank) * 8 + file
if history:
while history:
board.push(history.pop())
pygame.mixer.Sound.play(move_sfx)
render(lastmove=last_move())
elif incorrect and from_square is not None:
board.pop()
render(lastmove=last_move())
incorrect = False
to_square = None
from_square = None
elif review and from_square is not None:
board, best_move = next_problem()
stats += 1
#print("first: " + str(stats))
total += 1
failed = False
review = False
to_square = None
from_square = None
flipped = not board.turn
render(lastmove=last_move())
elif not incorrect and not correct_timer and not review and from_square and pos != from_square:
to_square = pos
try_move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN and not review and not correct_timer and not incorrect and not history:
if not failed:
answer(board, 0)
failed = True
review = True
board.push(best_move)
pygame.mixer.Sound.play(move_sfx)
render(lastmove=last_move())
to_square = None
from_square = None
if event.key == pygame.K_LEFT and len(board.move_stack) and not incorrect and not correct_timer:
history.append(board.pop())
render(lastmove=last_move())
if event.key == pygame.K_RIGHT and history and not incorrect and not correct_timer:
board.push(history.pop())
pygame.mixer.Sound.play(move_sfx)
render(lastmove=last_move())
if event.key == pygame.K_ESCAPE:
run = False
window.fill((255, 255, 255))
window.blit(pygame_surface, pygame_surface.get_rect(center=window.get_rect().center))
if correct_timer:
correct_timer -= 1
if not correct_timer:
board, best_move = next_problem()
stats += 1
total += 1
#print("second: " + str(stats)) first try right
failed = False
to_square = None
from_square = None
flipped = not board.turn
render(lastmove=last_move())
pygame.display.flip()
pygame.display.set_caption("Problem: " + str(stats) + " | Total Seen: " + str(total))
if __name__ == "__main__":
pass
def save():
with open(cache_file, 'w') as fp:
json.dump(cache, fp)
fp.flush()
with open(playerdata_file, 'w') as fp:
json.dump(playerdata, fp)
fp.flush()
pygame.quit()
save()
exit()