Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- def nowa_plansza(H, W, pola=None):
- L = numpy.zeros([H,W], dtype = float)
- for i,j in pola:
- L[i][j] = 1
- return L
- def sasiedzi(plansza, i ,j):
- suma = 0
- if((i-1 >= 0)and(j-1 >= 0)):
- if(plansza[i-1][j-1] > 0):
- suma += 1
- if(j-1 >= 0):
- if(plansza[i][j-1] > 0):
- suma += 1
- if((i+1 < plansza.shape[0])and(j-1 >= 0)):
- if(plansza[i+1][j-1] > 0):
- suma += 1
- if(i-1 >= 0):
- if(plansza[i-1][j] > 0):
- suma += 1
- if((i-1 >= 0)and(j+1 < plansza.shape[1])):
- if(plansza[i-1][j+1] > 0):
- suma += 1
- if(j+1 < plansza.shape[1]):
- if(plansza[i][j+1] > 0):
- suma += 1
- if((i+1 < plansza.shape[0])and(j+1 < plansza.shape[1])):
- if(plansza[i+1][j+1] > 0):
- suma += 1
- if(i+1 < plansza.shape[0]):
- if(plansza[i+1][j] > 0):
- suma += 1
- return suma
- def krok(plansza):
- plansza2 = plansza.copy()
- for i in range(plansza.shape[0]):
- for j in range(plansza.shape[1]):
- if(plansza[i][j] == 0):
- if(sasiedzi(plansza,i,j) == 3):
- plansza2[i][j] = 1
- else:
- if((sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3)):
- plansza2[i][j] =0
- elif(sasiedzi(plansza,i,j)==2)or(sasiedzi(plansza,i,j)==3):
- plansza2[i][j] = plansza[i][j]/2
- return plansza2
- def symulacja(plansza, n):
- L = [plansza]
- for i in range(n):
- M = krok(L[i])
- L.append(M)
- return L
- plansza = nowa_plansza(5,5,[(1,1),(2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,2)])
- print(symulacja(plansza,2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement