-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrain.py
156 lines (137 loc) · 5.65 KB
/
Train.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
import tkinter as tk
from tkinter.messagebox import showinfo
from random import choice
from scipy.stats import betabinom
import chess
class Mixin:
def train(self):
if self.fen_reglages is None:
self.fen_reglages = tk.Toplevel(master=self,bg="white")
self.fen_reglages.transient(self)
self.fen_reglages.resizable(width=tk.FALSE,height=tk.FALSE)
self.fen_reglages.title("Settings")
self.fen_reglages.protocol('WM_DELETE_WINDOW',lambda :None)
lbl_coul=tk.Label(self.fen_reglages,text="Train as:",bg="white")
self.val_coul=tk.StringVar(self.fen_reglages)
self.val_coul.set("white")
lstbox_coul=tk.OptionMenu(
self.fen_reglages,self.val_coul,
"white","black")
lbl_ecart=tk.Label(self.fen_reglages,
text="Allowed gap from main move:",bg="white")
self.val_ecart=tk.IntVar(self.fen_reglages)
self.val_ecart.set(0)
lstbox_ecart=tk.OptionMenu(
self.fen_reglages,self.val_ecart,
0,1,2,3)
lbl_choix_partie=tk.Label(self.fen_reglages,
text="Position:",bg="white")
self.val_choix=tk.StringVar(self.fen_reglages)
self.val_choix.set("current")
lstbox_choix_partie=tk.OptionMenu(
self.fen_reglages,self.val_choix,
"current","game start","random game start")
lbl_choix_coup=tk.Label(
self.fen_reglages,text="Move choice:",bg="white")
self.val_coup=tk.StringVar(self.fen_reglages)
self.val_coup.set("order dependant")
lstbox_choix_coup=tk.OptionMenu(
self.fen_reglages,self.val_coup,
"order dependant","uniform")
lbl_ligne_vide=tk.Label(self.fen_reglages,text="",bg="white")
frame_action = tk.Frame(self.fen_reglages)
button_go=tk.Button(frame_action,text="Let's go!",
command=self.train_go)
button_go.pack(side=tk.RIGHT)
button_cancel=tk.Button(frame_action,text="Cancel",
command=self.fen_reglages.withdraw)
button_cancel.pack(side=tk.RIGHT)
lbl_coul.pack()
lstbox_coul.pack()
lbl_ecart.pack()
lstbox_ecart.pack()
lbl_choix_partie.pack()
lstbox_choix_partie.pack()
lbl_choix_coup.pack()
lstbox_choix_coup.pack()
lbl_ligne_vide.pack()
frame_action.pack()
else:
self.fen_reglages.deiconify()
def train_go(self):
self.fen_reglages.withdraw()
self.config(menu=tk.Menu(self))
self.player_color = chess.WHITE if self.val_coul.get() == "white"\
else chess.BLACK
self.flipped = not self.player_color
if self.val_choix.get() == "random game start":
self.pgn = choice(self.pgn_games)
self.pgn_index = self.pgn_games.index(self.pgn)
elif self.val_choix.get() == "game start":
self.pgn = self.pgn.game()
self.editbar.pack_forget()
self.navbar.pack_forget()
self.vert_sep.remove(self.frame_infos)
self.minsize(480,608)
self.trainbar.pack(fill=tk.X)
self.canvas.unbind("<Button-1>")
self.canvas.bind("<Button-1>",self.click_train)
self.training=True
if self.pgn.turn() != self.player_color:
self.choose_move()
else:
self.chessboard = self.pgn.board()
self.maybe_endtraining()
def maybe_endtraining(self):
self.to_primary_transposition()
if not self.pgn.variations:
showinfo("The end","Back to navigation")
self.stop_training()
def choose_move(self):
self.maybe_endtraining()
if self.pgn.variations:
if self.val_coup.get() == "uniform":
self.pgn = choice(self.pgn.variations)
elif self.val_coup.get() == "order dependant":
self.pgn = self.pgn.variations[
betabinom.rvs(len(self.pgn.variations)-1,0.6,1.2)]
else:
assert False
self.maybe_endtraining()
if self.analyzing:
self.change_analyze()
def clue(self):
self.selected_square = self.pgn.variations[0].move.from_square
self.hilight(self.selected_square)
self.refresh()
def stop_training(self):
self.trainbar.pack_forget()
self.config(menu=self.menubar)
self.training=False
self.set_pgn()
if self.editing:
self.edit()
else:
self.edit()
self.read()
def click_train(self, event):
current_column = event.x // self.square_size
current_row = 7 - (event.y // self.square_size)
if self.flipped:
current_row = 7-current_row
current_column = 7-current_column
square = chess.square(current_column,current_row)
if self.selected_square is not None:
move=chess.Move(from_square=self.selected_square,
to_square=square)
self.selected_square = None
self.hilighted = []
if self.pgn.has_variation(move):
child = self.pgn.variation(move)
i = self.pgn.variations.index(child)
if i<=self.val_ecart.get():
self.pgn = child
self.choose_move()
else:
self.hilight(square)
self.refresh()