我试图找到一种方法来增加一个值(房间)到列表(地牢)的下一个项目,这样我就可以在文本冒险中创建房间的随机实例。

def dungeon_generator(): #used to generate random dungeon order
    dungeon = random.sample(range(2, 19), 8)
    dungeon.append(20) #used to add Boss
    dungeon[:0]=[1] #used to add Entrance

伪码:
room = dungeon[0] #i.e. room == 1
while room != 0

    if room == 1:
        do things in room
        room finished
        room = NEXT number in dungeon list

    elif room == 2
        do things in room
        room finished
        room = NEXT number in dungeon list

etc

    elif room == 0
        game over

有人能推荐阅读下一个地下城列表的最佳方式吗?因为我使用了random.sample,所以如果这有帮助,列表项将是唯一的。

最佳答案

import random

def dungeon_generator(): #used to generate random dungeon order
    dungeon = random.sample(range(2, 19), 8)
    dungeon.append(20) #used to add Boss
    dungeon[:0]=[1] #used to add Entrance
    return dungeon


room = 0 #i.e. room == 1

dungeon = dungeon_generator();

while room < len(dungeon) and dungeon[room] != 0:
    if dungeon[room] == 1:
        # do something
        print (dungeon[room])
    elif dungeon[room] == 2:
        # do something
        print (dungeon[room])
    room = room + 1

10-06 14:55