Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // wymiary grida
  2. let columns = 60;
  3. let rows = 60;
  4.  
  5. // tablica która trzyma komórki
  6. const grid = new Array(columns);
  7. for(let i = 0; i < grid.length; i++) {
  8.     grid[i] = new Array(rows);
  9. }
  10.  
  11. // wirtualna tablica która przechowuje stan gry
  12. const grid2 = new Array(columns);
  13. for(let i = 0; i < grid2.length; i++) {
  14.     grid2[i] = new Array(rows);
  15. }
  16.  
  17. // domyślnie wszystkie komórki martwe
  18. for (let i = 0; i < rows; i++) {
  19.     for (let j = 0; j < columns; j++) {
  20.         grid2[i][j] = false;
  21.     }
  22. }
  23.  
  24. // rysowanie grida
  25. function drawGrid(){
  26.     let container = document.getElementById("container");
  27.     container.innerHTML = ""
  28.     for (let i = 0; i < rows; i++) {
  29.         for (let j = 0; j < columns; j++)    {
  30.             let cell = document.createElement("div");            
  31.             if(grid2[i][j]) {
  32.                 cell.classList.add("alive");
  33.             }
  34.             cell.addEventListener("click", () => {
  35.                 cell.classList.toggle("alive");                
  36.             });
  37.             grid[i][j] = cell;
  38.             container.append(cell);
  39.         }    
  40.     }
  41. }
  42.  
  43. // liczenie zywych sąsiadów
  44. function countLiveNeighbors(row, col) {
  45.     let count = 0;
  46.     for(let i = row - 1; i <= row + 1; i++) {
  47.         if (i >= 0 && i < rows){
  48.             for(let j = col - 1; j <= col + 1; j++) {
  49.                 if (j >= 0 && j < columns){
  50.                     if (i != row || j != col) {
  51.                         count += grid[i][j].classList.contains("alive") ? 1 : 0;                        
  52.                     }                    
  53.                 }                
  54.             }            
  55.         }            
  56.     }
  57.     return count;
  58. }
  59.  
  60.  
  61. let go = document.getElementById("go");
  62. let go2 = document.getElementById("go2");
  63.  
  64. // inicjalizacja pustego grida
  65. drawGrid();
  66.  
  67. // zyćko
  68. function life(){
  69.     for (let i = 0; i < rows; i++) {
  70.         for (let j = 0; j < columns; j++) {
  71.             if(grid[i][j].classList.contains("alive")) {
  72.                 grid2[i][j] = true;
  73.             }
  74.             if(countLiveNeighbors(i, j) == 3) {
  75.                 grid2[i][j] = true;
  76.             }  
  77.             if(countLiveNeighbors(i, j) != 2 && countLiveNeighbors(i, j) != 3) {
  78.                 grid2[i][j] = false;
  79.             }                      
  80.         }
  81.     }
  82.     drawGrid();
  83. }
  84. go.addEventListener("click", ()=>{
  85.     change();
  86.     go.classList.toggle('off');
  87. });
  88.  
  89. let alive;
  90.  
  91. function change() {
  92.     if (!alive) {
  93.         alive = window.setInterval(life,100);
  94.     } else {
  95.         window.clearInterval(alive);
  96.         alive = null;
  97.     }
  98. }
  99.  
  100. go2.addEventListener("click", ()=>{
  101.     for (let i = 0; i < rows; i++) {
  102.         for (let j = 0; j < columns; j++) {
  103.             grid2[i][j] = Math.random() > 0.5 ? true : false;
  104.         }
  105.     }
  106.     drawGrid();
  107. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement