Esempio n. 1
0
def main():
    # Create a initial state randomly
    square_size = 4

    init_state = PuzzleState(square_size=square_size)
    # dst = [1, 2, 3,
    #        8,-1, 6,
    #        7, 4, 5]
    dst = [1, 4, 5, 14, 2, 6, 13, 15, 11, 7, -1, 10, 8, 9, 12, 3]
    init_state.state = np.asarray(dst).reshape(square_size, square_size)

    move_list = generate_moves(100)  # 起始状态是通过在目标状态上随机执行一定步数的移动指令生成,当前设置为100步
    init_state.state = runs(init_state, move_list).state

    # Set a determined destination state
    dst_state = PuzzleState(square_size=square_size)
    dst_state.state = np.asarray(dst).reshape(square_size, square_size)

    # Find the path from 'init_state' to 'dst_state'
    move_list = astar_search_for_puzzle_problem(init_state, dst_state)

    move_list = convert_moves(move_list)

    # Perform your path
    if run_moves(init_state, dst_state, move_list):
        print_moves(init_state, move_list)
        print("Our dst state: ")
        dst_state.display()
        print("Get to dst state. Success !!!")
    else:
        print_moves(init_state, move_list)
        print("Our dst state: ")
        dst_state.display()
        print("Can not get to dst state. Failed !!!")
Esempio n. 2
0
 def run(self, grid_state: PuzzleState):
     for step in self.steps:
         if grid_state.puzzle.is_solved():
             return
         step.prepare(grid_state)
         if self.debug:
             time_start = time.time()
             check_for_next_step = step.check_for_next_step(grid_state)
             print("Running step " + str(step.__class__.__name__) +
                   ": Changes? " + str(check_for_next_step))
             if check_for_next_step:
                 step.do_next_step(grid_state)
                 grid_state.update()
                 grid_state.display()
                 print("Took " + str(time.time() - time_start) + " seconds")
         elif step.check_for_next_step(grid_state):
             step.do_next_step(grid_state)
def main():

    # Create a initial state randomly
    square_size = 5

    init_state = PuzzleState(square_size=square_size)
    #    dst = [1, 2, 3,
    #           8,-1, 6,
    #           7, 4, 5]
    #    dst = [1,  4, 5,  14,
    #           2,  6, 13, 15,
    #           11, 7, -1, 10,
    #           8,  9, 12, 3  ]
    dst = [
        1, 3, 4, 19, 21, 2, 13, 20, 24, 23, 5, 7, 12, 14, 18, 17, 16, 6, 8, 10,
        9, 11, -1, 15, 22
    ]
    init_state.state = np.asarray(dst).reshape(square_size, square_size)

    move_list = generate_moves(50)
    init_state.state = runs(init_state, move_list).state

    # Set a determined destination state
    dst_state = PuzzleState(square_size=square_size)
    dst_state.state = np.asarray(dst).reshape(square_size, square_size)

    # Find the path from 'init_state' to 'dst_state'
    move_list = astar_search_for_puzzle_problem(init_state, dst_state)

    move_list = convert_moves(move_list)

    # Perform your path
    if run_moves(init_state, dst_state, move_list):
        print_moves(init_state, move_list)
        print("Our dst state: ")
        dst_state.display()
        print("Get to dst state. Success !!!")
    else:
        print_moves(init_state, move_list)
        print("Our dst state: ")
        dst_state.display()
        print("Can not get to dst state. Failed !!!")