Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. n = input('Lattice size = ');
  2.  
  3. %Initial state
  4. s = zeros(n + 2, n + 2);
  5. m = floor((n + 2) / 2);
  6. s(m, m) = 1;
  7.  
  8. u = 255 * s(2 : n + 1, 2 : n + 1);
  9. figure(1), image(u), colormap(summer)
  10.  
  11. %Iterasi sebanyak 100 kali
  12. for iter = 1 : 200
  13. %Update state dengan aturan Game of Life
  14. st = s;
  15. for i = 2 : n + 1
  16. for j = 2 : n + 1
  17. t_hidup = st(i - 1, j - 1) + st(i - 1, j) + st(i - 1, j + 1);
  18. t_hidup = t_hidup + st(i , j - 1) + st(i, j) + st(i, j + 1);
  19. t_hidup = t_hidup + st(i + 1, j - 1) + st(i + 1, j) + st(i + 1, j + 1);
  20.  
  21. if(st(i, j) == 0)
  22. if((t_hidup == 1) || t_hidup == 3)
  23. s(i, j) = 1;
  24. end
  25. else
  26. if((t_hidup < 2) || t_hidup > 6)
  27. s(i, j) = 0;
  28. end
  29. end
  30. end
  31. end
  32.  
  33. if (mod(iter, 10) == 0)
  34. u = 255 * s(2 : n + 1, 2 : n + 1);
  35. figure(1), image(u), colormap(summer)
  36. drawnow;
  37. end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement