繁体   English   中英

如何转换此伪代码以及公式需要什么列表

[英]How do I convert this pseudo-code and what list do I need for the formula

我已经获得了一个计算任务,可以使用伪代码和算法创建随机生成的游戏关卡。 必须使用#来打印地图,空格和少于3个邻居的像元将死亡。 但是我们被告知要解决这个问题而不涉及任何一个,可以肯定地说我不知道​​自己在做什么。 任何帮助,将不胜感激...

这是整个代码:

# -*- coding: utf-8 -*-
import random

def create_random_map(height, width):
  map = []
  for y in range(height):
    for x in range(width):
        map.append( bool(random.randint(0,1)) )

  return map

def apply_cellular_automaton(map, height, width, born, survive):
   return map

def draw_map(map, height, width):
  pass

height = 45
width = 79

map = create_random_map(height, width)

for i in range(5):
  map = apply_cellular_automaton(map, height, width, [6, 7, 8], [3, 4, 5, 6, 7, 8])

for i in range(3):
  map = apply_cellular_automaton(map, height, width, [5, 6, 7, 8], [5, 6, 7, 8])


draw_map(map, height, width)

我得到的伪代码

 PROCEDURE draw_map(map, height, width)
    FOR EACH grid row
            CREATE AN EMPTY row
            FOR EACH cell in row
                    IF cell IS alive
                            ADD ‘#’ TO row
                    ELSE
                            ADD ‘ ‘ TO row
            PRINT row

我得到的公式示例

i = (y * width) + x
61 = (7 * 8) + 5


您可能首先会设计出可行的地图结构。 地图必须是二维结构。 一种实现方法是使map成为列表列表,即行列表,其中每个行元素都是该行的列。 请参阅Rosetta Code网站上的在运行时创建二维数组

接下来,填充一个数组,然后尝试将您的draw_map伪代码转换为围绕2D数组实现的适当Python。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM