-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwator_JR.py
242 lines (163 loc) · 7.27 KB
/
wator_JR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from tkinter import *
from random import randint, choice
import time
import os
class Monde:
def __init__(self, hauteur, largeur):
self.largeur = largeur
self.hauteur = hauteur
self.grille = [[ " " for _ in range(largeur)] for _ in range(hauteur)]
def afficher_monde(self):
for ligne in self.grille:
for case in ligne:
if isinstance(case,Poisson):
print("🐠", end = " | " )
elif isinstance (case,Requin):
print ( "🦈", end =" | ")
else:
print(" ",end=" | ")
print ("\n")
def peupler (self, nb_poisson, nb_requin):
max_poisson = 0
max_requin = 0
if nb_poisson + nb_requin > self.hauteur * self.largeur:
return ValueError("Merci de rentrer un nombre plus petit")
while max_poisson < nb_poisson:
x_rand = randint(0,self.largeur-1)
y_rand = randint(0,self.hauteur-1)
if self.grille [y_rand][x_rand]== " ":
self.grille[y_rand][x_rand] = Poisson (y_rand, x_rand)
max_poisson += 1
while max_requin < nb_requin:
x_rand = randint(0,self.largeur-1)
y_rand = randint(0,self.hauteur-1)
if self.grille [y_rand][x_rand]== " ":
self.grille[y_rand][x_rand] = Requin (y_rand, x_rand)
max_requin += 1
def jouer_un_tour(self):
self.poissons = []
self.requins = []
for ligne in self.grille:
for case in ligne:
if isinstance (case,Poisson):
self.poissons.append (case)
for poisson in self.poissons:
poisson.vivre_une_journee (self)
for ligne in self.grille:
for case in ligne:
if isinstance (case, Requin):
self.requins.append (case)
for requin in self.requins:
requin.vivre_une_journee(self)
self.afficher_monde()
class Poisson:
def __init__(self, y, x):
self.x = x
self.y = y
self.compteur_repro = 0
def deplacement_possible(self, monde):
coup_possibles = []
if monde.grille[(self.y-1)% monde.hauteur][self.x]== " " :
coup_possibles.append(((self.y-1)% monde.hauteur , self.x))
if monde.grille[(self.y+1)% monde.hauteur][self.x]== " ":
coup_possibles.append(((self.y+1)% monde.hauteur , self.x))
if monde.grille [self.y][(self.x+1) % monde.largeur]== " ":
coup_possibles.append((self.y , (self.x + 1) % monde.largeur))
if monde.grille [self.y][(self.x-1) % monde.largeur]==" ":
coup_possibles.append((self.y , (self.x-1) % monde.largeur))
return coup_possibles
def se_deplacer(self, monde):
coups_possibles=self.deplacement_possible(monde)
if len (coups_possibles)!=0:
if coups_possibles!=[]:
coup_a_jouer = choice (coups_possibles)
x_coup=coup_a_jouer[ 1 ]
y_coup=coup_a_jouer[ 0 ]
x_preced=self.x
y_preced=self.y
self.x = x_coup
self.y = y_coup
monde.grille[y_coup][x_coup]=self
if self.compteur_repro >= 5:
monde.grille[y_preced][x_preced]= Poisson (y_preced, x_preced)
self.compteur_repro=0
else:
monde.grille [y_preced][x_preced]= " "
def vivre_une_journee(self, monde):
self.compteur_repro +=1
self.se_deplacer (monde)
class Requin:
def __init__(self,y,x):
self.x = x
self.y = y
self.compteur_repro = 0
self.energie= 5
def deplacement_possible (self,monde):
deplacements=[]
if isinstance (monde.grille[(self.y+1) % monde.hauteur-1][self.x],Poisson):
deplacements.append(((self.y+1) % monde.hauteur, self.x))
return deplacements
elif isinstance (monde.grille[(self.y-1) % monde.hauteur-1][self.x], Poisson):
deplacements.append(((self.y-1) % monde.hauteur, self.x))
return deplacements
elif isinstance (monde.grille[self.y][(self.x+1) % monde.largeur],Poisson):
deplacements.append((self.y, (self.x+1) % monde.largeur))
return deplacements
elif isinstance (monde.grille[self.y][(self.x-1)% monde.largeur],Poisson):
deplacements.append((self.y, (self.x-1) % monde.largeur))
return deplacements
else:
if monde.grille [(self.y+1) % monde.hauteur][self.x] == " ":
deplacements.append (((self.y+1) % monde.hauteur, self.x))
if monde.grille [(self.y-1 ) % monde.hauteur][self.x] == " ":
deplacements.append(((self.y-1) % monde.hauteur, self.x ))
if monde.grille [self.y][(self.x+1) % monde.largeur] == " ":
deplacements.append((self.y,(self.x+1)% monde.largeur ))
if monde.grille [self.y][(self.x-1) % monde.largeur] == " ":
deplacements.append (( self.y , (self.x-1) % monde.largeur))
return deplacements
def se_deplacer (self, monde ,deplacements):
preced_y = self.y
preced_x = self.x
if self.energie ==0:
monde.grille [self.y][self.x] = " "
elif deplacements !=[]:
choix = choice(deplacements)
if isinstance(monde.grille[choix[0]][choix[1]], Poisson):
if self.compteur_repro >=4:
self.y = choix [0]
self.x = choix [1]
monde.grille[preced_y][preced_x] = Requin (preced_y , preced_x)
monde.grille [choix[0]][choix[1]] = self
self.compteur_repro = 0
else:
self.y = choix [0]
self.x = choix [1]
monde.grille [choix[0]] [choix[1]] = self
monde.grille [preced_y] [preced_x] = " "
self.energie +=1
elif self.compteur_repro >=10:
self.y = choix [0]
self.y = choix [1]
monde.grille [preced_y][preced_x] = Requin (preced_y, preced_x)
monde.grille [choix[0]][choix[1]] = self
self.compteur_repro=0
else:
self.y = choix [0]
self.x = choix [1]
monde.grille[choix[0]][choix[1]] = self
monde.grille[preced_y][preced_x] = " "
def vivre_une_journee (self,monde):
self.se_deplacer(monde ,self.deplacement_possible(monde))
self.energie -= 1
if self.energie==0:
monde.grille[self.y][self.x] = " "
elif self.energie > 10:
self.energie =10
self.compteur_repro +=1
monde=Monde(10, 8)
monde.peupler(10,10)
monde.afficher_monde()
for _ in range (50):
print("----------------------------------------------------------")
monde.jouer_un_tour()