def test_expected(self): rubrik1 = Rubik(*list("YRBOGW")) rubrik1.parse_instructions("R2") self.assertEqual(rubrik1.side_to_string(side='f'), "B B G\nB B G\nB B G") rubrik2 = Rubik(*list("YORBWG")) rubrik2.parse_instructions("RL'") self.assertEqual(rubrik2.side_to_string(side='f'), "G R G\nG R G\nG R G")
def mainloop(): pygame.init() clock = pygame.time.Clock() win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Rubik's Cube") run = True rubik = Rubik() points, centers, edges, corners = get_init_points() save_positions, reset_positions = handle_save_points(points) handle_mouse_drag = init_mouse_drag(points) in_progress_animation, init_move, animate = animation( rubik, centers, edges, corners) handle_functional_keys, in_progress_function, continue_function = init_functional_keys( rubik, init_move) handle_key_event = init_handle_keys(init_move, save_positions, reset_positions) while run: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT or \ (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or \ (event.type == pygame.KEYDOWN and event.key == pygame.K_q): run = False if not in_progress_animation() and not in_progress_function(): handle_mouse_drag(event) handle_key_event(event) handle_functional_keys(event) if in_progress_animation(): animate() elif in_progress_function(): continue_function() else: handle_rotation_keys(points) win.fill((128, 128, 128)) draw_orientation(win, rubik) draw_rubik(win, rubik, centers, edges, corners) pygame.display.update() pygame.quit()
def phase_three_corners(rubik, debug): tree = init_phase(rubik) moves = bfs(rubik, tree, 'phase_three', validate_phase_three_corners) if debug: pass #print tree return moves def phase_three_edges(rubik, debug): tree = init_phase(rubik) moves = bfs(rubik, tree, 'phase_three', validate_phase_three_edges) if debug: pass #print tree return moves def validate_phase_four(rubik): return rubik.is_solved() def phase_four(rubik, debug): tree = init_phase(rubik) moves = bfs(rubik, tree, 'phase_four', validate_phase_four) if debug: pass #print tree return moves if __name__ == '__main__': rubik = Rubik() rubik.scramble() moves = solve(rubik, debug=True) print moves
relatively_correct[i] = red_blocks[i] and opposites[i] == opposite for i in xrange(len(relatively_correct)): if not relatively_correct[i]: face, x, y = find_edge(rubik, [color_U, opposites[i]]) def find_edge(rubik, colors): edge_coordinates = [(2, 1), (1, 2), (0, 1), (1, 0)] for face in rubik.faces.keys(): for i, j in edge_coordinates: if rubik.faces[face][i][j] == colors[0] and get_opposite_edge_color( rubik, face + str(i) + str(j)) == colors[1]: return face, i, j def get_opposite_edge_color(rubik, key): dictionary = { 'U01': rubik.faces['F'][2][1], 'U10': rubik.faces['L'][1][0], 'U12': rubik.faces['R'][1][2], 'U21': rubik.faces['B'][0][1], } return dictionary[key] if __name__ == '__main__': rubik = Rubik() first_cross(rubik) rubik.describe() #solve(rubik)
# диапазон длины начальной комбинации для "разборки" кубика InitMoveRange = 10, 20 # диапазон для генерации случайных последовательностей RandomSequenceRange = 1, 26 # максимальное количество проверенных комбинаций, не приведших к учеличению индекса решенности # по достижении данной величины происходит возврат состояния кубика к предыдущему сохраненному состоянию #MaxUngoodSequences = 100000 MaxUngoodSequences = sum( [12**i for i in range(RandomSequenceRange[0], RandomSequenceRange[1] + 1)]) * 2 ### ИНИЦИАЛИЗАЦИЯ print("MaxUngoodSequences= ", MaxUngoodSequences) # создание объекта класса Rubik rubik = Rubik() # печать процента решенности кубика print("solved on", rubik.get_solution_percent(), "%") # случайная перестановка граней кубика print("gettind some entropy ...") InitialSequence = rubik.get_random_sequence(InitMoveRange[0], InitMoveRange[1]) #print("initial sequence:", InitialSequence) print("random moves count", rubik.execute_sequence(InitialSequence)) # печать развертки кубика rubik.print_layout() # начальный индекс решенности CurrentSolutionIndex = rubik._get_solution_index() # печать процента решенности кубика print("solved on", rubik.get_solution_percent(), "%") # сброс счетчика движений rubik.reset_rotate_count()
#!/usr/bin/env python # -*- coding: utf-8 -*- from rubik import Rubik InitMoveRange = 1, 5 rubik = Rubik() print("solved on", rubik.get_solution_percent()) for i in range(20): InitialSequence = rubik.get_random_sequence(InitMoveRange[0], InitMoveRange[1]) print(InitialSequence)
def main(): rubik = Rubik(2) rotate_cube = { pygame.K_UP: (0, -1), pygame.K_DOWN: (0, 1), pygame.K_LEFT: (1, -1), pygame.K_RIGHT: (1, 1) } rotate_slc = { pygame.K_1: (0, 0, 1), pygame.K_2: (0, 1, 1), pygame.K_3: (0, 2, 1), pygame.K_4: (1, 0, 1), pygame.K_5: (1, 1, 1), pygame.K_6: (1, 2, 1), pygame.K_7: (2, 0, 1), pygame.K_8: (2, 1, 1), pygame.K_9: (2, 2, 1) } ang_x, ang_y = 45, -135 rot_cube = [0, 0] animate = False animate_ang = 0 animate_speed = 5 rotate = (0, 0, 0) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key in rotate_cube: value = rotate_cube[event.key] rot_cube[value[0]] = value[1] if not animate and event.key in rotate_slc: animate = True rotate = rotate_slc[event.key] if event.type == pygame.KEYUP: if event.key in rotate_cube: rot_cube = [0, 0] ang_x += rot_cube[0] * 2 ang_y += rot_cube[1] * 2 GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() GL.glTranslatef(0, 0, -40) GL.glRotatef(ang_y, 0, 1, 0) GL.glRotatef(ang_x, 1, 0, 0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glClearColor(1, 1, 1, 1) if animate: if animate_ang >= 90: for cube in rubik.cubes: cube.update(*rotate) animate = False animate_ang = 0 for cube in rubik.cubes: cube.draw(cube.polygons, animate, animate_ang, *rotate) if animate: animate_ang += animate_speed pygame.display.flip() pygame.time.wait(10)
q_target = r # next state is terminal self.q_table.ix[s, a] += self.lr * (q_target - q_predict) # update def check_state_exist(self, state): if state not in self.q_table.index: # append new state to q table self.q_table = self.q_table.append( pd.Series( [0] * len(self.actions), index=self.q_table.columns, name=state, )) if __name__ == "__main__": rubik = Rubik() RL = QLearningTable(actions=list(range(rubik.n_actions))) # RL.load_experience('./q_table_100000.csv') success = 0 for episode in range(100000): # initial observation state = rubik.state while True: # fresh env # RL choose action based on observation action = RL.choose_action(str(state)) # RL take action and get next observation and reward state_, reward, done = rubik.take_action(action) # RL learn from this transition
def create_cube_from_node(tree, node_id): return Rubik(tree.get_node(node_id).identifier)
def capture_face(self): if len(self.faces) < 6: face = [] for row in self.current_face: face.append([]) for cell in row: face[-1].append(cell) next_face = self.POSSIBLE_FACES[len(self.faces)] if next_face == "B": # DONE!!!!! #face = face[::-1] # Hacer espejo vertical, no horizontal for i in xrange(len(face)): face[i] = face[i][::-1] elif next_face == "R": face = face[::-1] face = zip(*face[::-1]) # Girar 90 grados # face = face[::-1] # girar 90 grados, hacer espejo horizontal elif next_face == "L": # DONE!!!! for i in xrange(len(face)): face[i] = face[i][::-1] face = zip(*face[::-1]) elif next_face == "F": # DONE!!!!! face = face[::-1] elif next_face == "U": face = face[::-1] face = zip(*face[::-1]) pass # Girar 270 grados elif next_face == "D": face = face[::-1] face = zip(*face[::-1]) pass # Espejo y 90 grados self.faces[next_face] = face print face if len(self.faces) == 6: colors = [] colors.append(self.map_face( self.faces["B"] )) colors.append(self.map_face( self.faces["F"] )) colors.append(self.map_face( self.faces["R"] )) colors.append(self.map_face( self.faces["L"] )) colors.append(self.map_face( self.faces["D"] )) colors.append(self.map_face( self.faces["U"] )) # colors = [[0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4]] colors_rgb = {} colors_rgb["B"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["B"], 3))) colors_rgb["F"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["F"], 3))) colors_rgb["R"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["R"], 3))) colors_rgb["L"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["L"], 3))) colors_rgb["U"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["D"], 1))) colors_rgb["D"] = self.map_face_RGB( self.flip_matrix( self.rotate_matrix(self.faces["U"], 3))) rubik = Rubik(colors_rgb) rubik.describe() c = Cube(self.dimensions, None, None, colors) c.draw_interactive() plt.show() solve(Rubik(colors_rgb))
def capture_face(self): if len(self.faces) < 6: face = [] for row in self.current_face: face.append([]) for cell in row: face[-1].append(cell) next_face = self.POSSIBLE_FACES[len(self.faces)] if next_face == "B": # DONE!!!!! #face = face[::-1] # Hacer espejo vertical, no horizontal for i in xrange(len(face)): face[i] = face[i][::-1] elif next_face == "R": face = face[::-1] face = zip(*face[::-1]) # Girar 90 grados # face = face[::-1] # girar 90 grados, hacer espejo horizontal elif next_face == "L": # DONE!!!! for i in xrange(len(face)): face[i] = face[i][::-1] face = zip(*face[::-1]) elif next_face == "F": # DONE!!!!! face = face[::-1] elif next_face == "U": face = face[::-1] face = zip(*face[::-1]) pass # Girar 270 grados elif next_face == "D": face = face[::-1] face = zip(*face[::-1]) pass # Espejo y 90 grados self.faces[next_face] = face print face if len(self.faces) == 6: colors = [] colors.append(self.map_face(self.faces["B"])) colors.append(self.map_face(self.faces["F"])) colors.append(self.map_face(self.faces["R"])) colors.append(self.map_face(self.faces["L"])) colors.append(self.map_face(self.faces["D"])) colors.append(self.map_face(self.faces["U"])) # colors = [[0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4], [0,1,2,3,4,5,3,2,4]] colors_rgb = {} colors_rgb["B"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["B"], 3))) colors_rgb["F"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["F"], 3))) colors_rgb["R"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["R"], 3))) colors_rgb["L"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["L"], 3))) colors_rgb["U"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["D"], 1))) colors_rgb["D"] = self.map_face_RGB( self.flip_matrix(self.rotate_matrix(self.faces["U"], 3))) rubik = Rubik(colors_rgb) rubik.describe() c = Cube(self.dimensions, None, None, colors) c.draw_interactive() plt.show() solve(Rubik(colors_rgb))
def setUp(self): self.rubik = Rubik(*list("YRBOGW"))
class TestRubik(unittest.TestCase): def setUp(self): self.rubik = Rubik(*list("YRBOGW")) def test_export(self): g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_f(self): self.rubik.move_f() g = [ #[self.up, self.left, self.front, self.right, self.back, self.down] [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['R', 'R', 'R']], # up [['R', 'R', 'W'],['R', 'R', 'W'],['R', 'R', 'W']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['Y', 'O', 'O'],['Y', 'O', 'O'],['Y', 'O', 'O']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['O', 'O', 'O'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_f_reverse(self): self.rubik.move_f(reverse=True) g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['O', 'O', 'O']], # up [['R', 'R', 'Y'],['R', 'R', 'Y'],['R', 'R', 'Y']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['W', 'O', 'O'],['W', 'O', 'O'],['W', 'O', 'O']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['R', 'R', 'R'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_b(self): self.rubik.move_b() g = [ [['O', 'O', 'O'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['Y', 'R', 'R'],['Y', 'R', 'R'],['Y', 'R', 'R']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['O', 'O', 'W'],['O', 'O', 'W'],['O', 'O', 'W']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['R', 'R', 'R']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_b_reverse(self): self.rubik.move_b(reverse=True) g = [ [['R', 'R', 'R'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['W', 'R', 'R'],['W', 'R', 'R'],['W', 'R', 'R']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['O', 'O', 'Y'],['O', 'O', 'Y'],['O', 'O', 'Y']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['O', 'O', 'O']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_u(self): self.rubik.move_u() g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['B', 'B', 'B'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['O', 'O', 'O'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['G', 'G', 'G'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['R', 'R', 'R']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_u_reverse(self): self.rubik.move_u(reverse=True) g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['G', 'G', 'G'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['R', 'R', 'R'],['B', 'B', 'B'],['B', 'B', 'B']], # front [['B', 'B', 'B'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['G', 'G', 'G'],['G', 'G', 'G'],['O', 'O', 'O']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_d(self): self.rubik.move_d() g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['G', 'G', 'G']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['R', 'R', 'R']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['B', 'B', 'B']], # right [['O', 'O', 'O'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_d_reverse(self): self.rubik.move_d(reverse=True) g = [ [['Y', 'Y', 'Y'],['Y', 'Y', 'Y'],['Y', 'Y', 'Y']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['B', 'B', 'B']], # left [['B', 'B', 'B'],['B', 'B', 'B'],['O', 'O', 'O']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['G', 'G', 'G']], # right [['R', 'R', 'R'],['G', 'G', 'G'],['G', 'G', 'G']], # back [['W', 'W', 'W'],['W', 'W', 'W'],['W', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_l(self): self.rubik.move_l() g = [ [['G', 'Y', 'Y'],['G', 'Y', 'Y'],['G', 'Y', 'Y']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['Y', 'B', 'B'],['Y', 'B', 'B'],['Y', 'B', 'B']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['W', 'G', 'G'],['W', 'G', 'G'],['W', 'G', 'G']], # back [['B', 'W', 'W'],['B', 'W', 'W'],['B', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_l_reverse(self): self.rubik.move_l(reverse=True) g = [ [['B', 'Y', 'Y'],['B', 'Y', 'Y'],['B', 'Y', 'Y']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['W', 'B', 'B'],['W', 'B', 'B'],['W', 'B', 'B']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['Y', 'G', 'G'],['Y', 'G', 'G'],['Y', 'G', 'G']], # back [['G', 'W', 'W'],['G', 'W', 'W'],['G', 'W', 'W']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_r(self): self.rubik.move_r() g = [ [['Y', 'Y', 'B'],['Y', 'Y', 'B'],['Y', 'Y', 'B']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['B', 'B', 'W'],['B', 'B', 'W'],['B', 'B', 'W']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['G', 'G', 'Y'],['G', 'G', 'Y'],['G', 'G', 'Y']], # back [['W', 'W', 'G'],['W', 'W', 'G'],['W', 'W', 'G']], # down ] self.assertEqual(self.rubik.export(),g) def test_move_r_reverse(self): self.rubik.move_r(reverse=True) g = [ [['Y', 'Y', 'G'],['Y', 'Y', 'G'],['Y', 'Y', 'G']], # up [['R', 'R', 'R'],['R', 'R', 'R'],['R', 'R', 'R']], # left [['B', 'B', 'Y'],['B', 'B', 'Y'],['B', 'B', 'Y']], # front [['O', 'O', 'O'],['O', 'O', 'O'],['O', 'O', 'O']], # right [['G', 'G', 'W'],['G', 'G', 'W'],['G', 'G', 'W']], # back [['W', 'W', 'B'],['W', 'W', 'B'],['W', 'W', 'B']], # down ] self.assertEqual(self.rubik.export(),g) def test_parse_instructions_forward(self): r = self.rubik g = copy.deepcopy(r) # adjust r r.move_f() r.move_b() r.move_u() r.move_d() r.move_l() r.move_r() r.move_f() r.move_b() r.move_u() r.move_d() r.move_l() r.move_r() # adjust g g.parse_instructions("FBUDLRFBUDLR") self.assertEqual(r.export(),g.export()) def test_parse_instructions_reverse(self): r = self.rubik g = copy.deepcopy(r) # adjust r r.move_f(reverse=True) r.move_b(reverse=True) r.move_u(reverse=True) r.move_d(reverse=True) r.move_l(reverse=True) r.move_r(reverse=True) # adjust g g.parse_instructions("F'B'U'D'L'R'") self.assertEqual(r.export(),g.export()) def test_parse_instructions_twos(self): r = self.rubik g = copy.deepcopy(r) # adjust r r.move_f() r.move_f() r.move_b() r.move_b() r.move_u() r.move_u() r.move_d() r.move_d() r.move_l() r.move_l() r.move_r() r.move_r() # adjust g g.parse_instructions("F2B2U2D2L2R2") self.assertEqual(r.export(),g.export()) def test_expected(self): rubrik1 = Rubik(*list("YRBOGW")) rubrik1.parse_instructions("R2") self.assertEqual(rubrik1.side_to_string(side='f'), "B B G\nB B G\nB B G") rubrik2 = Rubik(*list("YORBWG")) rubrik2.parse_instructions("RL'") self.assertEqual(rubrik2.side_to_string(side='f'), "G R G\nG R G\nG R G") def test_complex_steps(self): r = Rubik(*list("WGOYRB")) self.assertEqual(r.side_to_string(side='u'), "W W W\nW W W\nW W W") r.move_l() self.assertEqual(r.side_to_string(side='f'), "W O O\nW O O\nW O O") self.assertEqual(r.side_to_string(side='u'), "R W W\nR W W\nR W W") r.move_b() self.assertEqual(r.side_to_string(side='l'), "W G G\nW G G\nR G G") self.assertEqual(r.side_to_string(side='u'), "Y Y Y\nR W W\nR W W") r.move_u(reverse=True) self.assertEqual(r.side_to_string(side='f'), "W G G\nW O O\nW O O") self.assertEqual(r.side_to_string(side='u'), "Y W W\nY W W\nY R R") r.move_d(reverse=True) self.assertEqual(r.side_to_string(side='f'), "W G G\nW O O\nY Y O") self.assertEqual(r.side_to_string(side='u'), "Y W W\nY W W\nY R R") r.move_b(reverse=True) self.assertEqual(r.side_to_string(side='u'), "W W R\nY W W\nY R R")
def test_complex_steps(self): r = Rubik(*list("WGOYRB")) self.assertEqual(r.side_to_string(side='u'), "W W W\nW W W\nW W W") r.move_l() self.assertEqual(r.side_to_string(side='f'), "W O O\nW O O\nW O O") self.assertEqual(r.side_to_string(side='u'), "R W W\nR W W\nR W W") r.move_b() self.assertEqual(r.side_to_string(side='l'), "W G G\nW G G\nR G G") self.assertEqual(r.side_to_string(side='u'), "Y Y Y\nR W W\nR W W") r.move_u(reverse=True) self.assertEqual(r.side_to_string(side='f'), "W G G\nW O O\nW O O") self.assertEqual(r.side_to_string(side='u'), "Y W W\nY W W\nY R R") r.move_d(reverse=True) self.assertEqual(r.side_to_string(side='f'), "W G G\nW O O\nY Y O") self.assertEqual(r.side_to_string(side='u'), "Y W W\nY W W\nY R R") r.move_b(reverse=True) self.assertEqual(r.side_to_string(side='u'), "W W R\nY W W\nY R R")
#!/usr/bin/env python # -*- coding: utf-8 -*- from rubik import Rubik # поиск алгоритма бога для случайного состояния кубика # создание объекта класса Rubik rubik = Rubik() # печать процента решенности кубика print("solved on", rubik.get_solution_percent(), "%") # случайная перестановка граней кубика print("gettind some entropy ...") InitialSequence = rubik.get_random_sequence() #print("initial sequence:", InitialSequence) print("random moves count", rubik.execute_sequence(InitialSequence)) # печать развертки кубика rubik.print_layout() # печать процента решенности кубика print("solved on", rubik.get_solution_percent(), "%") InitialLayout = rubik._get_layout() # сброс счетчика движений rubik.reset_rotate_count() # начало поиска print("begin solving ...") # максимальный процент решенности кубика MaxPercent = 0 # количество проверенных комбинаций CombinationsCount = 0 while not rubik.is_solved(): # восстанавливаем исходное состояние кубика rubik._set_layout(InitialLayout) # генерируем случайную комбинацию из 26 движений (число бога)
standar_colors(rubik.faces['D'][1][2]) + standar_colors(rubik.faces['R'][1][0]), #DR standar_colors(rubik.faces['D'][0][1]) + standar_colors(rubik.faces['B'][2][1]), #DB standar_colors(rubik.faces['D'][1][0]) + standar_colors(rubik.faces['L'][1][2]), #DL standar_colors(rubik.faces['F'][1][2]) + standar_colors(rubik.faces['R'][2][1]), #FR standar_colors(rubik.faces['F'][1][0]) + standar_colors(rubik.faces['L'][2][1]), #FL standar_colors(rubik.faces['B'][1][2]) + standar_colors(rubik.faces['R'][0][1]), #BR standar_colors(rubik.faces['B'][1][0]) + standar_colors(rubik.faces['L'][0][1]), #BL standar_colors(rubik.faces['U'][0][2]) + standar_colors(rubik.faces['F'][2][2]) + standar_colors(rubik.faces['R'][2][2]), #UFR standar_colors(rubik.faces['U'][2][2]) + standar_colors(rubik.faces['R'][0][2]) + standar_colors(rubik.faces['B'][0][2]), #URB standar_colors(rubik.faces['U'][2][0]) + standar_colors(rubik.faces['B'][0][0]) + standar_colors(rubik.faces['L'][0][0]), #UBL standar_colors(rubik.faces['U'][0][0]) + standar_colors(rubik.faces['L'][2][0]) + standar_colors(rubik.faces['F'][2][0]), #ULF standar_colors(rubik.faces['D'][2][2]) + standar_colors(rubik.faces['R'][2][0]) + standar_colors(rubik.faces['F'][0][2]), #DRF standar_colors(rubik.faces['D'][2][0]) + standar_colors(rubik.faces['F'][0][0]) + standar_colors(rubik.faces['L'][2][2]), #DFL standar_colors(rubik.faces['D'][0][0]) + standar_colors(rubik.faces['L'][0][2]) + standar_colors(rubik.faces['B'][2][0]), #DLB standar_colors(rubik.faces['D'][0][2]) + standar_colors(rubik.faces['B'][2][2]) + standar_colors(rubik.faces['R'][0][0]) #DBR ] #print out #print " UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR" return out def solve(rubik): return subprocess.Popen(rubik_to_args(rubik, "bin/Solver.bin"), stdout=subprocess.PIPE).communicate()[0].split() #input_arg = "UF/ UR/ UB/ UL/ DF/ DR/ DB/ DL/ FR/ FL/ BR/ BL/ UFR/ URB/ UBL/ ULF/ DRF/ DFL/ DLB/ DBR"; if __name__ == '__main__': rubik = Rubik() rubik.scramble() rubik.describe() print solve(rubik)
if debug: pass #print tree return moves def phase_three_edges(rubik, debug): tree = init_phase(rubik) moves = bfs(rubik, tree, 'phase_three', validate_phase_three_edges) if debug: pass #print tree return moves def validate_phase_four(rubik): return rubik.is_solved() def phase_four(rubik, debug): tree = init_phase(rubik) moves = bfs(rubik, tree, 'phase_four', validate_phase_four) if debug: pass #print tree return moves if __name__ == '__main__': rubik = Rubik() rubik.scramble() moves = solve(rubik, debug=True) print moves
standar_colors(rubik.faces['F'][2][0]), #ULF standar_colors(rubik.faces['D'][2][2]) + standar_colors(rubik.faces['R'][2][0]) + standar_colors(rubik.faces['F'][0][2]), #DRF standar_colors(rubik.faces['D'][2][0]) + standar_colors(rubik.faces['F'][0][0]) + standar_colors(rubik.faces['L'][2][2]), #DFL standar_colors(rubik.faces['D'][0][0]) + standar_colors(rubik.faces['L'][0][2]) + standar_colors(rubik.faces['B'][2][0]), #DLB standar_colors(rubik.faces['D'][0][2]) + standar_colors(rubik.faces['B'][2][2]) + standar_colors(rubik.faces['R'][0][0]) #DBR ] #print out #print " UF UR UB UL DF DR DB DL FR FL BR BL UFR URB UBL ULF DRF DFL DLB DBR" return out def solve(rubik): return subprocess.Popen(rubik_to_args(rubik, "bin/Solver.bin"), stdout=subprocess.PIPE).communicate()[0].split() #input_arg = "UF/ UR/ UB/ UL/ DF/ DR/ DB/ DL/ FR/ FL/ BR/ BL/ UFR/ URB/ UBL/ ULF/ DRF/ DFL/ DLB/ DBR"; if __name__ == '__main__': rubik = Rubik() rubik.scramble() rubik.describe() print solve(rubik)
red_blocks = rubik.is_top_cross_done() relatively_correct = [False, False, False, False] for i in xrange(len(red_blocks)): opposite = get_opposite_edge_color(rubik, pieces[i]) relatively_correct[i] = red_blocks[i] and opposites[i] == opposite for i in xrange(len(relatively_correct)): if not relatively_correct[i]: face, x, y = find_edge(rubik, [color_U, opposites[i]]) def find_edge(rubik, colors): edge_coordinates = [(2,1),(1,2),(0,1),(1,0)] for face in rubik.faces.keys(): for i,j in edge_coordinates: if rubik.faces[face][i][j] == colors[0] and get_opposite_edge_color(rubik, face + str(i) + str(j)) == colors[1]: return face, i, j def get_opposite_edge_color(rubik, key): dictionary = { 'U01': rubik.faces['F'][2][1], 'U10': rubik.faces['L'][1][0], 'U12': rubik.faces['R'][1][2], 'U21': rubik.faces['B'][0][1], } return dictionary[key] if __name__ == '__main__': rubik = Rubik() first_cross(rubik) rubik.describe() #solve(rubik)
def act3(path): act = [] for i in range(0, len(path)): if i == len(path) - 1: return act m = path[i] n = path[i + 1] if (n[1] == m[13] and n[3] == m[15] and n[5] == m[1] and n[7] == m[3] and n[9] == m[5] and n[11] == m[7] and n[13] == m[9] and n[15] == m[11] and n[20] == m[22] and n[21] == m[20] and n[22] == m[23] and n[23] == m[21]): act.append('RC') if (n[1] == m[5] and n[3] == m[7] and n[5] == m[9] and n[7] == m[11] and n[9] == m[13] and n[11] == m[15] and n[13] == m[1] and n[14] == m[14] and n[15] == m[3] and n[20] == m[21] and n[21] == m[23] and n[22] == m[20] and n[23] == m[22]): act.append('R') if (n[0] == m[2] and n[1] == m[0] and n[2] == m[3] and n[3] == m[1] and n[4] == m[20] and n[5] == m[21] and n[14] == m[16] and n[15] == m[17] and n[16] == m[4] and n[17] == m[5] and n[20] == m[15] and n[21] == m[14] and n[22] == m[22] and n[23] == m[23]): act.append('T') if (n[0] == m[1] and n[1] == m[3] and n[2] == m[0] and n[3] == m[2] and n[4] == m[16] and n[5] == m[17] and n[14] == m[21] and n[15] == m[20] and n[16] == m[14] and n[17] == m[15] and n[20] == m[4] and n[21] == m[5] and n[22] == m[22] and n[23] == m[23]): act.append('TC') if (n[0] == m[2] and n[1] == m[0] and n[2] == m[19] and n[3] == m[17] and n[4] == m[6] and n[5] == m[4] and n[6] == m[7] and n[7] == m[5] and n[8] == m[22] and n[9] == m[20] and n[14] == m[16] and n[15] == m[17] and n[16] == m[4] and n[17] == m[8] and n[19] == m[9] and n[20] == m[2] and n[21] == m[14] and n[22] == m[3]): act.append('F') if (n[0] == m[2] and n[1] == m[0] and n[2] == m[20] and n[3] == m[22] and n[4] == m[5] and n[5] == m[7] and n[6] == m[4] and n[7] == m[6] and n[8] == m[17] and n[9] == m[19] and n[14] == m[16] and n[15] == m[17] and n[16] == m[4] and n[17] == m[3] and n[18] == m[18] and n[19] == m[2] and n[20] == m[9] and n[22] == m[8]): act.append('FC') r = Rubik([ 'y', 'b', 'y', 'b', 'g', 'y', 'g', 'y', 'w', 'g', 'w', 'g', 'b', 'w', 'b', 'w', 'r', 'r', 'r', 'r', 'o', 'o', 'o', 'o' ], [ 'b', 'b', 'b', 'b', 'y', 'y', 'y', 'y', 'g', 'g', 'g', 'g', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'o', 'o', 'o', 'o' ]) d = ITERATIVE_DFS(r) d.limitedDFS() p = [] for i in range(1, len(d.path) + 1): p.append(d.path[-i]) print('iterative dfs limited') print('path :', d.path) print('max memory', d.maxMemory) print('visited', d.visited) print('expand', d.expand) print('act', act3((p)))