Workshop › Chess › Step 11 of 48
The king steps too
What we are doing
A king steps one square in any of 8 directions: another fixed list. The same loop now handles both pieces; only the
list changes.
```js
const KING = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
if (kind === 'N' || kind === 'K') {
for (const [dr, dc] of kind === 'N' ? KNIGHT : KING) {
```
What it means
KING lists the 8 neighbouring squares.
|| means "or": knights and kings both enter the block.
kind === 'N' ? KNIGHT : KING picks the list for this piece.
Your turn
- Under
KNIGHT write KING.
- In
pseudoMoves, change the if (kind === 'N') line and the for line under it.
Skills: Arrays and objects
← A list of moves Which square was clicked? →
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