Update genForet.py
This commit is contained in:
parent
7de14e92b5
commit
eb15bf56a4
1 changed files with 65 additions and 15 deletions
80
genForet.py
80
genForet.py
|
|
@ -7,29 +7,79 @@ Created on Fri Feb 1 11:30:02 2019
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
#import tkinter as tk
|
#import tkinter as tk
|
||||||
|
"""
|
||||||
|
Les états possibles de nos cases :
|
||||||
|
- 0 : pas d'arbre
|
||||||
|
- 1 : un arbre sain
|
||||||
|
- -1 : un arbre en feu
|
||||||
|
- -2 : un arbre mort
|
||||||
|
"""
|
||||||
#Fonctions agissant sur la matrice
|
#Fonctions agissant sur la matrice
|
||||||
def genForet(n,p):
|
def genForet(n,p):
|
||||||
foret=np.full(shape=(n,n), fill_value=0)
|
if n>=2:
|
||||||
for i in range(0,foret.shape[0]):
|
foret=np.full(shape=(n,n), fill_value=0)
|
||||||
for j in range(0,foret.shape[1]):
|
for i in range(0,foret.shape[0]):
|
||||||
if random.random() <= p:
|
for j in range(0,foret.shape[1]):
|
||||||
foret[i,j]=1
|
if random.random() <= p:
|
||||||
return foret
|
foret[i,j]=1
|
||||||
|
return foret
|
||||||
|
else:
|
||||||
|
raise ValueError("n doit prendre une valeur de 2 au moins")
|
||||||
|
|
||||||
def enflammer(foret,x,y): #Fonction mettant le feu aléatoirement à une case
|
def enflammer(foret,x,y): #Fonction mettant le feu aléatoirement à une case
|
||||||
if x==-1:
|
if x==-1:
|
||||||
x=random.randint(0, foret.shape[0]-1)
|
x=random.randint(0, foret.shape[0]-1)
|
||||||
if y==-1:
|
if y==-1:
|
||||||
y=random.randint(0, foret.shape[1]-1)
|
y=random.randint(0, foret.shape[1]-1)
|
||||||
foret[x,y]=-1
|
foretEnflammée = np.copy(foret)
|
||||||
return foret
|
foretEnflammée[x,y]=-1
|
||||||
|
return foretEnflammée
|
||||||
def propagation(n, p=0.5, x=-1, y=-1):
|
|
||||||
F=[]
|
def propager(F,k,i,j):
|
||||||
foret = genForet(n, p)
|
if not i == 0 and not i == F[k].shape[0]-1 and not j == 0 and not j == F[k].shape[1]-1: #Si la case n'est pas sur la bordure
|
||||||
F.append(foret)
|
print("La case {};{} va bruler les quatre autour d'elle".format(i,j))
|
||||||
F.append(enflammer(foret,x,y))
|
if F[k][i-1,j] == 1: #Si la case au dessus est un arbre
|
||||||
|
F[k][i-1,j] = -1 #La case au dessus brûle
|
||||||
|
if F[k][i+1,j] == 1: #Si la case au dessous est un arbre
|
||||||
|
F[k][i+1,j] = -1 #La case en dessous brûle
|
||||||
|
if F[k][i,j-1] == 1: #Si la case à gauche est un arbre
|
||||||
|
F[k][i,j-1] = -1 #La case à gauche brûle
|
||||||
|
if F[k][i,j+1] ==1: #Si la case à droite est un arbre
|
||||||
|
F[k][i,j+1] = -1 #La case à droite brûle
|
||||||
|
|
||||||
|
if i == 0:#La case est au bord supérieur
|
||||||
|
if F[k][i+1,j] == 1: #Si la case au dessous est un arbre
|
||||||
|
F[k][i+1,j] = -1 #La case en dessous brûle
|
||||||
|
if i == F[k].shape[0]-1: #La case est au bord inférieur
|
||||||
|
if F[k][i-1,j] == 1: #Si la case au dessus est un arbre
|
||||||
|
F[k][i-1,j] = -1 #La case au dessus brûle
|
||||||
|
if j == 0: #La case est au bord gauche
|
||||||
|
if F[k][i,j+1] ==1: #Si la case à droite est un arbre
|
||||||
|
F[k][i,j+1] = -1 #La case à droite brûle
|
||||||
|
if j == F[k].shape[1]-1: #La case est au bord droit
|
||||||
|
if F[k][i,j-1] == 1: #Si la case à gauche est un arbre
|
||||||
|
F[k][i,j-1] = -1 #La case à gauche brûle
|
||||||
|
|
||||||
|
def feuDeForet(n, tours, p=0.5, x=-1, y=-1):
|
||||||
|
F=[genForet(n, p)]
|
||||||
|
print(F[0], "Instant initial")
|
||||||
|
F.append(enflammer(F[0],x,y)) #On stocke les deux premiers état de notre forêt
|
||||||
|
print(F[1], "Rang : 1 (Départ de feu)")
|
||||||
|
for k in range(2,tours):
|
||||||
|
F.append(np.copy(F[k-1])) #On copie la forêt du tour précédent
|
||||||
|
for i in range(0,F[k].shape[0]):
|
||||||
|
for j in range(0,F[k].shape[1]):
|
||||||
|
|
||||||
|
if F[k][i,j]==-1:#Si la case est en feu au rang k
|
||||||
|
if F[k-1][i,j]==0: #Si cette case ne portait pas d'arbre au rang k-1 alors on propage le feu et la case s'éteint
|
||||||
|
propager(F,k,i,j)
|
||||||
|
F[k][i,j]=0
|
||||||
|
elif F[k-2][i,j]==-1: #Si la case était en feu au tour précédent : propage et mort de l'arbre
|
||||||
|
propager(F,k,i,j)
|
||||||
|
F[k][i,j]=-2
|
||||||
|
else: #La case a été mise en feu, elle propage le feu
|
||||||
|
propager(F,k,i,j)
|
||||||
|
print(F[k], "Rang : {}".format(k))
|
||||||
return F #Return temporaire
|
return F #Return temporaire
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue