()
| 168 | } |
| 169 | |
| 170 | render() { |
| 171 | const { grid, move, time, status } = this.state; |
| 172 | |
| 173 | //Render Grid |
| 174 | const newGrid = document.createElement('div'); |
| 175 | newGrid.className = 'grid'; |
| 176 | |
| 177 | for (let i = 0; i < 4; i++) { |
| 178 | for (let j = 0; j < 4; j++) { |
| 179 | const button = document.createElement('button'); |
| 180 | |
| 181 | if (status === 'playing') { |
| 182 | button.addEventListener('click', this.handleClickBox(new Box(j, i))); |
| 183 | } |
| 184 | |
| 185 | button.textContent = grid[i][j] === 0 ? '' : grid[i][j].toString(); |
| 186 | newGrid.appendChild(button); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | document.querySelector('.grid').replaceWith(newGrid); |
| 191 | |
| 192 | //render button |
| 193 | const newButton = document.createElement('button'); |
| 194 | if (status === 'ready') newButton.textContent = 'play'; |
| 195 | if (status === 'playing') newButton.textContent = 'reset'; |
| 196 | if (status === 'won') newButton.textContent = 'play'; |
| 197 | |
| 198 | newButton.addEventListener('click', () => { |
| 199 | clearInterval(this.tickId); |
| 200 | this.tickId = setInterval(this.tick, 1000); |
| 201 | this.setState(State.start()); |
| 202 | }); |
| 203 | |
| 204 | document.querySelector('.footer button').replaceWith(newButton); |
| 205 | |
| 206 | // Render move |
| 207 | document.getElementById('move').textContent = `Move: ${move}`; |
| 208 | |
| 209 | // Render Time |
| 210 | document.getElementById('time').textContent = `Time: ${time}`; |
| 211 | |
| 212 | // Render Message |
| 213 | if (status === 'won') { |
| 214 | document.querySelector('.message').textContent = 'You win!'; |
| 215 | } else { |
| 216 | document.querySelector('.message').textContent = ''; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | const GAME = Game.ready(); |
no test coverage detected