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.arange(H*W).reshape((H,W)) * 0
- 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] == 1):
- suma += 1
- if(j-1 >= 0):
- if(plansza[i][j-1] == 1):
- suma += 1
- if((i+1 < plansza.shape[0])and(j-1 >= 0)):
- if(plansza[i+1][j-1] == 1):
- suma += 1
- if(i-1 >= 0):
- if(plansza[i-1][j] == 1):
- suma += 1
- if((i-1 >= 0)and(j+1 < plansza.shape[1])):
- if(plansza[i-1][j+1] == 1):
- suma += 1
- if(j+1 < plansza.shape[1]):
- if(plansza[i][j+1] == 1):
- suma += 1
- if((i+1 < plansza.shape[0])and(j+1 < plansza.shape[1])):
- if(plansza[i+1][j+1] == 1):
- suma += 1
- if(i+1 < plansza.shape[0]):
- if(plansza[i+1][j] == 1):
- suma += 1
- return suma
- def krok(plansza):
- for i in range(plansza.shape[0]):
- for j in range(plansza.shape[1]):
- if(plansza[i][j] == 1):
- if(sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3):
- plansza[i][j] = 0
- elif(plansza[i][j] == 0):
- if(sasiedzi(plansza,i,j) == 3):
- plansza[i][j] = 1
- return plansza
- def symulacja(plansza, n):
- L = []
- L.append(plansza)
- for i in range(n+1):
- M = krok(L[i])
- L.append(M)
- return L
- szybowiec = nowa_plansza(5, 5, [(1,1), (2,1), (3,1), (3,2), (3,3),(2,3), (1,3), (1,2)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement