元胞自动机 returns 全 0 [PYTHON]

Cellular Automata returns all 0s [PYTHON]

我在 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 ranges 中的停止参数被排除在范围之外 (range < stop)

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

您可以通过更改该测试的值来调整平滑度(例如此处为 3):

if neighbor_wall_count > 3: