Workshop › Chess › Step 10 of 48
pseudoMoves(color) looks at every square, and for each knight of that color tries the 8 jumps. A jump that stays on
the board and does not land on its own piece is a move { from: [r, c], to: [tr, tc] }. An enemy piece there is a
capture.
```js
// Every move that follows the pieces' patterns, without checking whether it leaves the king in check.
function pseudoMoves(color) {
const moves = []
const add = (r, c, tr, tc) => moves.push({ from: [r, c], to: [tr, tc] })
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
const piece = board[r][c]
if (piece[0] !== color) continue
const kind = piece[1]
if (kind === 'N') {
for (const [dr, dc] of KNIGHT) {
const tr = r + dr
const tc = c + dc
if (inside(tr, tc) && board[tr][tc][0] !== color) add(r, c, tr, tc)
}
}
}
}
return moves
}
```moves starts empty; add pushes one move object onto it.continue skips to the next square when this one is not ours.for (const [dr, dc] of KNIGHT) takes each jump and unpacks its two numbers.board[tr][tc][0] !== color: an empty square's [0] is undefined, so empty squares and enemy pieces pass.return moves hands back the list.Under inside, leave an empty line and write the comment and pseudoMoves.
Skills: Arrays and objects, Loops
← The knight's jumps as data The king steps too →