Home>

I am creating a simulation of a forest fire using a two-dimensional array by programming. The language I'm using is python. I am using visual studio.

Place trees on the ground on the N × N square. A tree is growing with a probability of p (state: 1). No tree is growing with probability 1-p (state: 0) Example: map1 [x] [y] = 1 → Feeling that a tree is growing.
Let 2 be the state of the burning tree and 3 be the state of the extinguished tree.
Set the initial value of map1 [x] [y] to 0 or 1, set some trees to 2 in the first step, and update the steps until the fire is completely extinguished. When (x, y) is 1, (x, y) becomes 2 if any one of the adjacent squares is in the state of 2, and becomes 3 in the next step. 2 spreads to only 1 square in 1 step.

Two or more squares of fire will spread in one step. As my policy, I created the code for each of the four corners, four sides, and the middle. I tried rewriting the fire spread step several times, but it still didn't work.

Can you tell me what's wrong?
Thank you.

Corresponding source code
``````import random
import itertools
random.seed (50)
p = 0.7 # Probability of growing trees
N = 5 # Forest size
haji = [0, -1]
tyousei = [1, -1]
map1 = []
for x in range (N):
map1.append ( * N)
for y in range (N):
if random.random ()<= p:
map1 [x] [y] = 1
map2 = []
for _ in range (N):
map2.append ( * N)
while True:
a = random.randint (0, N-1)
b = random.randint (0, N-1)
if map1 [a] [b] == 1:
map1 [a] [b] = 2
break
print (* map1, sep = "\ n")
all_list = itertools.chain (* map1)
all_list = list (all_list)
while 2 in all_list:
for x in haji: # corner
if x == 0: # leftmost corner
for y, z in zip (haji, tyousei):
if map1 [y] [x] == 1:
right = map1 [y] [x + 1]
updown = map1 [y + z] [x]
if right == 2 or updown == 2:
map2 [y] [x] = 2
else: else:
map2 [y] [x] = 1
elif map1 [y] [x] == 2:
map2 [y] [x] = 3
else: else:
map2 [y] [x] = map1 [y] [x]
else: # Rightmost corner
for y, z in zip (haji, tyousei):
if map1 [y] [x] == 1:
left = map1 [y] [x-1]
updown = map1 [y + z] [x]
if left == 2 or updown == 2:
map2 [y] [x] = 2
else: else:
map2 [y] [x] = 1
elif map1 [y] [x] == 2:
map2 [y] [x] = 3
else: else:
map2 [y] [x] = map1 [y] [x]
for x, z in zip (haji, tyousei):
for y in range (1, N-1): # Top and bottom edges
if map1 [x] [y] == 1:
left = map1 [x] [y-1]
right = map1 [x] [y + 1]
updown = map1 [x + z] [y]
if left == 2 or right == 2 or updown == 2:
map2 [x] [y] = 2
else: else:
map2 [x] [y] = 1
elif map1 [x] [y] == 2:
map2 [x] [y] = 3
else: else:
map2 [x] [y] = map1 [x] [y]
for y, z in zip (haji, tyousei):
for x in range (1, N-1): # Left and right edges
if map1 [x] [y] == 1:
up = map1 [x-1] [y]
down = map1 [x + 1] [y]
side = map1 [x] [y + z]
if up == 2 or down == 2 or side == 2:
map2 [x] [y] = 2
else: else:
map2 [x] [y] = 1
elif map1 [x] [y] == 2:
map2 [x] [y] = 3
else: else:
map2 [x] [y] = map1 [x] [y]
for x in range (1, N-1): #other than edge
for y in range (1, N-1):
if map1 [x] [y] == 1:
up = map1 [x-1] [y]
down = map1 [x + 1] [y]
left = map1 [x] [y-1]
right = map1 [x] [y + 1]
if up == 2 or down == 2 or left == 2 or right == 2:
map2 [x] [y] = 2 # Fire spread
else: else:
map2 [x] [y] = 1 #No change
elif map1 [x] [y] == 2:
map2 [x] [y] = 3 # Fire extinguishing
else: else:
map2 [x] [y] = map1 [x] [y] #No change
map1 = map2
all_list = itertools.chain (* map1)
all_list = list (all_list)
print ("============")
print (* map1, sep = "\ n")``````
What I tried

The value of the random number is fixed.

``import copy``
``````--map1 = map2