Workshop › Chess › Step 40 of 48
Show the last move
What we are doing
Keep the last move in lastMove and light up its two squares like the selected one, so you always see what just
happened.
```js
let lastMove
lastMove = null
makeMove(m)
lastMove = m
const highlight = same(selected, [r, c]) || (lastMove && (same(lastMove.from, [r, c]) || same(lastMove.to, [r, c])))
if (highlight) {
```
What it means
lastMove is null at the start and set by play (not by makeMove, which is also used for trying moves).
- A square is highlighted if it is selected, or if there is a last move and the square is its start or its target.
Your turn
- Above
let state write let lastMove; in reset, above state = 'playing' write lastMove = null.
- In
play, under makeMove(m) write lastMove = m.
- In
draw, replace if (same(selected, [r, c])) { with the two new lines.
Skills: Game states, Drawing on a canvas
← Click to play again A score for a position →
All steps
- A dark table
- One row of squares
- All 64 squares
- The starting position as text
- Color and kind
- Draw the pieces
- An outline for white
- Draw again and again
- The knight's jumps as data
- A list of moves
- The king steps too
- Which square was clicked?
- Light up the selected square
- Whose turn is it?
- Where can it go?
- Show the targets
- Play a move
- Click a dot to move
- Say whose turn it is
- The rook slides
- Bishops and queens
- Pawns step forward
- Pawns capture diagonally
- A pawn becomes a queen
- Is a square attacked?
- Attacks from far away
- Is the king in check?
- A move that remembers
- Take a move back
- Only legal moves
- Castling moves
- The rook jumps over
- Losing the right to castle
- No castling through check
- Remember the double step
- En passant
- Checkmate and stalemate
- Check! and the result
- Click to play again
- Show the last move
- A score for a position
- Thinking ahead
- Pick the best move
- Alpha-beta: skip what cannot matter
- Try the best-looking moves first
- The computer plays black
- You are white
- Build it yourself: the fifty-move rule