Exemplo 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 !!!")
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 !!!")
import numpy

from puzzle_state import PuzzleState

row = 1
col = 0
# lit = curr_state[:row]+[curr_state[row][:col]+curr_state[row][col+1:col+2]+curr_state[row][col:col+1]+curr_state[row][col+2:]]+curr_state[row+1:]
# print(curr_state)
# print(lit)
curr_state = PuzzleState()
curr_state.state = numpy.ones(shape=(4, 4))

s1 = PuzzleState()
s1.state = numpy.zeros(shape=(4, 4))
s1.state = curr_state.state[:row - 1] + [
    curr_state.state[row - 1][:col] + curr_state.state[row][col:col + 1] +
    curr_state.state[row - 1][col + 1:]
] + [
    curr_state.state[row][:col] + curr_state.state[row - 1][col:col + 1] +
    curr_state.state[row][col + 1:]
] + curr_state.state[row + 1:]
print(s1.state)