List.append() 将所有元素更改为附加项

发布于 2024-10-21 17:38:28 字数 2564 浏览 2 评论 0原文

我用 Python 编写的迷宫生成程序似乎有问题。我正在尝试随机创建一条在选定点处分支的路径,并且随着路径的推移这些点会被存储。当迷宫到达死胡同时,它将通过测试顶部值而不是弹出该值并转到下一个值来对访问过的点进行排序,直到到达不是死胡同的位置。然而,当我尝试将项目附加到我用来保存我去过的空间的列表中时,发生了一些奇怪的事情,我以前从未见过它。这是代码,查看它的最佳方法是多次运行它,直到它完全通过。我还没有真正找到解决死胡同问题的方法,所以如果有人也能帮助我解决这个问题,那就太好了。

import random

width = 8

def check(x,y):
    """Figures out the directions that Gen can move while"""
    if x-1 == -1:
        maze[x][y][3] = 0 

    if x+1 == 8:
        maze[x][y][1] = 0

    if y+1 == 8:
        maze[x][y][2] = 0

    if y-1 == -1:
        maze[x][y][0] = 0

    if x + 1 in range(0,8) and visited[x+1][y] == False:
        maze[x][y][1] = 2

    if x - 1 in range(0,8) and visited[x-1][y] == False:
        maze[x][y][3] = 2

    if y + 1 in range(0,8) and visited[x][y+1] == False:
        maze[x][y][2] = 2

    if y - 1 in range(0,8) and visited[x][y-1] == False:
        maze[x][y][0] = 2



def Gen(x,y):
    visited[x][y] = True
    past.append(current)
    dirs = []
    check(x,y)
    print current

    if maze[x][y][0] == 2:
        dirs.append(0)
    if maze[x][y][1] == 2:
        dirs.append(1)
    if maze[x][y][2] == 2:
        dirs.append(2)
    if maze[x][y][3] == 2:
        dirs.append(3)

    pos = random.choice(dirs)

    print dirs

    maze[x][y][pos] = 1  

    if pos == 0:
        current[1] -= 1
    if pos == 1:
        current[0] += 1
    if pos == 2:
        current[1] += 1
    if pos == 3:
        current[0] -= 1

    if maze[x][y][0] == 4:
        maze[x][y][0] = 1

    if maze[x][y][1] == 4:
        maze[x][y][1] = 1

    if maze[x][y][2] == 4:
        maze[x][y][2] = 1

    if maze[x][y][3] == 4:
        maze[x][y][3] = 1

    print maze[x][y]
    print past, '\n'


#Build the initial values for the maze to be replaced later
maze = []
current = [0,0]
visited = []
past = []

#Generate empty 2d list with a value for each of the xy coordinates
for i in range(0,width):
    maze.append([])
    for q in range(0, width):
        maze[i].append([])
        for n in range(0, 4):
            maze[i][q].append(4)

#Makes a list of falses for all the non visited places
for x in range(0, width):
    visited.append([])
    for y in range(0, width):
        visited[x].append(False)

#Generates the walls
#for q in range(0, width):
#    for i in range(0, width):
#        check(q, i)

current = [0,0]

while current != [7,7]:
    Gen(current[0], current[1])
print maze

正如您所看到的,它从 0,0 开始,然后计算出可能采取的路径。它从其中随机选择并将房间那一侧的值设置为 0,0 到 1,这意味着一条通道。 2 表示有墙,0 表示出界。 4 只是一个占位符,因为在迷宫完全生成时所有值都应该被填充。

如果有人可以帮助我,那就太好了,非常感激。提前致谢。

I seem to have a problem with my maze generating program made in Python. I'm trying to randomly create a path that branches out at select points, with the points getting stored as it goes along. When the maze gets to a dead end, it will sort back through the visited points by testing the top value than popping that and going to the next one, until it gets to a spot where it isn't a dead end. However, when I try to append items to the list I'm using to save the spaces I've been to, something strange happens, I've never seen it before actually. Here's the code, and the best way to see it is to run it a through times until it goes all the way through. I haven't really found a way to counter the dead end problem, so if anyone could help me with that also, that would be great.

import random

width = 8

def check(x,y):
    """Figures out the directions that Gen can move while"""
    if x-1 == -1:
        maze[x][y][3] = 0 

    if x+1 == 8:
        maze[x][y][1] = 0

    if y+1 == 8:
        maze[x][y][2] = 0

    if y-1 == -1:
        maze[x][y][0] = 0

    if x + 1 in range(0,8) and visited[x+1][y] == False:
        maze[x][y][1] = 2

    if x - 1 in range(0,8) and visited[x-1][y] == False:
        maze[x][y][3] = 2

    if y + 1 in range(0,8) and visited[x][y+1] == False:
        maze[x][y][2] = 2

    if y - 1 in range(0,8) and visited[x][y-1] == False:
        maze[x][y][0] = 2



def Gen(x,y):
    visited[x][y] = True
    past.append(current)
    dirs = []
    check(x,y)
    print current

    if maze[x][y][0] == 2:
        dirs.append(0)
    if maze[x][y][1] == 2:
        dirs.append(1)
    if maze[x][y][2] == 2:
        dirs.append(2)
    if maze[x][y][3] == 2:
        dirs.append(3)

    pos = random.choice(dirs)

    print dirs

    maze[x][y][pos] = 1  

    if pos == 0:
        current[1] -= 1
    if pos == 1:
        current[0] += 1
    if pos == 2:
        current[1] += 1
    if pos == 3:
        current[0] -= 1

    if maze[x][y][0] == 4:
        maze[x][y][0] = 1

    if maze[x][y][1] == 4:
        maze[x][y][1] = 1

    if maze[x][y][2] == 4:
        maze[x][y][2] = 1

    if maze[x][y][3] == 4:
        maze[x][y][3] = 1

    print maze[x][y]
    print past, '\n'


#Build the initial values for the maze to be replaced later
maze = []
current = [0,0]
visited = []
past = []

#Generate empty 2d list with a value for each of the xy coordinates
for i in range(0,width):
    maze.append([])
    for q in range(0, width):
        maze[i].append([])
        for n in range(0, 4):
            maze[i][q].append(4)

#Makes a list of falses for all the non visited places
for x in range(0, width):
    visited.append([])
    for y in range(0, width):
        visited[x].append(False)

#Generates the walls
#for q in range(0, width):
#    for i in range(0, width):
#        check(q, i)

current = [0,0]

while current != [7,7]:
    Gen(current[0], current[1])
print maze

As you can see, it starts at 0,0 and then figures out the possible paths to take. It randomly selects from those and sets the value for that side of the room in 0,0 to 1, which means a passage. 2 means wall and 0 means out of bounds. 4 is just a placeholder as all values should be filled by the time the maze is completely generated.

If anyone could help me, that would be great and very appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-10-28 17:38:28

我相信当前列表只是被多次复制到过去列表中。因此,您有相同列表的多个副本。

修复方法:在 past.append(current) 行(def Gen(x,y): 下面两行)中,将其更改为 past.append(当前[:])

符号 list[:] 创建列表的副本。从技术上讲,您正在创建整个列表的一部分。

顺便说一句,更好的解决方案是不使用全局 current 变量:)

I believe the current list is simply copied multiple times into past. So you have multiple copies of the same list.

To fix: in the line past.append(current) (two lines below def Gen(x,y):), change it to past.append(current[:]).

The notation list[:] creates a copy of the list. Technically, you are creating a slice of the whole list.

By the way, a better solution would be to not use a global current variable :)

傲影 2024-10-28 17:38:28

在 Python 中使用列表推导式时,会出现此行为。您需要按条附加;否则,替换操作会在列表中执行多次。

This behavior is expected when using List Comprehension in Python. You need to append by strip; otherwise, the replace operation is executed multiple times in the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文