基于随机布尔值在python中创建四叉树

基于随机布尔值在python中创建四叉树,python,python-3.x,algorithm,recursion,binary-tree,Python,Python 3.x,Algorithm,Recursion,Binary Tree,我使用一个随机种子来生成布尔值,这些布尔值决定一个节点的四个子节点是否存在。对于上下文,这用于为我正在处理的游戏中随机生成的房间生成值。 要执行此操作的类位于此处: class Room: def __init__(self, roomID): # the initial room @ c/r with seed roomIDs [c][r] random.seed(roomID) # the value of the room is equal to the see

我使用一个随机种子来生成布尔值,这些布尔值决定一个节点的四个子节点是否存在。对于上下文,这用于为我正在处理的游戏中随机生成的房间生成值。 要执行此操作的类位于此处:

class Room:
def __init__(self, roomID):

    # the initial room @ c/r with seed roomIDs [c][r]
    random.seed(roomID)

    # the value of the room is equal to the seed that generated it
    self.id = roomID

    # use the seed to determine if it has rooms attached
    self.north = bool(random.getrandbits(1))
    self.south = bool(random.getrandbits(1))
    self.east = bool(random.getrandbits(1))
    self.west = bool(random.getrandbits(1))

def insert(self, roomIds, c, r):
    # Compare the new value with the parent node
    if self.north == True:
        self.north.insert = Room(roomIds[c][r + 1])
    if self.south == True:
        self.south.insert = Room(roomIds[c][r - 1])
    if self.east == True:
        self.east.insert = Room(roomIds[c + 1][r])
    if self.west == True:
        self.west.insert = Room(roomIds[c - 1][r])
RoomID等于该房间对应的RowID中的行/列 例如,如果我们有一个2x2数组

12
2.5

RoomID[0][0]将是1,这是该房间第0列第0行的RoomID

我生成地图的功能如下

def generateMap(Head, RoomIds, c, r, size):
if size <= 0:
    return

if Head.north == True:
    try:
        Head.north = Room(roomID=RoomIds[c + 1][r])
    except:
        pass
    tmp = Head.north
    return generateMap(tmp, RoomIds, c + 1, r, size - 1)
if Head.south == True:
    try:
        Head.north = Room(roomID=RoomIds[c - 1][r])
    except:
        pass
    tmp = Head.north
    return generateMap(tmp, RoomIds, c - 1, r, size - 1)
if Head.east == True:
    try:
        Head.north = Room(roomID=RoomIds[c][r + 1])
    except:
        pass
    tmp = Head.north
    return generateMap(tmp, RoomIds, c, r + 1, size - 1)
if Head.west == True:
    try:
        Head.north = Room(roomID=RoomIds[c][r - 1])
    except:
        pass
    tmp = Head.north
    return generateMap(tmp, RoomIds, c, r - 1, size - 1)
其中size是数组的总大小。而5,5只是一个任意的起始空间。 这个实现在某种程度上起作用。它生成正确数量的房间,但RoomIDs在两个值之间循环。以下是一些示例输出:

Room: @ 9 is 28664435753962367559
Room: @ 8 is 21687111371302414893
Room: @ 7 is 28664435753962367559
Room: @ 6 is 21687111371302414893
Room: @ 5 is 28664435753962367559
Room: @ 4 is 21687111371302414893
Room: @ 3 is 28664435753962367559
Room: @ 2 is 21687111371302414893
Room: @ 1 is 28664435753962367559
Room: @ 0 is 61454798153344042283
我想知道在我的逻辑中发生了什么,每个房间的种子总是一样的。提前感谢您的帮助

Room: @ 9 is 28664435753962367559
Room: @ 8 is 21687111371302414893
Room: @ 7 is 28664435753962367559
Room: @ 6 is 21687111371302414893
Room: @ 5 is 28664435753962367559
Room: @ 4 is 21687111371302414893
Room: @ 3 is 28664435753962367559
Room: @ 2 is 21687111371302414893
Room: @ 1 is 28664435753962367559
Room: @ 0 is 61454798153344042283