about summary refs log tree commit diff stats
path: root/js/life.js
blob: e28e1f33d302708c323aef1141e19a16ce5fae19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp 
function countNeighbors(grid, x, y) {
  // Define the offsets for the neighboring cells
  const neighborOffsets = [
      [-1, -1], [-1, 0], [-1, 1],
      [0, -1],           [0, 1],
      [1, -1],  [1, 0],  [1, 1]
  ];

  // Use reduce to iterate over the neighborOffsets and count the number of live neighbors
  return neighborOffsets.reduce((count, [dx, dy]) => {
      // Calculate the coordinates of the neighboring cell
      const nx = x + dx;
      const ny = y + dy;

      // Check if the neighboring cell is within the grid boundaries and if it is alive
      if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) {
          // If the neighboring cell is alive, increment the count
          return count + 1;
      }

      // If the neighboring cell is not alive or is outside the grid boundaries, return the current count
      return count;
  }, 0);
}

function step(grid) {
  // Use map to iterate over each cell in the grid and calculate the next state based on the rules of the game
  return grid.map((row, x) =>
      row.map((cell, y) => {
          // Count the number of live neighbors for the current cell
          const neighbors = countNeighbors(grid, x, y);

          // Apply the rules of the game to determine the next state of the cell
          // If the cell has 3 live neighbors or has 2 live neighbors and is already alive, it stays alive
          // Otherwise, it becomes dead
          return neighbors === 3 || (neighbors === 2 && cell) ? 1 : 0;
      })
  );
}

function printGrid(grid) {
  grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').join('')));
  console.log('\n');
}

function simulate(initial, steps) {
  let grid = initial;
  Array.from({ length: steps }).forEach(() => {
      printGrid(grid);
      grid = step(grid);
  });
}


const initialBoard = [
  [0, 1, 0],
  [0, 0, 1],
  [1, 1, 1],
  [0, 0, 0],
];

simulate(initialBoard, 10);

const rpentomino = [
  [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];

// simulate(rpentomino, 10);

// big glider
const bigGlider = [
  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];

// simulate(bigGlider, 10);

const randomBoard = Array.from({ length: 22 }, () =>
  Array.from({ length: 22 }, () => Math.round(Math.random()))
);

simulate(randomBoard, 50);