[英]Can you explain what the pseudo-code below is trying to do? and what type of code it may be?
[英]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.