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.
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 ([0] * N)
for y in range (N):
if random.random ()<= p:
map1 [x] [y] = 1
map2 = []
for _ in range (N):
map2.append ([0] * 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.
-
Answer # 1
Related articles
- about processing to exclude the character string group specified from list in python
- python - about hamiltonian neural networks
- python - about write loop to csv
- about python argument and data definition
- python 3x - about downloading anaconda
- python - about the optimum angle of rotation matrix
- python - about downloading youtube videos by youtube-dl
- i have a question about basic python problems
- python - about "" "of" "" select === = "" "
- about batch change of file name using python
- about the python speedtest code
- about the implementation of combinations in python
- please tell me about the role of python tag = "mychr"
- about python def issues
- about the operation of python's speedtest module (library)
- python - what i don't understand about yolo9000 (v2)
- python - about x-axis adjustment in matplotlib
- about data plotting in python
- python 3x - about return code of python3 subprocess
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Add