【发布时间】:2021-12-04 07:31:50
【问题描述】:
我已经编写了一个程序(见下文)作为 python 中的元胞自动机的实现。当我运行该程序时,我会收到一个随机种子噪声模式和已通过该程序运行的新模式。这 ”。”是零,“#”是一。问题是,当我运行程序时,我收到了随机噪声(看起来不错),但是当我收到平滑版本时,我只得到句点(零)。
app.py
import perlin
line = ""
noise = perlin.Noise(20,20, 420, 1)
print(" ")
for i in range(20):
line += "="
print(line)
# Parses smoothed map
line = ""
print(" ")
for i in range(len(noise.map)):
for j in range(len(noise.map[i])):
if noise.map[i][j] == 0:
line += "."
else:
line += "#"
print(line)
line = ""
perlin.py
import numpy as np
class Noise:
def inbounds(self,x,y):
if (x >= 0 and x <= self.width) and (y >=0 and y <= self.height):
return True
else:
return False
def smooth(self,iterations):
for i in range(iterations):
temp_map = self.map
for j in range(self.height):
for k in range(self.width):
neighbor_wall_count = 0
for y in range(j-1,j+1):
for x in range(k-1, k+1):
if self.inbounds(x,y):
if y != j or x != k:
if temp_map[y][x] == 1:
neighbor_wall_count += 1
else:
neighbor_wall_count += 1
if neighbor_wall_count > 3:
self.map[j][k] = 1
else:
self.map[j][k] = 0
def __init__(self, width, height, seed, iter):
# Sets all of the self variables
self.width = width
self.height = height
self.seed = seed
np.random.seed(self.seed)
w, h = self.width, self.height
# Declares the pattern map list
map = [[0 for i in range(w)] for j in range(h)]
#Generator
for y in range(h):
for x in range(w):
map[y][x] = np.random.randint(0,2)
self.map = map
# Parser
line = ""
for i in range(len(self.map)):
for j in range(len(self.map[i])):
if self.map[i][j] == 0:
line += "."
else:
line += "#"
print(line)
line = ""
self.smooth(iter)
输出
#..###.#....#...#..#
..#####..##.#.###..#
###.###.###..####...
###....##..###.#.#..
#.#.###..##...##..##
#......#..#..#.##..#
.###.#.#.##..#...##.
##...##....#####..#.
#..##...#....###..#.
#.....#.##.####...##
#..#..#.#.##..##..#.
.#.###.###...#..#..#
...#.##....#..#.##..
#.#..#.#.#.###..####
.#..#....#..##.###..
#..#.#.#........###.
.#####..#.#..####...
##..#.#.##.##..#..##
##.#.#.##.####.#..##
####.........##..##.
====================
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
【问题讨论】:
-
哦,天哪...它是如此简单,但我花了这么多时间。 python 范围已排除。
for y in range(j-1,j+2)(x相同)。您还必须更正inbounds(x < self.width与y相同) -
我做了更改,但不是全零,而是全零。
-
你必须调整
neighbor_wall_count -
天哪。绝对的传奇。它太小了,我看不见。救生员,非常感谢!
-
你能把这个作为答案发给我吗?
标签: python random noise perlin-noise cellular-automata