AI 此刻也非常无奈 完全无助甚至想打人了吧.一个魔方模拟物理旋转在用kociemba解都搞不定这个数据逻辑
利用AI工具 制作一个模拟魔方按照物理特性转动改变六个面的颜色,在实际求解, 用kociemba解法还原 ChatGPT,DeepSeek Cursor,Claude 3.7 Google Gemini 没有一个AI能完美解决得了这个程序代码 .每个AI都自我反复提出问题.修修补补始终都解决不了.问题都能看清楚.修修补补却拿不出一点实际的能力出来.
import tkinter as tk
from tkinter import messagebox
import kociemba
import random
import time
from copy import deepcopy
from collections import Counter
class RubiksCubeGUI:
def __init__(self, root):
self.root = root
self.root.title("魔方模拟器 - 终极修正版")
# 标准颜色配置(必须与Kociemba算法一致)
self.colors = {
'U': 'white', # 上
'D': 'yellow', # 下
'F': 'green', # 前
'B': 'blue', # 后
'L': 'orange', # 左
'R': 'red' # 右
}
# 初始化状态
self.initial_state = {face: [[face for _ in range(3)] for _ in range(3)] for face in 'UDFBLR'}
self.state = deepcopy(self.initial_state)
self.setup_ui()
self.update_display()
self.log("魔方模拟器已初始化")
def setup_ui(self):
"""初始化用户界面"""
main_frame = tk.Frame(self.root)
main_frame.pack(padx=10, pady=10)
# 左侧面板 - 魔方显示
left_frame = tk.Frame(main_frame)
left_frame.pack(side=tk.LEFT, padx=10)
self.canvas = tk.Canvas(left_frame, width=400, height=400, bg='white')
self.canvas.pack(pady=(0, 10))
# 功能按钮
btn_frame = tk.Frame(left_frame)
btn_frame.pack()
tk.Button(btn_frame, text="随机打乱", command=self.scramble_cube, width=10).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="求解魔方", command=self.solve_cube, width=10).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="重置魔方", command=self.reset_cube, width=10).pack(side=tk.LEFT, padx=5)
# 旋转按钮面板
rot_frame = tk.Frame(main_frame)
rot_frame.pack(side=tk.LEFT, padx=10)
# 添加旋转按钮
for i, face in enumerate('UDFBLR'):
tk.Button(rot_frame, text=f"{face}", command=lambda f=face: self.rotate_face(f), width=5).grid(row=i, column=0, pady=2)
tk.Button(rot_frame, text=f"{face}'", command=lambda f=face: self.rotate_face(f+"'"), width=5).grid(row=i, column=1, pady=2)
tk.Button(rot_frame, text=f"{face}2", command=lambda f=face: self.rotate_face(f+"2"), width=5).grid(row=i, column=2, pady=2)
# 日志面板
log_frame = tk.Frame(main_frame)
log_frame.pack(side=tk.LEFT, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(log_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.log_text = tk.Text(log_frame, height=20, width=60, yscrollcommand=scrollbar.set)
self.log_text.pack(fill=tk.BOTH, expand=True)
scrollbar.config(command=self.log_text.yview)
tk.Button(log_frame, text="清空日志", command=self.clear_log).pack(side=tk.BOTTOM, pady=5)
def rotate_face(self, move):
"""旋转魔方的一个面"""
face = move[0]
is_prime = "'" in move
is_double = "2" in move
# 保存当前状态用于调试
old_state = deepcopy(self.state)
# 旋转面本身
if is_double:
self.state[face] = [row[::-1] for row in self.state[face][::-1]]
elif is_prime:
self.state[face] = [list(row) for row in zip(*self.state[face])][::-1]
else:
self.state[face] = [list(row) for row in zip(*self.state[face][::-1])]
# 更新相邻面的边缘块
self.update_adjacent_edges(face, is_prime, is_double)
# 验证旋转后的状态
valid, msg = self.validate_state()
if not valid:
self.log(f"警告:旋转后状态无效 - {msg}")
# 恢复原始状态
self.state = old_state
self.update_display()
self.log(f"执行旋转: {move}")
self.log_current_state()
def update_adjacent_edges(self, face, is_prime, is_double):
"""完全修正的边缘块更新方法"""
# 定义每个面的相邻边缘
edge_definitions = {
'U': {
'edges': [('F',0), ('R',0), ('B',0), ('L',0)],
'reverse': [False, False, False, False]
},
'D': {
'edges': [('F',2), ('L',2), ('B',2), ('R',2)],
'reverse': [False, False, False, False]
},
'F': {
'edges': [('U',2), ('R','L'), ('D',0), ('L','R')],
'reverse': [False, True, False, True]
},
'B': {
'edges': [('U',0), ('L','R'), ('D',2), ('R','L')],
'reverse': [False, True, False, True]
},
'L': {
'edges': [('U','L'), ('F','L'), ('D','L'), ('B','R')],
'reverse': [False, False, False, True]
},
'R': {
'edges': [('U','R'), ('B','L'), ('D','R'), ('F','R')],
'reverse': [False, True, False, True]
}
}
config = edge_definitions.get(face, {})
if not config:
return
edges = config['edges']
reverse_flags = config['reverse']
# 获取当前边缘块
temp = []
for i, (adj_face, edge) in enumerate(edges):
if edge == 'L':
colors = [self.state[adj_face][j][0] for j in range(3)]
elif edge == 'R':
colors = [self.state[adj_face][j][2] for j in range(3)]
else:
colors = self.state[adj_face][edge][:]
if reverse_flags[i]:
colors = colors[::-1]
temp.append(colors)
# 调整边缘顺序
if is_double:
temp = temp[2:] + temp[:2]
elif is_prime:
temp = temp[1:] + temp[:1]
else:
temp = temp[-1:] + temp[:-1]
# 更新边缘块
for i in range(4):
adj_face, edge = edges[i]
new_colors = temp[i]
if reverse_flags[i]:
new_colors = new_colors[::-1]
if edge == 'L':
for j in range(3):
self.state[adj_face][j][0] = new_colors[j]
elif edge == 'R':
for j in range(3):
self.state[adj_face][j][2] = new_colors[j]
else:
self.state[adj_face][edge] = new_colors
def get_cube_string(self):
"""完全符合Kociemba规范的状态字符串生成"""
# 定义各面读取顺序(基于官方实现)
face_reading = {
'U': [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)], # 行优先
'R': [(0,2),(1,2),(2,2),(0,1),(1,1),(2,1),(0,0),(1,0),(2,0)], # 列倒序
'F': [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)], # 行优先
'D': [(2,0),(2,1),(2,2),(1,0),(1,1),(1,2),(0,0),(0,1),(0,2)], # 行倒序(关键修正!)
'L': [(0,0),(1,0),(2,0),(0,1),(1,1),(2,1),(0,2),(1,2),(2,2)], # 列正序
'B': [(2,2),(1,2),(0,2),(2,1),(1,1),(0,1),(2,0),(1,0),(0,0)] # 行和列都倒序
}
cube_str = []
for face in 'URFDLB': # 必须严格此顺序
for (i,j) in face_reading[face]:
cube_str.append(self.state[face][i][j])
cube_str = ''.join(cube_str)
# 验证中心块(关键检查点)
centers = {
4: 'U', 13: 'R', 22: 'F',
31: 'D', 40: 'L', 49: 'B'
}
for pos, color in centers.items():
if cube_str[pos] != color:
raise ValueError(f"中心块错误:位置{pos}应为{color},实际{cube_str[pos]}")
return cube_str
def validate_state(self):
"""验证魔方状态是否合法"""
# 检查各颜色数量
counts = Counter(self.get_cube_string())
if counts != {'U':9,'D':9,'F':9,'B':9,'L':9,'R':9}:
return False, "颜色数量不正确"
return True, "状态有效"
def solve_cube(self):
"""求解魔方"""
try:
# 验证状态
valid, msg = self.validate_state()
if not valid:
messagebox.showerror("错误", msg)
return
# 获取状态字符串
cube_str = self.get_cube_string()
self.log(f"求解状态字符串: {cube_str}")
# 调用Kociemba算法
solution = kociemba.solve(cube_str)
self.log(f"求解步骤: {solution}")
# 执行解法
for move in solution.split():
self.log(f"执行: {move}")
self.rotate_face(move)
self.root.update()
time.sleep(0.5)
if self.is_solved():
messagebox.showinfo("成功", "魔方已还原!")
else:
messagebox.showerror("失败", "还原未完成")
except Exception as e:
messagebox.showerror("错误", f"求解失败: {str(e)}")
def scramble_cube(self):
"""随机打乱魔方"""
moves = ['U', 'D', 'F', 'B', 'L', 'R']
scramble = [random.choice(moves) + random.choice(["", "'", "2"]) for _ in range(15)]
self.log(f"随机打乱步骤: {' '.join(scramble)}")
for move in scramble:
self.rotate_face(move)
self.root.update()
time.sleep(0.2)
def reset_cube(self):
"""重置魔方"""
self.state = deepcopy(self.initial_state)
self.update_display()
self.log("魔方已重置")
def is_solved(self):
"""检查是否已还原"""
for face in self.initial_state:
for i in range(3):
for j in range(3):
if self.state[face][i][j] != face:
return False
return True
def update_display(self):
"""更新魔方显示"""
self.canvas.delete("all")
# 各面在画布上的位置
positions = {
'U': (100, 50),
'D': (100, 250),
'F': (100, 150),
'B': (300, 150),
'L': (0, 150),
'R': (200, 150)
}
for face, (x, y) in positions.items():
for i in range(3):
for j in range(3):
self.canvas.create_rectangle(
x + j * 30, y + i * 30,
x + (j + 1) * 30, y + (i + 1) * 30,
fill=self.colors[self.state[face][i][j]],
outline='black'
)
self.canvas.create_text(x + 45, y - 15, text=face, font=('Arial', 12, 'bold'))
def log(self, message):
"""记录日志"""
self.log_text.insert(tk.END, message + "\n")
self.log_text.see(tk.END)
def log_current_state(self):
"""记录当前状态"""
self.log("\n当前魔方状态:")
for face in 'UDFBLR':
self.log(f"{face}面:")
for row in self.state[face]:
self.log(" ".join(row))
try:
cube_str = self.get_cube_string()
self.log(f"状态字符串: {cube_str}")
except Exception as e:
self.log(f"状态字符串错误: {str(e)}")
def clear_log(self):
"""清空日志"""
self.log_text.delete(1.0, tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = RubiksCubeGUI(root)
root.mainloop()