wildfire/genForet.py
LordOf20th c763e17923 Update
Affichage !
Arrêt  auto de feuDeForet quand plus de flammes
2019-02-12 22:32:03 +01:00

111 lines
No EOL
4.2 KiB
Python

# -*- coding: utf-8 -*-
"""
Created on Fri Feb 1 11:30:02 2019
@author: LordOf20th
"""
import numpy as np
import random
import tkinter as tk
from PIL import Image
"""
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
def genForet(n,p):
if n>=2:
foret=np.full(shape=(n,n), fill_value=0)
for i in range(0,foret.shape[0]):
for j in range(0,foret.shape[1]):
if random.random() <= p:
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
if x==-1:
x=random.randint(0, foret.shape[0]-1)
if y==-1:
y=random.randint(0, foret.shape[1]-1)
foretEnflammée = np.copy(foret)
foretEnflammée[x,y]=-1
return foretEnflammée
def propager(F,k,i,j):
try:
if F[k-1][i-1,j] == 1: #Si la case au dessus est un arbre
F[k][i-1,j] = -1 #La case au dessus brûle
except IndexError:
pass
try:
if F[k-1][i+1,j] == 1: #Si la case au dessous est un arbre
F[k][i+1,j] = -1 #La case en dessous brûle
except IndexError:
pass
try:
if F[k-1][i,j-1] == 1: #Si la case à gauche est un arbre
F[k][i,j-1] = -1 #La case à gauche brûle
except IndexError:
pass
try:
if F[k-1][i,j+1] ==1: #Si la case à droite est un arbre
F[k][i,j+1] = -1 #La case à droite brûle
except IndexError:
pass
def feuDeForet(F):
k=2
while -1 in F[k-1]:
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-1][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))
k = k+1
def wildfire(n, p, x=-1, y=-1):
F=[genForet(n,p)] #Génération de la forêt selon les paramètres voulus
print(F[0], "Instant initial")
F.append(enflammer(F[0],x,y)) #La forêt est enflammée puis stockée
print(F[1], "Rang : 1 (Départ de feu)")
feuDeForet(F)
return F
def affichage(F):
for k in range(0,len(F)):
fenetre = tk.Tk() #Crée la fenêtre que l'on va modifier
label = tk.Label(fenetre, text="Wildfire") #Met en place le titre
label.pack()
canvas = tk.Canvas(fenetre, width=(F[k].shape[0]+2)*10, height=(F[k].shape[1]+2)*10,background='#c0c0c0') #On définit le canevas qui affiche notre forêt
canvas.create_rectangle(10,10,(F[k].shape[0]+1)*10,(F[0].shape[1]+1)*10,fill='#c68c53')
for i in range(0,F[k].shape[0]):
for j in range(0,F[k].shape[1]):
if F[k][i,j]== 1:
canvas.create_rectangle((i+1)*10,(j+1)*10,(i+2)*10,(j+2)*10,fill='green',width=0)
elif F[k][i,j]==-1:
canvas.create_rectangle((i+1)*10,(j+1)*10,(i+2)*10,(j+2)*10,fill='red',width=0)
elif F[k][i,j]==-2:
canvas.create_rectangle((i+1)*10,(j+1)*10,(i+2)*10,(j+2)*10,fill='black',width=0)
canvas.pack()
bouton = tk.Button(fenetre, text="Suivant", command=fenetre.destroy )
bouton.pack()
fenetre.mainloop() #affiche la fenêtre
#Fonctions utiles à l'affichage
def pause():
input("Appuyer sur Entrée pour continuer . . .")