Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import numpy
  2.  
  3. def nowa_plansza(H, W, pola=None):
  4. L = numpy.zeros([H,W], dtype = float)
  5. for i,j in pola:
  6. L[i][j] = 1
  7. return L
  8.  
  9.  
  10. def sasiedzi(plansza, i ,j):
  11. suma = 0
  12. if((i-1 >= 0)and(j-1 >= 0)):
  13. if(plansza[i-1][j-1] > 0):
  14. suma += 1
  15.  
  16. if(j-1 >= 0):
  17. if(plansza[i][j-1] > 0):
  18. suma += 1
  19.  
  20. if((i+1 < plansza.shape[0])and(j-1 >= 0)):
  21. if(plansza[i+1][j-1] > 0):
  22. suma += 1
  23.  
  24. if(i-1 >= 0):
  25. if(plansza[i-1][j] > 0):
  26. suma += 1
  27.  
  28. if((i-1 >= 0)and(j+1 < plansza.shape[1])):
  29. if(plansza[i-1][j+1] > 0):
  30. suma += 1
  31.  
  32. if(j+1 < plansza.shape[1]):
  33. if(plansza[i][j+1] > 0):
  34. suma += 1
  35.  
  36. if((i+1 < plansza.shape[0])and(j+1 < plansza.shape[1])):
  37. if(plansza[i+1][j+1] > 0):
  38. suma += 1
  39.  
  40. if(i+1 < plansza.shape[0]):
  41. if(plansza[i+1][j] > 0):
  42. suma += 1
  43. return suma
  44.  
  45. def krok(plansza):
  46. plansza2 = plansza.copy()
  47. for i in range(plansza.shape[0]):
  48. for j in range(plansza.shape[1]):
  49. if(plansza[i][j] == 0):
  50. if(sasiedzi(plansza,i,j) == 3):
  51. plansza2[i][j] = 1
  52. else:
  53. if((sasiedzi(plansza,i,j)<2)or(sasiedzi(plansza,i,j)>3)):
  54. plansza2[i][j] =0
  55. elif(sasiedzi(plansza,i,j)==2)or(sasiedzi(plansza,i,j)==3):
  56. plansza2[i][j] = plansza[i][j]/2
  57.  
  58. return plansza2
  59.  
  60. def symulacja(plansza, n):
  61. L = [plansza]
  62. for i in range(n):
  63. M = krok(L[i])
  64. L.append(M)
  65.  
  66. return L
  67.  
  68. plansza = nowa_plansza(5,5,[(1,1),(2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,2)])
  69.  
  70. print(symulacja(plansza,2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement