gpt4 book ai didi

python - 索引错误: list index out of range in a matrix

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:29 26 4
gpt4 key购买 nike

我一直在尝试创建一个程序生成的地下城,如 this article 中所示。 。但这对我来说有点难以理解这种算法的工作原理。所以,我一直在使用 this作为至少了解房间布局的指南。

文章中使用的程序是用Java编写的,所以我根据我的“现实”做了一些调整,并尝试在Python 3.5中模拟相同的结果。

我的代码如下:

from random import randint


class Room:

"""docstring for Room"""

def __init__(self, x, y, w, h):
"""[summary]

[description]

Arguments:
x {int} -- bottom-left horizontal anchorpoint of the room
y {int} -- bottom-left vertical anchor point of the room
w {int} -- width of the room
h {int} -- height of the room
"""
self.x1 = x
self.x2 = x + w
self.y1 = y
self.y2 = y + h
self.w = w
self.h = h
self.center = ((self.x1 + self.x2)/2, (self.y1 + self.y2)/2)

def intersects(self, room):
"""[summary]

Verifies if the rooms overlap

Arguments:
room {Room} -- a room object
"""
return(self.x1 <= room.x2 and self.x2 >= room.x1 and \
self.y1 <= room.y2 and self.y2 >= room.y1)

def __str__(self):
room_info = ("Coords: (" + str(self.x1) + ", " + str(self.y1) +
") | (" + str(self.x2) + ", " + str(self.y2) + ")\n")
room_info += ("Center: " + str(self.center) + "\n")
return(room_info)

MIN_ROOM_SIZE = 10
MAX_ROOM_SIZE = 20
MAP_WIDTH = 400
MAP_HEIGHT = 200
MAX_NUMBER_ROOMS = 20

dungeon_map = [[None] * MAP_WIDTH for i in range(MAP_HEIGHT)]
# print(dungeon_map)


def crave_room(room):
"""[summary]

"saves" a room in the dungeon map by making everything inside it's limits 1

Arguments:
room {Room} -- the room to crave in the dungeon map
"""
for x in xrange(min(room.x1, room.x2), max(room.x1, room.x2) + 1):
for y in xrange(min(room.y1, room.y2), max(room.y1, room.y2) + 1):
print(x, y) # debug
dungeon_map[x][y] = 1
print("Done") # dungeon


def place_rooms():

rooms = []

for i in xrange(0, MAX_NUMBER_ROOMS):
w = MIN_ROOM_SIZE + randint(0, MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1)
h = MIN_ROOM_SIZE + randint(0, MAX_ROOM_SIZE - MIN_ROOM_SIZE + 1)
x = randint(0, MAP_WIDTH - w) + 1
y = randint(0, MAP_HEIGHT - h) + 1

new_room = Room(x, y, w, h)
fail = False
for other_room in rooms:
if new_room.intersects(other_room):
fail = True
break
if not fail:
print(new_room)
crave_room(new_room) # WIP
new_center = new_room.center
# rooms.append(new_room)
if len(rooms) != 0:
prev_center = rooms[len(rooms) - 1].center

if(randint(0, 1) == 1):
h_corridor(prev_center[0], new_center[0], prev_center[1])
v_corridor(prev_center[1], new_center[1], prev_center[0])
else:
v_corridor(prev_center[1], new_center[1], prev_center[0])
h_corridor(prev_center[0], new_center[0], prev_center[1])
if not fail:
rooms.append(new_room)
for room in rooms:
print(room)


def h_corridor(x1, x2, y):
for x in xrange(min(x1, x2), max(x1, x2) + 1):
dungeon_map[x][y] = 1


def v_corridor(y1, y2, x):
for y in xrange(min(y1, y2), max(y1, y2) + 1):
dungeon_map[x][y] = 1

place_rooms()

但是每当我运行它时,我都会收到以下错误:

Traceback (most recent call last):
File "/home/user/dungeon.py", line 114, in <module>
place_rooms()
File "/home/user/dungeon.py", line 87, in place_rooms
crave_room(new_room)
File "/home/user/dungeon.py", line 65, in crave_room
dungeon_map[x][y] = 1
IndexError: list index out of range

根据我从代码中了解到的情况,crave_room 函数应该可以正常工作,因为我使用的是 minmax 函数。由于 h_corridorv_corridor 函数的工作方式类似,因此它们呈现出相同类型的问题。

我不确定问题是否发生,因为我使用矩阵代替原始文章中使用的 Canvas 。我怀疑是局部/全局变量问题,但我不认为这就是问题所在。我担心我犯了一个非常愚蠢的错误并且没有看到它。

任何关于更好的数据结构使用的代码改进技巧或建议都将受到欢迎,如果有人有关于该主题的更清晰/更简单的文章,最好是关于Python的文章,我在这里看到了很多相关的帖子,但是我还是有点迷失。

感谢您的帮助。 :D

最佳答案

您的 dungeon_map 声明不正确:

dungeon_map = [[None] * MAP_WIDTH] * MAP_HEIGHT

正确的做法应该是:

dungeon_map = [[None] * MAP_HEIGHT] * MAP_WIDTH

既然你已经做到了这一点,让我们来看看第二个更严重的问题。我们来做一个小比例(小 map )的实验:

MAP_WIDTH = 4
MAP_HEIGHT = 2
dungeon_map = [[None] * MAP_HEIGHT] * MAP_WIDTH

print('\nBefore Assignment:')
print(dungeon_map)

dungeon_map[2][1] = 'y'

print('\nAfter Assignment:')
print(dungeon_map)

在本实验中,我们创建了一个 4 列 x 2 行矩阵,并更改了一个单元格的值,所以让我们看一下输出:

Before Assignment:
[[None, None], [None, None], [None, None], [None, None]]

After Assignment:
[[None, 'y'], [None, 'y'], [None, 'y'], [None, 'y']]

这是怎么回事?本质上,您声明相同的列表 MAP_WIDTH 次。下面的声明行既方便又巧妙,但不正确:

dungeon_map = [[None] * MAP_HEIGHT] * MAP_WIDTH

声明这样一个矩阵的正确方法是:

dungeon_map = [[None for x in range(MAP_HEIGHT)] for y in range(MAP_WIDTH)]

关于python - 索引错误: list index out of range in a matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com