Workshop › 2048 › Step 14 of 26
A move only counts if it changed something; otherwise a player could fill the board by pressing a useless direction. We compare the board before and after, and add a tile only when they differ.
```js
function move(direction) {
const before = JSON.stringify(board)
if (JSON.stringify(board) === before) return false
addTile()
return true
}
```JSON.stringify(board) turns the whole board into one text, like '[[2,4,0,0],...]', which can be compared with
===.move returns false before adding a tile; otherwise it adds one and returns true.move, write the before line.addTile(), and return true under it.Skills: Game states
← The arrow key Right is left, reversed →