Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为基于文本的RPG创建更一致、随机生成的世界空间_Python_Arrays_Python 2.7_Oop - Fatal编程技术网

Python 为基于文本的RPG创建更一致、随机生成的世界空间

Python 为基于文本的RPG创建更一致、随机生成的世界空间,python,arrays,python-2.7,oop,Python,Arrays,Python 2.7,Oop,目前,我可以在二维数组中创建一个随机世界。不过,我觉得太随意了。以下是我目前正在使用的类: 来自随机导入randint、choice、randrange class WorldSpace(object): def __init__(self, row, col, world_array): self.row = row # Number of lists to be created. self.col = col # Number of indexes

目前,我可以在二维数组中创建一个随机世界。不过,我觉得太随意了。以下是我目前正在使用的类:

来自随机导入randint、choice、randrange

class WorldSpace(object):
    def __init__(self, row, col, world_array):
        self.row = row  # Number of lists to be created.
        self.col = col  # Number of indexes inside of each row.
        self.world_array = world_array
创建世界的
WorldSpace
方法:

@classmethod
def generate(cls, autogen):
    print 'Starting world generation...'
    print
    if autogen is True:
        row_amt = 75
        col_amt = 75

    else:
        row_amt = input('Please specify row length of world array.: ')
        col_amt = input('Please specify column length of world array.: ')
        if (row_amt or col_amt) == 0:
            print 'Invalid world values!'
            cls.generateWorld(False)

    world_array = [[' ']*col_amt for _ in xrange(row_amt)]
    print 'Created world...'

    return cls(row_amt, col_amt, world_array)
一种改变世界的方法——目前只创建
森林
,尽管在我完整的代码段中,也形成了一系列海洋和山脉:

def modify_world(self, autogen):
    if autogen is True:
        # Forests:
        count = randint(6, 10)
        while count > 0:
            a = randint(1, (self.row / randint(2, 6)))
            b = randint(1, (self.col / randint(2, 6)))
            row_val = randint(5, self.row)
            count_val = randint(5, 15)
            self.genLine_WObj(a, b, row_val, 't', count_val)
            count -=1

    print('\n'.join([''.join(['{:1}'.format(item) for item in row])
      for row in self.world_array]))
    inp = input('')
    if inp != '':
        return
以及实际创建
平铺的方法:

def genLine_WObj(self, a, b, row_val, char, count):
    # 'genLine_WObj' - Generate Line(like) world object.
    # Used to place lines of certain characters with psuedo-randomized
    # width and length onto the world array.
    while count != 0:
        row_col_dict = {row_val: (a, b)}
        for row in row_col_dict.keys():
            startPos, endPos = row_col_dict[row]

            for i in range(startPos, endPos):
                self.world_array[row][i] = char

        b += choice([0, 1])
        a += choice([0, 0, 0, 0, 1])

        row_val -= 1
        count -= 1
现在要实际运行该程序:

world = WorldSpace.generate(True)
world.modify_world(True)
然而,虽然工作时间约为20-30%,但有时它会生成小森林,或小对
t
字符,而此时它应该在地图周围创建森林。如何改进代码,使随机生成更加一致

固定:

  • 有时您的
    a
    b
    大,并且不会生成林
  • 你们所有的森林都在地图的左边
  • 为更改的行添加内联注释

    def modify_world(self, autogen):
        if autogen is True:
            # Forests:
            count = randint(6, 10)
            while count > 0:
                a = randrange(self.col)           # begin of forest
                width = self.col / randint(2, 6)  # initial width of forest
                b = int(a + width)                # end of forest
                row_val = randint(5, self.row)
                count_val = randint(5, 15)
                self.genLine_WObj(a, b, row_val, 't', count_val)
                count -=1
    
        print('\n'.join([''.join(['{:1}'.format(item) for item in row])
          for row in self.world_array]))
        inp = input('')
        if inp != '':
            return
    
    
    def genLine_WObj(self, a, b, row_val, char, count):
        # 'genLine_WObj' - Generate Line(like) world object.
        # Used to place lines of certain characters with psuedo-randomized
        # width and length onto the world array.
        while count != 0:
            row_col_dict = {row_val: (a, b)}
            for row in row_col_dict.keys():
                startPos, endPos = row_col_dict[row]
    
                for i in range(startPos, min(self.col, endPos)):  # added min
                    self.world_array[row][i] = char
    
            b += choice([0, 1])
            a += choice([0, 0, 0, 0, 1])
    
            row_val -= 1
            count -= 1
    
    我会把森林的宽度改成与地图大小无关的。例如:

    width = randint(2, 15)
    

    但这取决于你的目标。

    你还没有解释目标。有些东西不可能既随机又一致——这在术语上是矛盾的。“小双”是什么意思?代码也有问题:例如,查看genLine的while循环中的前三行-创建一个单元素字典并对其进行“迭代”(循环只运行一次),然后使用键查找刚刚显式分配给它的值,并将其分配给两个新变量。这些代码实际上都没有做任何事情。我建议您将代码发布到CodeReview,并获得帮助来清理它。