|
| 1 | +<div id="rules"> |
| 2 | + <p> |
| 3 | + The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John |
| 4 | + Horton Conway in 1970.[1] It is a zero-player game,[2][3] meaning that its evolution is determined by its initial |
| 5 | + state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and |
| 6 | + observing how it evolves. It is Turing complete and can simulate a universal constructor or any other Turing |
| 7 | + machine. |
| 8 | + </p> |
| 9 | + <p> |
| 10 | + Rules: |
| 11 | + <ol> |
| 12 | + <li>Any live cell with two or three live neighbours survives.</li> |
| 13 | + <li>Any dead cell with three live neighbours becomes a live cell.</li> |
| 14 | + <li>All other live cells die in the next generation. Similarly, all other dead cells stay dead.</li> |
| 15 | + </ol> |
| 16 | + </p> |
| 17 | +</div> |
| 18 | +<div id="settings"> |
| 19 | + <label for="size">Grid size: </label> |
| 20 | + <input type="text" id="size" /> |
| 21 | + <button id="start">Start</button> |
| 22 | +</div> |
| 23 | +<div id="game" style="display: none"> |
| 24 | + <div style="display: flex; gap: 10px; margin-bottom: 15px"> |
| 25 | + <div style="display: none"> |
| 26 | + <label for="previous-grid">Previous state</label> |
| 27 | + <fieldset id="previous-grid" disabled style="border: solid 1px black"></fieldset> |
| 28 | + </div> |
| 29 | + <div> |
| 30 | + <label for="grid">Current state</label> |
| 31 | + <fieldset id="grid" style="border: solid 1px black"></fieldset> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + <button id="next">Next generation</button> |
| 35 | + <button id="auto">Auto</button> |
| 36 | +</div> |
| 37 | + |
| 38 | +<script> |
| 39 | + const gameOfLife = (size) => { |
| 40 | + const state = [...Array(size)].map(() => [...Array(size)].map(() => Math.floor(Math.random() * 2))); |
| 41 | + |
| 42 | + function draw(container) { |
| 43 | + // First empty the container |
| 44 | + while (container.firstChild) { |
| 45 | + container.removeChild(container.firstChild); |
| 46 | + } |
| 47 | + const { state } = this; |
| 48 | + |
| 49 | + for (let i = 0; i < state.length; i++) { |
| 50 | + const row = state[i]; |
| 51 | + const rowDiv = document.createElement('div'); |
| 52 | + for (let j = 0; j < row.length; j++) { |
| 53 | + const cell = row[j]; |
| 54 | + const checkbox = document.createElement('input'); |
| 55 | + checkbox.type = 'checkbox'; |
| 56 | + checkbox.checked = !!cell; |
| 57 | + checkbox.addEventListener('change', () => { |
| 58 | + state[i][j] = checkbox.checked ? 1 : 0; |
| 59 | + }); |
| 60 | + rowDiv.appendChild(checkbox); |
| 61 | + } |
| 62 | + container.appendChild(rowDiv); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + function next() { |
| 67 | + const nextState = [...Array(size)].map(() => [...Array(size)]); |
| 68 | + const { state } = this; |
| 69 | + |
| 70 | + for (let i = 0; i < state.length; i++) { |
| 71 | + const prevRow = state[i - 1] ?? []; |
| 72 | + const row = state[i]; |
| 73 | + const nextRow = state[i + 1] ?? []; |
| 74 | + for (let j = 0; j < row.length; j++) { |
| 75 | + const liveNeighbors = |
| 76 | + (prevRow[j - 1] ?? 0) + |
| 77 | + (prevRow[j] ?? 0) + |
| 78 | + (prevRow[j + 1] ?? 0) + |
| 79 | + (row[j - 1] ?? 0) + |
| 80 | + (row[j + 1] ?? 0) + |
| 81 | + (nextRow[j - 1] ?? 0) + |
| 82 | + (nextRow[j] ?? 0) + |
| 83 | + (nextRow[j + 1] ?? 0); |
| 84 | + // Cell keeps living if it has 2 or 3 neighbors alive. |
| 85 | + // Dead cell becomes alive with 3 live neighbors. |
| 86 | + nextState[i][j] = (liveNeighbors === 2 && row[j]) || liveNeighbors === 3 ? 1 : 0; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + state: nextState, |
| 92 | + draw, |
| 93 | + next, |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + state, |
| 99 | + draw, |
| 100 | + next, |
| 101 | + }; |
| 102 | + }; |
| 103 | + |
| 104 | + const startBtn = document.querySelector('#start'); |
| 105 | + startBtn.addEventListener('click', () => { |
| 106 | + const sizeInput = document.querySelector('#size'); |
| 107 | + const gridSize = Number(sizeInput.value); |
| 108 | + if (gridSize === 0 || isNaN(gridSize)) { |
| 109 | + alert("Please enter a number for 'Grid size'"); |
| 110 | + return; |
| 111 | + } |
| 112 | + const settingsDiv = document.querySelector('#settings'); |
| 113 | + settingsDiv.remove(); |
| 114 | + |
| 115 | + const gameDiv = document.querySelector('#game'); |
| 116 | + gameDiv.style.display = 'block'; |
| 117 | + |
| 118 | + const previousGrid = document.querySelector('#previous-grid'); |
| 119 | + const currentGrid = document.querySelector('#grid'); |
| 120 | + let game = gameOfLife(gridSize); |
| 121 | + |
| 122 | + function cycleGame() { |
| 123 | + game.draw(previousGrid); |
| 124 | + game = game.next(); |
| 125 | + game.draw(currentGrid); |
| 126 | + } |
| 127 | + |
| 128 | + cycleGame(); |
| 129 | + |
| 130 | + const nextBtn = document.querySelector('#next'); |
| 131 | + nextBtn.addEventListener('click', () => { |
| 132 | + previousGrid.parentNode.style.display = 'block'; |
| 133 | + cycleGame(); |
| 134 | + }); |
| 135 | + |
| 136 | + const autoBtn = document.querySelector('#auto'); |
| 137 | + let running = false; |
| 138 | + let interval; |
| 139 | + autoBtn.addEventListener('click', () => { |
| 140 | + if (running) { |
| 141 | + currentGrid.disabled = false; |
| 142 | + nextBtn.disabled = false; |
| 143 | + autoBtn.textContent = 'Auto'; |
| 144 | + clearInterval(interval); |
| 145 | + running = false; |
| 146 | + } else { |
| 147 | + currentGrid.disabled = true; |
| 148 | + nextBtn.disabled = true; |
| 149 | + autoBtn.textContent = 'Stop'; |
| 150 | + cycleGame(); |
| 151 | + interval = setInterval(() => { |
| 152 | + cycleGame(); |
| 153 | + }, 2000); |
| 154 | + running = true; |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | +</script> |
0 commit comments