Workshop › 2048 › Step 19 of 26

Game over

What we are doing

A state variable says whether the game is 'playing' or 'over'. After a move, if no move is left, it is over, and move does nothing any more.

```js
let state // 'playing', 'won' or 'over'

  state = 'playing'

function move(direction) {
  if (state !== 'playing') return false

  addTile()
  if (!canMove()) state = 'over'
  return true
}
```

What it means

Your turn

  1. Under let score write let state; in newGame, under score = 0, write state = 'playing'.
  2. Write the guard as the first line of move.
  3. In move, under addTile(), write the canMove line.

Skills: Game states

All steps

  1. The board
  2. Sixteen cells
  3. A grid of numbers
  4. A color for every number
  5. Numbers on the tiles
  6. Big numbers, smaller text
  7. Find the empty cells
  8. A new tile
  9. Two tiles to start
  10. Slide a row
  11. Merge equal tiles
  12. Move left
  13. The arrow key
  14. Only real moves count
  15. Right is left, reversed
  16. Up and down are columns
  17. Score
  18. Is there a move left?
  19. Game over
  20. Making 2048
  21. Win and lose screens
  22. Space to go on
  23. Best score
  24. Swipes
  25. A tap is not a swipe
  26. Build it yourself: undo