def main(): # Init the cube print("Start:") cube = RubiksCube() cube.show() # Shuffle the cube moves = ["F", "R", "Ui", "D", "D"] print("Rotating:", moves) for move in moves: cube.rotate(move) cube.show() # Check if cube is finished if cube.isFinished(): print("Finished!") else: print("Solving...") # Init a frontier with the shuffled cube as start start = Node(state=cube, parent=None, action=None) frontier = QueueFrontier( ) # queue is FIFO; performs a Breadth First Search frontier.add(start) # Keep looping until a solution is found while True: # Choose the first node from the frontier node = frontier.remove() # Add neighbors to the frontier possibleMoves = [ "U", "F", "L", "R", "D", "B", "Ui", "Fi", "Li", "Ri", "Di", "Bi" ] for move in possibleMoves: # Try a move neighborCube = copy.deepcopy(node.state) neighborCube.rotate(move) # This move fixed the cube? if neighborCube.isFinished(): moves = [] moves.append(move) while node.parent is not None: moves.append(node.action) node = node.parent moves.reverse() print("Solved! Moves:", moves) print("Nodes:", len(frontier.frontier)) return moves # No moves fixed the cube; keep going else: child = Node(state=neighborCube, parent=node, action=move) frontier.add(child)
def __init__(self, parent=None): super().__init__(parent) self.cube = RubiksCube() self.orientation = LinearTransform() self.animation_transform = LinearTransform() self.animation_axis = None self.animation_angle = 0.0 self.animation_timer = QtCore.QTimer() self.animation_timer.start(1) self.animation_timer.timeout.connect(self.OnAnimationTimer) self.dragPos = None self.dragging = False self.setTitle('Rubik\'s Cube')
def __init__(self, master): cube = RubiksCube() master.geometry("525x455") master.title("Rubik's cube") frame = Frame(master) frame.place(x=0, y=0, width=500, height=455) self.canvas = Canvas(frame, width=500, height=455) self.canvas.pack() self.state = "flat" menu = Menu(master) master.config(menu=menu) subMenu = Menu(menu) menu.add_cascade(label="Select view type", menu=subMenu) subMenu.add_command(label="Flat", command=lambda: self.draw_flat(cube)) subMenu.add_command(label="Stationary 3D", command=lambda: self.draw_stationary_3D(cube)) # subMenu.add_command(label="Moving 3D", command=cube.move1) # subMenu.add_command(label="Draggable 3D", command=cube.move1) button_texts = [ "||↑", "||↓", "|↑|", "|↓|", "↑||", "↓||", "→--", "←--", "-→-", "-←-", "--→", "--←", "||↰", "||↱", "|↰|", "|↱|", "↰||", "↱||" ] movements = [ lambda: self.move_vertical_cw(cube, 2), lambda: self.move_vertical_acw(cube, 2), lambda: self.move_vertical_cw(cube, 1), lambda: self.move_vertical_acw(cube, 1), lambda: self.move_vertical_cw(cube, 0), lambda: self.move_vertical_acw(cube, 0), lambda: self.move_horizontal_cw(cube, 0), lambda: self.move_horizontal_acw(cube, 0), lambda: self.move_horizontal_cw(cube, 1), lambda: self.move_horizontal_acw(cube, 1), lambda: self.move_horizontal_cw(cube, 2), lambda: self.move_horizontal_acw(cube, 2), lambda: self.move_rotate_cw(cube, 2), lambda: self.move_rotate_acw(cube, 2), lambda: self.move_rotate_cw(cube, 1), lambda: self.move_rotate_acw(cube, 1), lambda: self.move_rotate_cw(cube, 0), lambda: self.move_rotate_acw(cube, 0) ] i = 0 for button in button_texts: button = Button(master, text=button, command=movements[i]) button.place(x=425, y=5 + i * 25, width=100, height=20) i += 1
def setUp(self): self.cube = RubiksCube()
class CubeTurnTests(unittest.TestCase): def setUp(self): self.cube = RubiksCube() def test_L_prime(self): # Corners top_back_left = self.cube.top_layer['back_left'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() # Edges mid_back_left = self.cube.mid_layer['back_left'].sides.copy() top_left_middle = self.cube.top_layer['left_middle'].sides.copy() mid_front_left = self.cube.mid_layer['front_left'].sides.copy() bot_left_middle = self.cube.bot_layer['left_middle'].sides.copy() self.cube._L_prime() self.assertEqual(self.cube.top_layer['back_left'].sides['back'], top_front_left['top']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], top_front_left['front']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], top_front_left['left']) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], bot_front_left['left']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], bot_front_left['bottom']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], bot_back_left['left']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], bot_back_left['bottom']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], bot_back_left['back']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], top_back_left['left']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], top_back_left['top']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], top_back_left['back']) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], top_left_middle['left']) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], top_left_middle['top']) self.assertEqual(self.cube.top_layer['left_middle'].sides['left'], mid_front_left['left']) self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], mid_front_left['front']) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], bot_left_middle['left']) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], bot_left_middle['bottom']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], mid_back_left['left']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['bottom'], mid_back_left['back']) def test_L(self): # Corners top_back_left = self.cube.top_layer['back_left'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() # Edges mid_back_left = self.cube.mid_layer['back_left'].sides.copy() top_left_middle = self.cube.top_layer['left_middle'].sides.copy() mid_front_left = self.cube.mid_layer['front_left'].sides.copy() bot_left_middle = self.cube.bot_layer['left_middle'].sides.copy() self.cube._L() self.assertEqual(self.cube.top_layer['back_left'].sides['back'], bot_back_left['bottom']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], bot_back_left['back']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], bot_back_left['left']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], bot_front_left['left']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], bot_front_left['bottom']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], top_front_left['left']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], top_front_left['top']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], top_front_left['front']) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], top_back_left['left']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], top_back_left['back']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], top_back_left['top']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], mid_front_left['left']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['bottom'], mid_front_left['front']) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], top_left_middle['left']) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], top_left_middle['top']) self.assertEqual(self.cube.top_layer['left_middle'].sides['left'], mid_back_left['left']) self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], mid_back_left['back']) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], bot_left_middle['left']) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], bot_left_middle['bottom']) def test_R(self): # Corners top_back_right = self.cube.top_layer['back_right'].sides.copy() top_front_right = self.cube.top_layer['front_right'].sides.copy() bot_front_right = self.cube.bot_layer['front_right'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() # Edges mid_back_right = self.cube.mid_layer['back_right'].sides.copy() top_right_middle = self.cube.top_layer['right_middle'].sides.copy() mid_front_right = self.cube.mid_layer['front_right'].sides.copy() bot_right_middle = self.cube.bot_layer['right_middle'].sides.copy() self.cube._R() self.assertEqual(self.cube.top_layer['back_right'].sides['top'], top_front_right['front']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], top_front_right['top']) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], top_front_right['right']) self.assertEqual(self.cube.top_layer['front_right'].sides['front'], bot_front_right['bottom']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], bot_front_right['front']) self.assertEqual(self.cube.top_layer['front_right'].sides['right'], bot_front_right['right']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], bot_back_right['back']) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], bot_back_right['bottom']) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], bot_back_right['right']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], top_back_right['top']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], top_back_right['back']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], top_back_right['right']) self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], mid_front_right['front']) self.assertEqual(self.cube.top_layer['right_middle'].sides['right'], mid_front_right['right']) self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], bot_right_middle['bottom']) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], bot_right_middle['right']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['bottom'], mid_back_right['back']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], mid_back_right['right']) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], top_right_middle['top']) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], top_right_middle['right']) def test_R_prime(self): # Corners top_back_right = self.cube.top_layer['back_right'].sides.copy() top_front_right = self.cube.top_layer['front_right'].sides.copy() bot_front_right = self.cube.bot_layer['front_right'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() # Edges mid_back_right = self.cube.mid_layer['back_right'].sides.copy() top_right_middle = self.cube.top_layer['right_middle'].sides.copy() mid_front_right = self.cube.mid_layer['front_right'].sides.copy() bot_right_middle = self.cube.bot_layer['right_middle'].sides.copy() self.cube._R_prime() self.assertEqual(self.cube.top_layer['back_right'].sides['top'], bot_back_right['back']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], bot_back_right['bottom']) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], bot_back_right['right']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], bot_front_right['front']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], bot_front_right['bottom']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], bot_front_right['right']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], top_front_right['front']) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], top_front_right['top']) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], top_front_right['right']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], top_back_right['back']) self.assertEqual(self.cube.top_layer['front_right'].sides['front'], top_back_right['top']) self.assertEqual(self.cube.top_layer['front_right'].sides['right'], top_back_right['right']) self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], mid_back_right['back']) self.assertEqual(self.cube.top_layer['right_middle'].sides['right'], mid_back_right['right']) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], bot_right_middle['bottom']) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], bot_right_middle['right']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['bottom'], mid_front_right['front']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], mid_front_right['right']) self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], top_right_middle['top']) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], top_right_middle['right']) def test_F(self): top_front_right = self.cube.top_layer['front_right'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_front_right = self.cube.bot_layer['front_right'].sides.copy() top_front_middle = self.cube.top_layer['front_middle'].sides.copy() mid_front_left = self.cube.mid_layer['front_left'].sides.copy() bot_front_middle = self.cube.bot_layer['front_middle'].sides.copy() mid_front_right = self.cube.mid_layer['front_right'].sides.copy() self.cube._F() self.assertEqual(self.cube.top_layer['front_right'].sides['right'], top_front_left['top']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], top_front_left['left']) self.assertEqual(self.cube.top_layer['front_right'].sides['front'], top_front_left['front']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], bot_front_left['left']) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], bot_front_left['bottom']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], bot_front_right['bottom']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], bot_front_right['right']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], bot_front_right['front']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], top_front_right['right']) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], top_front_right['top']) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], top_front_right['front']) self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], mid_front_left['left']) self.assertEqual(self.cube.top_layer['front_middle'].sides['front'], mid_front_left['front']) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], bot_front_middle['bottom']) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], bot_front_middle['front']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['bottom'], mid_front_right['right']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], mid_front_right['front']) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], top_front_middle['top']) self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], top_front_middle['front']) def test_F_prime(self): top_front_right = self.cube.top_layer['front_right'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_front_right = self.cube.bot_layer['front_right'].sides.copy() top_front_middle = self.cube.top_layer['front_middle'].sides.copy() mid_front_left = self.cube.mid_layer['front_left'].sides.copy() bot_front_middle = self.cube.bot_layer['front_middle'].sides.copy() mid_front_right = self.cube.mid_layer['front_right'].sides.copy() self.cube._F_prime() self.assertEqual(self.cube.top_layer['front_left'].sides['left'], top_front_right['top']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], top_front_right['right']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], top_front_right['front']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], bot_front_right['right']) self.assertEqual(self.cube.top_layer['front_right'].sides['right'], bot_front_right['bottom']) self.assertEqual(self.cube.top_layer['front_right'].sides['front'], bot_front_right['front']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], bot_front_left['left']) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], bot_front_left['bottom']) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], top_front_left['left']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], top_front_left['top']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], top_front_left['front']) self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], mid_front_right['right']) self.assertEqual(self.cube.top_layer['front_middle'].sides['front'], mid_front_right['front']) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], bot_front_middle['bottom']) self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], bot_front_middle['front']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['bottom'], mid_front_left['left']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], mid_front_left['front']) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], top_front_middle['top']) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], top_front_middle['front']) def test_B(self): top_back_right = self.cube.top_layer['back_right'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() top_back_left = self.cube.top_layer['back_left'].sides.copy() top_back_middle = self.cube.top_layer['back_middle'].sides.copy() mid_back_right = self.cube.mid_layer['back_right'].sides.copy() bot_back_middle = self.cube.bot_layer['back_middle'].sides.copy() mid_back_left = self.cube.mid_layer['back_left'].sides.copy() self.cube._B() self.assertEqual(self.cube.top_layer['back_right'].sides['top'], bot_back_right['right']) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], bot_back_right['bottom']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], bot_back_right['back']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], bot_back_left['bottom']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], bot_back_left['left']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], bot_back_left['back']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], top_back_left['left']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], top_back_left['top']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], top_back_left['back']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], top_back_right['top']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], top_back_right['right']) self.assertEqual(self.cube.top_layer['back_left'].sides['back'], top_back_right['back']) self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], mid_back_right['right']) self.assertEqual(self.cube.top_layer['back_middle'].sides['back'], mid_back_right['back']) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], bot_back_middle['bottom']) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], bot_back_middle['back']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['bottom'], mid_back_left['left']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], mid_back_left['back']) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], top_back_middle['top']) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], top_back_middle['back']) def test_B_prime(self): top_back_right = self.cube.top_layer['back_right'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() top_back_left = self.cube.top_layer['back_left'].sides.copy() top_back_middle = self.cube.top_layer['back_middle'].sides.copy() mid_back_right = self.cube.mid_layer['back_right'].sides.copy() bot_back_middle = self.cube.bot_layer['back_middle'].sides.copy() mid_back_left = self.cube.mid_layer['back_left'].sides.copy() self.cube._B_prime() self.assertEqual(self.cube.top_layer['back_right'].sides['right'], top_back_left['top']) self.assertEqual(self.cube.top_layer['back_right'].sides['top'], top_back_left['left']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], top_back_left['back']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], bot_back_left['left']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], bot_back_left['bottom']) self.assertEqual(self.cube.top_layer['back_left'].sides['back'], bot_back_left['back']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], bot_back_right['bottom']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], bot_back_right['right']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], bot_back_right['back']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], top_back_right['right']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], top_back_right['top']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], top_back_right['back']) self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], mid_back_left['left']) self.assertEqual(self.cube.top_layer['back_middle'].sides['back'], mid_back_left['back']) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], bot_back_middle['bottom']) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], bot_back_middle['back']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['bottom'], mid_back_right['right']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], mid_back_right['back']) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], top_back_middle['top']) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], top_back_middle['back']) def test_U(self): top_front_right = self.cube.top_layer['front_right'].sides.copy() top_back_right = self.cube.top_layer['back_right'].sides.copy() top_back_left = self.cube.top_layer['back_left'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() top_front_middle = self.cube.top_layer['front_middle'].sides.copy() top_right_middle = self.cube.top_layer['right_middle'].sides.copy() top_back_middle = self.cube.top_layer['back_middle'].sides.copy() top_left_middle = self.cube.top_layer['left_middle'].sides.copy() self.cube._U() self.assertEqual(self.cube.top_layer['front_right'].sides['front'], top_back_right['right']) self.assertEqual(self.cube.top_layer['front_right'].sides['right'], top_back_right['back']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], top_back_right['top']) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], top_back_left['back']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], top_back_left['left']) self.assertEqual(self.cube.top_layer['back_right'].sides['top'], top_back_left['top']) self.assertEqual(self.cube.top_layer['back_left'].sides['back'], top_front_left['left']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], top_front_left['front']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], top_front_left['top']) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], top_front_right['front']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], top_front_right['right']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], top_front_right['top']) self.assertEqual(self.cube.top_layer['front_middle'].sides['front'], top_right_middle['right']) self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], top_right_middle['top']) self.assertEqual(self.cube.top_layer['right_middle'].sides['right'], top_back_middle['back']) self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], top_back_middle['top']) self.assertEqual(self.cube.top_layer['back_middle'].sides['back'], top_left_middle['left']) self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], top_left_middle['top']) self.assertEqual(self.cube.top_layer['left_middle'].sides['left'], top_front_middle['front']) self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], top_front_middle['top']) def test_U_prime(self): top_front_right = self.cube.top_layer['front_right'].sides.copy() top_back_right = self.cube.top_layer['back_right'].sides.copy() top_back_left = self.cube.top_layer['back_left'].sides.copy() top_front_left = self.cube.top_layer['front_left'].sides.copy() top_front_middle = self.cube.top_layer['front_middle'].sides.copy() top_right_middle = self.cube.top_layer['right_middle'].sides.copy() top_back_middle = self.cube.top_layer['back_middle'].sides.copy() top_left_middle = self.cube.top_layer['left_middle'].sides.copy() self.cube._U_prime() self.assertEqual(self.cube.top_layer['front_right'].sides['right'], top_front_left['front']) self.assertEqual(self.cube.top_layer['front_right'].sides['front'], top_front_left['left']) self.assertEqual(self.cube.top_layer['front_right'].sides['top'], top_front_left['top']) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], top_back_left['left']) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], top_back_left['back']) self.assertEqual(self.cube.top_layer['front_left'].sides['top'], top_back_left['top']) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], top_back_right['back']) self.assertEqual(self.cube.top_layer['back_left'].sides['back'], top_back_right['right']) self.assertEqual(self.cube.top_layer['back_left'].sides['top'], top_back_right['top']) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], top_front_right['right']) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], top_front_right['front']) self.assertEqual(self.cube.top_layer['back_right'].sides['top'], top_front_right['top']) self.assertEqual(self.cube.top_layer['front_middle'].sides['front'], top_left_middle['left']) self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], top_left_middle['top']) self.assertEqual(self.cube.top_layer['left_middle'].sides['left'], top_back_middle['back']) self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], top_back_middle['top']) self.assertEqual(self.cube.top_layer['back_middle'].sides['back'], top_right_middle['right']) self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], top_right_middle['top']) self.assertEqual(self.cube.top_layer['right_middle'].sides['right'], top_front_middle['front']) self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], top_front_middle['top']) def test_D(self): bot_front_right = self.cube.bot_layer['front_right'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() bot_front_middle = self.cube.bot_layer['front_middle'].sides.copy() bot_left_middle = self.cube.bot_layer['left_middle'].sides.copy() bot_back_middle = self.cube.bot_layer['back_middle'].sides.copy() bot_right_middle = self.cube.bot_layer['right_middle'].sides.copy() self.cube._D() self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], bot_front_left['left']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], bot_front_left['bottom']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], bot_back_left['left']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], bot_back_left['back']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], bot_back_left['bottom']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], bot_back_right['back']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], bot_back_right['right']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], bot_back_right['bottom']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], bot_front_right['right']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], bot_front_right['front']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], bot_front_right['bottom']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], bot_left_middle['left']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['bottom'], bot_left_middle['bottom']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], bot_back_middle['back']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['bottom'], bot_back_middle['bottom']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], bot_right_middle['right']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['bottom'], bot_right_middle['bottom']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], bot_front_middle['front']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['bottom'], bot_front_middle['bottom']) def test_D_prime(self): bot_front_right = self.cube.bot_layer['front_right'].sides.copy() bot_front_left = self.cube.bot_layer['front_left'].sides.copy() bot_back_left = self.cube.bot_layer['back_left'].sides.copy() bot_back_right = self.cube.bot_layer['back_right'].sides.copy() bot_front_middle = self.cube.bot_layer['front_middle'].sides.copy() bot_left_middle = self.cube.bot_layer['left_middle'].sides.copy() bot_back_middle = self.cube.bot_layer['back_middle'].sides.copy() bot_right_middle = self.cube.bot_layer['right_middle'].sides.copy() self.cube._D_prime() self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], bot_back_right['right']) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], bot_back_right['back']) self.assertEqual(self.cube.bot_layer['front_right'].sides['bottom'], bot_back_right['bottom']) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], bot_back_left['back']) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], bot_back_left['left']) self.assertEqual(self.cube.bot_layer['back_right'].sides['bottom'], bot_back_left['bottom']) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], bot_front_left['left']) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], bot_front_left['front']) self.assertEqual(self.cube.bot_layer['back_left'].sides['bottom'], bot_front_left['bottom']) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], bot_front_right['front']) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], bot_front_right['right']) self.assertEqual(self.cube.bot_layer['front_left'].sides['bottom'], bot_front_right['bottom']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], bot_right_middle['right']) self.assertEqual(self.cube.bot_layer['front_middle'].sides['bottom'], bot_right_middle['bottom']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], bot_back_middle['back']) self.assertEqual(self.cube.bot_layer['right_middle'].sides['bottom'], bot_back_middle['bottom']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], bot_left_middle['left']) self.assertEqual(self.cube.bot_layer['back_middle'].sides['bottom'], bot_left_middle['bottom']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], bot_front_middle['front']) self.assertEqual(self.cube.bot_layer['left_middle'].sides['bottom'], bot_front_middle['bottom'])
class CubeSolutionTests(unittest.TestCase): def setUp(self): self.cube = RubiksCube() # test solution funcs <- make sure the tests arent interfering with each other def bottom_face_tester(self): self.assertTrue(all(self.cube.bot_layer[bottom].sides['bottom'] for bottom in self.cube.bot_layer)) def bottom_layer_tester(self): right_center = self.cube.mid_layer['right_center'].sides['right'] left_center = self.cube.mid_layer['left_center'].sides['left'] front_center = self.cube.mid_layer['front_center'].sides['front'] back_center = self.cube.mid_layer['back_center'].sides['back'] self.assertTrue(all(self.cube.bot_layer[bottom].sides['bottom'] for bottom in self.cube.bot_layer)) # all 'sides' match their corresponding center self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], right_center) def middle_layer_tester(self): front_center = self.cube.mid_layer['front_center'].sides['front'] left_center = self.cube.mid_layer['left_center'].sides['left'] right_center = self.cube.mid_layer['right_center'].sides['right'] back_center = self.cube.mid_layer['back_center'].sides['back'] self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], front_center) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], left_center) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], back_center) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], right_center) def test_make_daisy(self): bottom_center = self.cube.bot_layer['bottom_center'].sides['bottom'] self.cube._make_daisy() self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], bottom_center) self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], bottom_center) self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], bottom_center) self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], bottom_center) def test_bottom_cross(self): bottom_center = self.cube.bot_layer['bottom_center'].sides['bottom'] self.cube._bottom_cross() self.assertEqual(self.cube.bot_layer['right_middle'].sides['bottom'], bottom_center) self.assertEqual(self.cube.bot_layer['left_middle'].sides['bottom'], bottom_center) self.assertEqual(self.cube.bot_layer['front_middle'].sides['bottom'], bottom_center) self.assertEqual(self.cube.bot_layer['back_middle'].sides['bottom'], bottom_center) # also test the other sides match their face right_center = self.cube.mid_layer['right_center'].sides['right'] left_center = self.cube.mid_layer['left_center'].sides['left'] front_center = self.cube.mid_layer['front_center'].sides['front'] back_center = self.cube.mid_layer['back_center'].sides['back'] self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], left_center) def test_bottom_layer(self): self.cube._solve_bot_layer() self.bottom_layer_tester() # def test_bottom_layer_robustness(self, n=50): # for _ in range(n): # self.cube.initialize_cube() # self.test_bottom_layer() # print("Success") def test_middle_layer(self): self.cube._solve_mid_layer() self.bottom_layer_tester() self.middle_layer_tester() # def test_middle_layer_robustness(self, n=50): # for _ in range(n): # self.cube.initialize_cube() # self.cube._solve_mid_layer() def test_top_cross(self): self.cube._solve_top_cross() self.bottom_layer_tester() self.middle_layer_tester() self.assertEqual(self.cube.top_layer['left_middle'].sides['top'], 'y') self.assertEqual(self.cube.top_layer['front_middle'].sides['top'], 'y') self.assertEqual(self.cube.top_layer['right_middle'].sides['top'], 'y') self.assertEqual(self.cube.top_layer['back_middle'].sides['top'], 'y') # def test_top_cross_robustness(self, n=50): # for _ in range(n): # self.cube.initialize_cube() # self.test_top_cross() def test_top_face(self): self.cube._solve_top_face() self.bottom_layer_tester() self.middle_layer_tester() self.assertTrue(all(self.cube.top_layer[top].sides['top'] for top in self.cube.top_layer)) # def test_top_face_robustness(self, n=50): # for _ in range(n): # self.cube.initialize_cube() # self.test_top_face() def test_top_corners(self): self.cube._solve_top_corners() self.bottom_layer_tester() self.middle_layer_tester() # test all four corners match their centers. # front front_center = self.cube.mid_layer['front_center'].sides['front'] self.assertEqual(self.cube.top_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], front_center) # left left_center = self.cube.mid_layer['left_center'].sides['left'] self.assertEqual(self.cube.top_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.top_layer['front_left'].sides['left'], left_center) # back back_center = self.cube.mid_layer['back_center'].sides['back'] self.assertEqual(self.cube.top_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.top_layer['back_right'].sides['back'], back_center) # right right_center = self.cube.mid_layer['right_center'].sides['right'] self.assertEqual(self.cube.top_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.top_layer['front_right'].sides['right'], right_center) # def test_top_corners_robustness(self, n=50): # for _ in range(n): # self.cube.initialize_cube() # self.test_top_corners() def test_solved_cube(self): self.cube.solve_cube() front_center = self.cube.mid_layer['front_center'].sides['front'] back_center = self.cube.mid_layer['back_center'].sides['back'] right_center = self.cube.mid_layer['right_center'].sides['right'] left_center = self.cube.mid_layer['left_center'].sides['left'] # top face self.assertTrue(all(self.cube.top_layer[top].sides['top'] for top in self.cube.top_layer)) # bot face self.bottom_face_tester() # front face self.assertEqual(self.cube.top_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.top_layer['front_middle'].sides['front'], front_center) self.assertEqual(self.cube.top_layer['front_left'].sides['front'], front_center) self.assertEqual(self.cube.mid_layer['front_left'].sides['front'], front_center) self.assertEqual(self.cube.mid_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_right'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_left'].sides['front'], front_center) self.assertEqual(self.cube.bot_layer['front_middle'].sides['front'], front_center) # back face self.assertEqual(self.cube.top_layer['back_right'].sides['back'], back_center) self.assertEqual(self.cube.top_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.top_layer['back_middle'].sides['back'], back_center) self.assertEqual(self.cube.mid_layer['back_right'].sides['back'], back_center) self.assertEqual(self.cube.mid_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_left'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_right'].sides['back'], back_center) self.assertEqual(self.cube.bot_layer['back_middle'].sides['back'], back_center) # right face self.assertEqual(self.cube.top_layer['front_right'].sides['right'], right_center) self.assertEqual(self.cube.top_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.top_layer['right_middle'].sides['right'], right_center) self.assertEqual(self.cube.mid_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.mid_layer['front_right'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['front_right'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['back_right'].sides['right'], right_center) self.assertEqual(self.cube.bot_layer['right_middle'].sides['right'], right_center) # left face self.assertEqual(self.cube.top_layer['front_left'].sides['left'], left_center) self.assertEqual(self.cube.top_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.top_layer['left_middle'].sides['left'], left_center) self.assertEqual(self.cube.mid_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.mid_layer['front_left'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['front_left'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['back_left'].sides['left'], left_center) self.assertEqual(self.cube.bot_layer['left_middle'].sides['left'], left_center) def test_solved_cube_robustness(self, n=50): for _ in range(n): self.cube.initialize_cube() self.cube.solve_cube()
class RubiksCubeWindow(QtGui.QOpenGLWindow): def __init__(self, parent=None): super().__init__(parent) self.cube = RubiksCube() self.orientation = LinearTransform() self.animation_transform = LinearTransform() self.animation_axis = None self.animation_angle = 0.0 self.animation_timer = QtCore.QTimer() self.animation_timer.start(1) self.animation_timer.timeout.connect(self.OnAnimationTimer) self.dragPos = None self.dragging = False self.setTitle('Rubik\'s Cube') def initializeGL(self): GL.glShadeModel(GL.GL_SMOOTH) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(0.7, 0.7, 0.7, 0.0) def paintGL(self): # TODO: Get the Z-buffer working. GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) viewport = GL.glGetIntegerv(GL.GL_VIEWPORT) width = viewport[2] height = viewport[3] aspectRatio = float(width) / float(height) length = 2.5 GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() if aspectRatio > 1.0: GL.glOrtho(-length * aspectRatio, length * aspectRatio, -length, length, -1.0, 100.0) else: GL.glOrtho(-length, length, -length / aspectRatio, length / aspectRatio, -1.0, 100.0) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() GLU.gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) GL.glBegin(GL.GL_QUADS) for x in range(3): for y in range(3): for z in range(3): cubie = self.cube.cubie_matrix[x][y][z] self.RenderCubieQuads(cubie) GL.glEnd() GL.glFlush() def RenderCubieQuads(self, cubie): center = cubie.position quad_vertices = [ Vector(-0.45, -0.45, 0.0), Vector(0.45, -0.45, 0.0), Vector(0.45, 0.45, 0.0), Vector(-0.45, 0.45, 0.0) ] for face in cubie.face_list: normal = face.normal if cubie.animation_transform: normal = cubie.animation_transform * normal normal = self.orientation * normal if normal.z < 0: continue self.RenderColor(face.color) frame = LinearTransform() frame.MakeFrame(face.normal) for vertex in quad_vertices: vertex = frame * vertex + center + face.normal * 0.5 if cubie.animation_transform: vertex = cubie.animation_transform * vertex vertex = self.orientation * vertex GL.glVertex3f(vertex.x, vertex.y, vertex.z) # TODO: Render constraint arrows. def RenderColor(self, color): if color == 'blue': GL.glColor3f(0.0, 0.0, 1.0) elif color == 'green': GL.glColor3f(0.0, 1.0, 0.0) elif color == 'yellow': GL.glColor3f(1.0, 1.0, 0.0) elif color == 'white': GL.glColor3f(1.0, 1.0, 1.0) elif color == 'red': GL.glColor3f(1.0, 0.0, 0.0) elif color == 'orange': GL.glColor3f(1.0, 0.5, 0.0) else: GL.glColor3f(0.0, 0.0, 0.0) def resizeGL(self, width, height): GL.glViewport(0, 0, width, height) def mousePressEvent(self, event): button = event.button() if button == QtCore.Qt.LeftButton: self.dragPos = event.pos() self.dragging = True def mouseMoveEvent(self, event): if self.dragging: pos = event.pos() dragVector = Vector() dragVector.x = float(pos.x() - self.dragPos.x()) dragVector.y = float(pos.y() - self.dragPos.y()) self.dragPos = pos scale = 0.01 xAngle = scale * dragVector.y yAngle = scale * dragVector.x xAxisRotation = LinearTransform() yAxisRotation = LinearTransform() xAxisRotation.MakeRotation(Vector(1.0, 0.0, 0.0), xAngle) yAxisRotation.MakeRotation(Vector(0.0, 1.0, 0.0), yAngle) self.orientation = xAxisRotation * self.orientation self.orientation = yAxisRotation * self.orientation self.orientation.Orthonormalize( ) # Do this just to deal with accumulated round-off error. self.update() def mouseReleaseEvent(self, event): self.dragging = False def ClearAnimation(self): for i in range(3): for j in range(3): for k in range(3): self.cube.cubie_matrix[i][j][k].animation_transform = None def wheelEvent(self, event): angleDelta = event.angleDelta().y() steps = angleDelta / 120 self.animation_angle = math.pi / 2.0 * float(steps) direction = 'cw' if steps < 0: steps = -steps direction = 'ccw' axis = self.FindFacingAxis() if not axis: return self.ClearAnimation() while steps > 0: if not self.cube.RotateFace(axis, direction): return steps -= 1 cubie_list = self.cube.CollectCubiesForAxis(axis) for cubie in cubie_list: cubie.animation_transform = self.animation_transform self.animation_axis = axis self.animation_transform.MakeRotation(self.animation_axis, self.animation_angle) self.update() def OnAnimationTimer(self): if math.fabs(self.animation_angle) > 0.0: animation_rate = math.pi / 128.0 if self.animation_angle > 0.0: self.animation_angle -= animation_rate if self.animation_angle < 0.0: self.animation_angle = 0.0 else: self.animation_angle += animation_rate if self.animation_angle > 0.0: self.animation_angle = 0.0 self.animation_transform.MakeRotation(self.animation_axis, self.animation_angle) self.update() def FindFacingAxis(self): best_axis = None best_z = 0.0 for i in range(2): for j in range(3): axis = Vector() c = 1.0 if i == 0 else -1.0 axis.x = c if j == 0 else 0.0 axis.y = c if j == 1 else 0.0 axis.z = c if j == 2 else 0.0 transformed_axis = self.orientation * axis if transformed_axis.z > best_z: best_z = transformed_axis.z best_axis = axis return best_axis