-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSolverGUI.py
More file actions
309 lines (259 loc) · 13.3 KB
/
Copy pathMagicSolverGUI.py
File metadata and controls
309 lines (259 loc) · 13.3 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
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
import tkinter as tk
from tkinter import messagebox
import os
import datetime
from PIL import Image, ImageDraw, ImageFont # Import necessari per il rendering in memoria
from RubiksCube import RubiksCube
from Solver import RubiksSolver
class RubiksAI:
def __init__(self, root):
# Inizializzazione interfaccia grafica
self.root = root
self.root.title("MagicSolver")
self.root.geometry("850x800")
self.root.configure(bg="#2c3e50")
# Inizializzazione del cubo logico e caricamento del modello (OHE)
self.init_cube_data()
try:
self.ai_solver = RubiksSolver(pipeline='OHE')
except Exception as e:
print(f"Avviso: Modello IA non trovato. Esegui prima il training! {e}")
self.ai_solver = None
self.selected_color = "white"
self.solution_moves = []
self.snapshot_count = 0
self.base_output_dir = "cubo_snapshots"
self.color_map_logic_to_gui = {
'w': 'white', 'y': 'yellow', 'g': 'green',
'b': 'blue', 'r': 'red', 'o': 'orange', '': 'gray'
}
self.color_map_gui_to_logic = {v: k for k, v in self.color_map_logic_to_gui.items() if k != ''}
self.setup_ui()
def init_cube_data(self):
"""Inizializza l'oggetto logico RubiksCube."""
self.cube_logic = RubiksCube()
def setup_ui(self):
"""Configurazioni blocchi, canvas e bottoni dell interfaccia grafica."""
# --- TOP: PALETTE ---
top_frame = tk.LabelFrame(self.root, text=" 1. Seleziona Colore ", bg="#34495e", fg="white", padx=10, pady=5)
top_frame.pack(pady=10)
for c in ["white", "orange", "green", "red", "blue", "yellow"]:
tk.Button(top_frame, bg=c, width=4, height=2, relief=tk.RAISED,
command=lambda col=c: self.set_color(col)).pack(side=tk.LEFT, padx=3)
# --- CENTER: CANVAS ---
self.canvas = tk.Canvas(self.root, width=650, height=480, bg="#ecf0f1", highlightthickness=0)
self.canvas.pack(pady=5)
# --- MIDDLE: MOSSE MANUALI ---
manual_frame = tk.LabelFrame(self.root, text=" 2. Rotazioni Facce ", bg="#34495e", fg="white", padx=10, pady=5)
manual_frame.pack(pady=5)
row1 = tk.Frame(manual_frame, bg="#34495e")
row1.pack()
# Mosse GUI standard
self.gui_moves = ['U', 'D', 'L', 'R', 'F', 'B']
for m in self.gui_moves:
tk.Button(row1, text=m, width=5, command=lambda x=m: self.apply_move_gui(x)).pack(side=tk.LEFT, padx=2)
tk.Button(row1, text=f"{m}'", width=5, bg="#bdc3c7", command=lambda x=f"{m}'": self.apply_move_gui(x)).pack(
side=tk.LEFT, padx=2)
# --- BOTTOM: CONTROLLI IA ---
bot_frame = tk.Frame(self.root, bg="#2c3e50")
bot_frame.pack(pady=15)
btn_style = {"font": ("Arial", 10, "bold"), "padx": 15, "pady": 8}
tk.Button(bot_frame, text="MESCOLA", bg="#f1c40f", **btn_style, command=self.scramble_cube).pack(side=tk.LEFT,
padx=5)
tk.Button(bot_frame, text="VERIFICA", bg="#3498db", fg="white", **btn_style, command=self.solve_with_ai).pack(
side=tk.LEFT, padx=5)
self.play_btn = tk.Button(bot_frame, text="ESEGUI SOLUZIONE", bg="#2ecc71", fg="white", state=tk.DISABLED,
**btn_style, command=self.start_solving_process)
self.play_btn.pack(side=tk.LEFT, padx=5)
tk.Button(bot_frame, text="RESET", bg="#e74c3c", fg="white", **btn_style, command=self.reset_ui).pack(
side=tk.LEFT, padx=5)
self.draw_cube()
def set_color(self, color):
"""Seleziona il colore con cui cambiare il cubo."""
self.selected_color = color
def draw_cube(self, current_move="Stato Attuale"):
"""Creazione rappresentazione grafica del cubo."""
self.canvas.delete("all")
self.canvas.create_text(325, 450, text=f"ULTIMA AZIONE: {current_move}", font=("Arial", 12, "bold"),
fill="#2c3e50")
size = 35
offsets = {
'U': (size * 3 + 20, 40),
'L': (20, size * 3 + 40),
'F': (size * 3 + 20, size * 3 + 40),
'R': (size * 6 + 20, size * 3 + 40),
'B': (size * 9 + 20, size * 3 + 40),
'D': (size * 3 + 19, size * 6 + 40)
}
face_data = {
'U': self.cube_logic.cube[0, 1:4, 1:4],
'D': self.cube_logic.cube[4, 1:4, 1:4],
'F': self.cube_logic.cube[1:4, 0, 1:4],
'B': self.cube_logic.cube[1:4, 4, 1:4],
'L': self.cube_logic.cube[1:4, 1:4, 0],
'R': self.cube_logic.cube[1:4, 1:4, 4]
}
for face, (ox, oy) in offsets.items():
data = face_data[face]
for r in range(3):
for c in range(3):
logic_color = data[r, c]
gui_color = self.color_map_logic_to_gui.get(logic_color, 'gray')
x1, y1 = ox + c * size, oy + r * size
rect = self.canvas.create_rectangle(x1, y1, x1 + (size - 2), y1 + (size - 2),
fill=gui_color, outline="#2c3e50")
self.canvas.tag_bind(rect, "<Button-1>",
lambda e, f=face, row=r, col=c: self.paint_sticker(f, row, col))
def paint_sticker(self, face, r, c):
"""Modifica un singolo sticker nel tensore logico."""
new_val = self.color_map_gui_to_logic[self.selected_color]
if face == 'U':
self.cube_logic.cube[0, 1 + r, 1 + c] = new_val
elif face == 'D':
self.cube_logic.cube[4, 1 + r, 1 + c] = new_val
elif face == 'F':
self.cube_logic.cube[1 + r, 0, 1 + c] = new_val
elif face == 'B':
self.cube_logic.cube[1 + r, 4, 1 + c] = new_val
elif face == 'L':
self.cube_logic.cube[1 + r, 1 + c, 0] = new_val
elif face == 'R':
self.cube_logic.cube[1 + r, 1 + c, 4] = new_val
self.draw_cube()
def apply_move_gui(self, move):
"""Traduce le mosse GUI (U, D...) in mosse RubiksCube (top, bottom...)."""
mapping = {'U': 'top', 'D': 'bottom', 'L': 'left', 'R': 'right', 'F': 'front', 'B': 'back'}
face_logic = mapping[move[0]]
is_rev = "'" in move
self.cube_logic.rotate_face(face_logic, reverse=is_rev)
self.draw_cube(move)
def validate_cube_reality(self):
"""Verifica se la configurazione attuale del cubo è fisicamente possibile."""
all_stickers = []
face_data = {
'U': self.cube_logic.cube[0, 1:4, 1:4], 'D': self.cube_logic.cube[4, 1:4, 1:4],
'F': self.cube_logic.cube[1:4, 0, 1:4], 'B': self.cube_logic.cube[1:4, 4, 1:4],
'L': self.cube_logic.cube[1:4, 1:4, 0], 'R': self.cube_logic.cube[1:4, 1:4, 4]
}
for data in face_data.values():
all_stickers.extend(data.flatten().tolist())
# 1. Controllo Conteggio (9 per colore)
for color_code in ['w', 'y', 'g', 'b', 'r', 'o']:
count = all_stickers.count(color_code)
if count != 9:
color_name = self.color_map_logic_to_gui[color_code]
messagebox.showerror("Cubo non valido!",
f"Il colore {color_name} appare {count} volte. Deve apparire esattamente 9 volte.")
return False
# 2. Controllo Centri (Devono essere fissi)
centers = {
"Top (Bianco)": self.cube_logic.cube[0, 2, 2], "Bottom (Giallo)": self.cube_logic.cube[4, 2, 2],
"Front (Verde)": self.cube_logic.cube[2, 0, 2], "Back (Blu)": self.cube_logic.cube[2, 4, 2],
"Left (Rosso)": self.cube_logic.cube[2, 2, 0], "Right (Arancio)": self.cube_logic.cube[2, 2, 4]
}
expected_centers = {'Top (Bianco)': 'w', 'Bottom (Giallo)': 'y', 'Front (Verde)': 'g',
'Back (Blu)': 'b', 'Left (Rosso)': 'r', 'Right (Arancio)': 'o'}
for name, color in centers.items():
if color != expected_centers[name]:
messagebox.showerror("Cubo non valido!",
f"Il centro della faccia {name} è errato. I centri non possono cambiare posizione!")
return False
# 3. Controllo spigoli per coppie impossibili
opposites = [('w', 'y'), ('g', 'b'), ('r', 'o')]
for c1, c2 in opposites:
pass
return True
def solve_with_ai(self):
"""Utilizza il Solver per trovare la soluzione."""
if not self.ai_solver:
messagebox.showerror("Errore", "Modello IA non caricato. Controlla i file .joblib")
return
if not self.validate_cube_reality():
return
if self.cube_logic.is_solved():
messagebox.showinfo("Info", "Il cubo è già risolto!")
return
# L'IA prova a risolvere il cubo attuale
solution, nodes = self.ai_solver.solve_adaptive_ultra(self.cube_logic)
if solution:
self.solution_moves = solution # Formato: [('top', False), ...]
# Soluzione trovata quindi il tasto Esegui diventa cliccabile
self.play_btn.config(state=tk.NORMAL)
messagebox.showinfo("Successo",
f"IA ha trovato una soluzione in {len(solution)} mosse!\nNodi esplorati: {nodes}")
else:
messagebox.showerror("IA Fallita", "L'IA non è riuscita a risolvere questa configurazione.")
def start_solving_process(self):
"""Crea la cartella dove verranno inseriti gli snapshot della soluzione del cubo."""
# Crea cartella snapshot
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
self.current_session_dir = os.path.join(self.base_output_dir, f"cubo_session_{timestamp}")
if not os.path.exists(self.current_session_dir): os.makedirs(self.current_session_dir)
self.snapshot_count = 0
self.play_solution(0)
def play_solution(self, index):
"""Riproduce gli steps trovati dall IA sull'interfaccia grafica."""
if index < len(self.solution_moves):
move_tuple = self.solution_moves[index] # ('face', reverse)
self.cube_logic.rotate_face(move_tuple[0], reverse=move_tuple[1])
# Label per la mossa
move_label = f"{move_tuple[0].upper()} {'(REV)' if move_tuple[1] else ''}"
self.draw_cube(move_label)
# Creazione snapshot
self.take_snapshot(f"step_{index:02d}", move_label, index + 1)
# Attesa (800ms)
self.root.after(800, lambda: self.play_solution(index + 1))
else:
self.draw_cube("RISOLTO!")
# Il bottone Esegui viene ri-disabilitato
self.play_btn.config(state=tk.DISABLED)
def take_snapshot(self, filename, move_text, step_num):
""" "Fotografa" lo stato attuale del cubo e salva il png nella cartella precedentemente creata."""
# Rendering in memoria per evitare immagini nere se la finestra è minimizzata
img = Image.new('RGB', (650, 520), "#ecf0f1")
draw = ImageDraw.Draw(img)
# Setup Font
try:
font_main = ImageFont.truetype("arial.ttf", 22)
font_small = ImageFont.truetype("arial.ttf", 14)
except:
font_main = font_small = None
draw.text((20, 15), f"STEP #{step_num}: {move_text}", fill="#2c3e50", font=font_main)
draw.line((20, 50, 630, 50), fill="#bdc3c7", width=2)
size = 35
offsets = {
'U': (size * 3 + 20, 70), 'L': (20, size * 3 + 70), 'F': (size * 3 + 20, size * 3 + 70),
'R': (size * 6 + 20, size * 3 + 70), 'B': (size * 9 + 20, size * 3 + 70),
'D': (size * 3 + 19, size * 6 + 70)
}
face_data = {
'U': self.cube_logic.cube[0, 1:4, 1:4], 'D': self.cube_logic.cube[4, 1:4, 1:4],
'F': self.cube_logic.cube[1:4, 0, 1:4], 'B': self.cube_logic.cube[1:4, 4, 1:4],
'L': self.cube_logic.cube[1:4, 1:4, 0], 'R': self.cube_logic.cube[1:4, 1:4, 4]
}
for face, (ox, oy) in offsets.items():
data = face_data[face]
for r in range(3):
for c in range(3):
logic_color = data[r, c]
gui_color = self.color_map_logic_to_gui.get(logic_color, 'gray')
x1, y1 = ox + c * size, oy + r * size
x2, y2 = x1 + (size - 2), y1 + (size - 2)
draw.rectangle([x1, y1, x2, y2], fill=gui_color, outline="#2c3e50")
# Footer
draw.text((480, 490), "MagicSolver", fill="#7f8c8d", font=font_small)
img.save(os.path.join(self.current_session_dir, f"{filename}.png"))
def scramble_cube(self):
"""Mischia il cubo per un numero definito di mosse."""
moves = self.cube_logic.scramble(n=10) # Numero di scramble
self.draw_cube("MESCOLATO")
self.play_btn.config(state=tk.DISABLED)
def reset_ui(self):
"""Riporta il cubo allo stato iniziale."""
self.init_cube_data()
self.play_btn.config(state=tk.DISABLED)
self.draw_cube("RESET")
if __name__ == "__main__":
root = tk.Tk()
app = RubiksAI(root)
root.mainloop()