简体   繁体   English

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

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

I've been given a computing assignment to create randomly generate game levels using pseudo-code and algorithms. 我已经获得了一个计算任务,可以使用伪代码和算法创建随机生成的游戏关卡。 The map must be printed using #'s and spaces and cells with fewer than 3 neighbors die. 必须使用#来打印地图,空格和少于3个邻居的像元将死亡。 But we have been told to work it out without covering either and it's safe to say I have no idea what I'm doing. 但是我们被告知要解决这个问题而不涉及任何一个,可以肯定地说我不知道​​自己在做什么。 Any help would be appreciated... 任何帮助,将不胜感激...

Here's the whole code: 这是整个代码:

# -*- 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)

Pseudo-code I have been given 我得到的伪代码

 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

Formula example I have been given 我得到的公式示例

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


You might first work out a viable structure for map. 您可能首先会设计出可行的地图结构。 map needs to be a two dimensional structure. 地图必须是二维结构。 One way of doing that is to make map a list-of-lists ie a list of rows where each rows elements are column of that row. 一种实现方法是使map成为列表列表,即行列表,其中每个行元素都是该行的列。 See Create a two-dimensional array at runtime on the Rosetta Code site. 请参阅Rosetta Code网站上的在运行时创建二维数组

Next populate an array and try and turn your draw_map pseudo-code into proper Python around your implementation of a 2D array. 接下来,填充一个数组,然后尝试将您的draw_map伪代码转换为围绕2D数组实现的适当Python。

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

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