RubiksCube / app.py
DHEIVER's picture
Update app.py
c1c8771 verified
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
class RubiksCube2x2Solver:
def __init__(self):
self.size = 2
self.cube = np.zeros((6, 2, 2), dtype=int)
# Configurar o estado inicial específico
self.cube[0] = np.array([[1, 3], [4, 2]]) # Face 1
self.cube[1] = np.array([[1, 5], [0, 5]]) # Face 2
self.cube[2] = np.array([[2, 2], [3, 4]]) # Face 3
self.cube[3] = np.array([[1, 5], [3, 3]]) # Face 4
self.cube[4] = np.array([[4, 0], [0, 1]]) # Face 5
self.cube[5] = np.array([[2, 4], [1, 0]]) # Face 6
self.color_names = {
0: "Branco",
1: "Amarelo",
2: "Verde",
3: "Azul",
4: "Vermelho",
5: "Laranja"
}
def get_solution(self):
"""Retorna os passos para resolver o cubo"""
solution = []
# Análise inicial
solution.append("ANÁLISE INICIAL:")
solution.append("1. Face Superior (Face 1):")
solution.append(" - Amarelo, Azul, Vermelho, Verde")
solution.append("2. Face Frontal (Face 2):")
solution.append(" - Amarelo, Laranja, Branco, Laranja")
solution.append("3. Face Direita (Face 3):")
solution.append(" - Verde, Verde, Azul, Vermelho")
solution.append("4. Face Esquerda (Face 4):")
solution.append(" - Amarelo, Laranja, Azul, Azul")
solution.append("5. Face Inferior (Face 5):")
solution.append(" - Vermelho, Branco, Branco, Amarelo")
solution.append("6. Face Traseira (Face 6):")
solution.append(" - Verde, Vermelho, Amarelo, Branco")
solution.append("")
# Estratégia de solução
solution.append("ESTRATÉGIA DE SOLUÇÃO:")
solution.append("1. Resolver Face Branca:")
solution.append(" a. Reunir peças brancas (atualmente nas faces 2, 5 e 6)")
solution.append(" b. Posicionar na face inferior")
solution.append(" c. Orientar corretamente")
solution.append("")
solution.append("2. Resolver Segunda Camada:")
solution.append(" a. Alinhar arestas verdes e vermelhas")
solution.append(" b. Alinhar arestas azuis e laranjas")
solution.append("")
solution.append("3. Resolver Face Amarela:")
solution.append(" a. Posicionar peças amarelas (atualmente espalhadas)")
solution.append(" b. Orientar cantos amarelos")
solution.append(" c. Permutar cantos se necessário")
solution.append("")
solution.append("SEQUÊNCIA DE MOVIMENTOS:")
solution.append("1. R U R' U' (reunir peças brancas)")
solution.append("2. U2 R U2 R' (posicionar brancas)")
solution.append("3. R U R' U R U2 R' (orientar cantos)")
solution.append("4. U' R U R' U' R U R' (segunda camada)")
solution.append("5. U R U' R' (face amarela)")
solution.append("6. R U R' U R U2 R' (orientação final)")
solution.append("")
solution.append("OBSERVAÇÕES:")
solution.append("- Use U para giros da face superior")
solution.append("- Use R para giros da face direita")
solution.append("- O apóstrofo (') indica giro anti-horário")
solution.append("- U2 significa girar duas vezes")
return solution
def create_visualization(cube_state):
fig, ax = plt.subplots(3, 4, figsize=(12, 9))
plt.subplots_adjust(hspace=0.4, wspace=0.4)
colors = {
0: "#FFFFFF", # Branco
1: "#FFFF00", # Amarelo
2: "#00FF00", # Verde
3: "#0000FF", # Azul
4: "#FF0000", # Vermelho
5: "#FFA500" # Laranja
}
face_positions = [
(0, 1), # Face Superior
(1, 1), # Face Frontal
(1, 2), # Face Direita
(1, 0), # Face Esquerda
(2, 1), # Face Inferior
(1, 3), # Face Traseira
]
face_names = ["Superior", "Frontal", "Direita", "Esquerda", "Inferior", "Traseira"]
for face_idx, (row, col) in enumerate(face_positions):
face = cube_state[face_idx]
for i in range(2):
for j in range(2):
color = colors[face[i, j]]
ax[row, col].add_patch(plt.Rectangle(
(j/2, (1-i)/2), 1/2, 1/2,
facecolor=color,
edgecolor='black'
))
ax[row, col].set_xlim(0, 1)
ax[row, col].set_ylim(0, 1)
ax[row, col].set_xticks([])
ax[row, col].set_yticks([])
ax[row, col].set_title(f'Face {face_names[face_idx]}')
for i in range(3):
for j in range(4):
if (i, j) not in face_positions:
fig.delaxes(ax[i, j])
return fig
def show_solution():
cube = RubiksCube2x2Solver()
fig = create_visualization(cube.cube)
solution = "\n".join(cube.get_solution())
return fig, solution
# Criar interface Gradio
with gr.Blocks(title="Resolvedor de Cubo 2x2") as demo:
gr.Markdown("# Resolvedor de Cubo 2x2")
gr.Markdown("### Estado atual do cubo e solução proposta")
with gr.Row():
cube_vis = gr.Plot(label="Estado do Cubo")
solution_text = gr.Textbox(label="Passos para Solução", lines=30)
demo.load(fn=show_solution, outputs=[cube_vis, solution_text])
# Iniciar aplicação
demo.launch()