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
| from ezCLI import *
from random import randrange
# ------------------------------------------------------------------------------
class Noyau(object):
# ----------------------------------------------------------------------------
def __init__(self, height, width, nb_colors):
self.height = height
self.width = width
self.nb_colors = nb_colors
self.mat = [[0 for j in range (self.width)] for i in range (self.height)]
for i in range (self.height):
for j in range (self.width) :
self.mat[i][j] = randrange(1,nb_colors+1)
print (grid(self.mat))
while self.mat[self.height-1][self.width-1] != 0 :
x = input("x = ")
y = input("y= ")
x= int(x)
y=int(y)
c= self.mat[x][y]
self.voisin(x,y,c,True)
print(grid(self.mat))
self.gravitelignes()
self.gravitecolonnes()
print (grid(self.mat))
def voisin(self,x,y,c, first):
self.x = x
self.y = y
self.c = c
if self.mat[x][y] == c and c != 0 :
if not first: self.mat[x][y] = 0
if x >0 : self.voisin (x-1,y,c,False)
if x < (self.height - 1 ) : self.voisin (x+1,y,c, False)
if y > 0 : self.voisin (x,y-1,c,False)
if y < (self.width - 1) : self.voisin (x,y+1,c,False)
#print(grid(self.mat))
def gravitelignes (self) :
for j in range (self.width) :
for i in range (self.height-1,-1,-1) :
if self.mat[i][j] == 0 :
for k in range(i-1, -1, -1):
if self.mat[k][j] == 0: continue
self.mat[i][j] = self.mat[k][j]
self.mat[k][j] = 0
#print (i, j, k, grid(self.mat))
break
def gravitecolonnes (self):
for j in range (self.width) :
if self.mat[self.height-1][j] == 0 :
print (j)
for k in range(j+1,self.width-2) :
if self.mat[self.height-1][k] == 0 :continue
else :
for i in range (self.height) :
self.mat[i][j] = self.mat[i][k]
self.mat[i][k] = 0
print (i, j, k, grid(self.mat))
if __name__ == "__main__":
n=Noyau(6,6,3) |
Partager