为什么 is_wall_r 和 is_wall_f 函数不能正确知道那里是否有墙?

问题描述 投票:0回答:0

我正在尝试使用 maze_dict 解决迷宫问题。 Maze_dict 是一个嵌套字典,它包含迷宫中每个位置的 y、x 值,以及在主要方向上是否有墙。我用来解决迷宫的方法是右墙追随者方法,计数器应该在右边保持一堵墙并向前移动。然而,似乎 is_wall_f 认为前面总是有一堵墙,即使没有。


maze = [[2, 1, 2, 2, 2],
 [2, 1, 1, 1, 2],
 [2, 1, 2, 2, 2],
 [2, 1, 1, 1, 2],
 [2, 2, 2, 1, 2]]

maze_dict = {(1, 1): {'N': 2, 'E': 1, 'S': 2, 'W': 2}, (1, 2): {'N': 2, 'E': 2, 'S': 1, 'W': 2}, (1, 3): {'N': 2, 'E': 2, 'S': 1, 'W': 1}, (1, 4): {'N': 2, 'E': 2, 'S': 1, 'W': 2}, (1, 5): {'N': 2, 'E': 2, 'S': 2, 'W': 2}, (2, 1): {'N': 2, 'E': 1, 'S': 2, 'W': 2}, (2, 2): {'N': 1, 'E': 1, 'S': 1, 'W': 2}, (2, 3): {'N': 2, 'E': 1, 'S': 2, 'W': 1}, (2, 4): {'N': 2, 'E': 2, 'S': 2, 'W': 1}, (2, 5): {'N': 2, 'E': 2, 'S': 2, 'W': 1}, (3, 1): {'N': 2, 'E': 1, 'S': 2, 'W': 2}, (3, 2): {'N': 1, 'E': 2, 'S': 1, 'W': 2}, (3, 3): {'N': 1, 'E': 2, 'S': 1, 'W': 1}, (3, 4): {'N': 1, 'E': 2, 'S': 1, 'W': 2}, (3, 5): {'N': 2, 'E': 2, 'S': 2, 'W': 2}, (4, 1): {'N': 2, 'E': 1, 'S': 2, 'W': 2}, (4, 2): {'N': 1, 'E': 1, 'S': 2, 'W': 2}, (4, 3): {'N': 2, 'E': 1, 'S': 2, 'W': 1}, (4, 4): {'N': 2, 'E': 2, 'S': 1, 'W': 1}, (4, 5): {'N': 2, 'E': 2, 'S': 2, 'W': 1}, (5, 1): {'N': 2, 'E': 2, 'S': 2, 'W': 2}, (5, 2): {'N': 1, 'E': 2, 'S': 2, 'W': 2}, (5, 3): {'N': 1, 'E': 1, 'S': 2, 'W': 2}, (5, 4): {'N': 1, 'E': 2, 'S': 2, 'W': 2}, (5, 5): {'N': 2, 'E': 2, 'S': 2, 'W': 1}}

m_height = 5
m_width = 5

EMPTY_TILE = 1
FILLED_TILE = 2

def solve_rightwall(maze, maze_dict):

    bottom_row = m_height - 1

    start_x = 2
    start_y = 1
    start_direction = 'S'
    
    print(start_y, start_x)

    current_x = start_x
    current_y = start_y
    current_pos = (current_y, current_x)
    current_direction = start_direction

    for colm in range(m_width):
        if maze[bottom_row][colm] == EMPTY_TILE:
            goal_pos = (bottom_row + 1, colm + 1)

    print(current_pos, goal_pos)

    while current_pos != goal_pos:

        if current_direction == 'N':
            right_direction = 'E'
        if current_direction == 'E':
            right_direction = 'S'
        if current_direction == 'S':
            right_direction = 'W'
        if current_direction == 'W':
            right_direction = 'N'

        if is_wall_r(maze_dict, current_pos, right_direction):
            print("Wall R",end="::")
            if is_wall_f(maze_dict, current_pos, current_direction):
              current_direction = turn_left(current_direction)
              print("Wall F",end="::")
        else:
            current_direction = turn_right(current_direction)
            print("No wall turning right",end="::")
                
    print("Solved")
    
    return True

def is_wall_r(maze_dict, current_pos, right_direction):

    temp_N = maze_dict[current_pos]['N']
    temp_E = maze_dict[current_pos]['E']
    temp_S = maze_dict[current_pos]['S']
    temp_W = maze_dict[current_pos]['W']

    if right_direction == 'N':
        if temp_N == FILLED_TILE:
            return True

    if right_direction == 'E':
        if temp_E == FILLED_TILE:
            return True

    if right_direction == 'S':
        if temp_S == FILLED_TILE:
            return True

    if right_direction == 'W':
        if temp_W == FILLED_TILE:
            return True
    else:
        return False

def is_wall_f(maze_dict, current_pos, current_direction):

    temp_N = maze_dict[current_pos]['N']
    temp_E = maze_dict[current_pos]['E']
    temp_W = maze_dict[current_pos]['S']
    temp_S = maze_dict[current_pos]['W']

    if current_direction == 'N':
        if temp_N == FILLED_TILE:
            return True
    if current_direction == 'E':
        if temp_E == FILLED_TILE:
            return True
    if current_direction == 'S':
        if temp_S == FILLED_TILE:
            return True
    if current_direction == 'W':
        if temp_W == FILLED_TILE:
            return True
    else:
        return False


def turn_right(current_direction):

    if current_direction == 'N':
        current_direction = 'E'
        return current_direction
    if current_direction == 'E':
        current_direction = 'S'
        return current_direction
    if current_direction == 'S':
        current_direction = 'W'
        return current_direction
    if current_direction == 'W':
        current_direction = 'N'
        return current_direction
    
def turn_left(current_direction):
    
    if current_direction == 'N':
        current_direction = 'W'
        return current_direction
    if current_direction == 'E':
        current_direction = 'N'
        return current_direction
    if current_direction == 'S':
        current_direction = 'E'
        return current_direction
    if current_direction == 'W':
        current_direction = 'S'
        return current_direction
        
    
def move_counter(current_x, current_y, current_direction):

    if current_direction == 'N':
        current_y -= 1
        current_pos = (current_y, current_x)
        pass
    if current_direction == 'E':
        current_x += 1
        current_pos = (current_y, current_x)
        pass
    if current_direction == 'S':
        current_y += 1
        current_pos = (current_y, current_x)
        pass
    if current_direction == 'W':
        current_x -= 1
        current_pos = (current_y, current_x)
        pass

    return (current_pos)

solve_rightwall(maze, maze_dict)

我不确定我做错了什么,因为检查 maze_dict 是一个简单的检查,或者我是这么认为的。任何帮助将不胜感激。

python maze
© www.soinside.com 2019 - 2024. All rights reserved.