Workshop › Chess › Step 20 of 48
A rook does not jump: it slides any distance in one of four directions until something stops it. So for each
direction we keep stepping with a while loop: an empty square is a move; a piece ends the slide, and if it is an
enemy it can be taken.
```js
const STRAIGHT = [[1, 0], [-1, 0], [0, 1], [0, -1]]
if (kind === 'R') {
for (const [dr, dc] of STRAIGHT) {
let tr = r + dr
let tc = c + dc
while (inside(tr, tc)) {
if (board[tr][tc]) {
if (board[tr][tc][0] !== color) add(r, c, tr, tc)
break
}
add(r, c, tr, tc)
tr += dr
tc += dc
}
}
}
```STRAIGHT lists the four straight directions as one-square steps.while (inside(tr, tc)) repeats as long as the square is on the board.break leaves the loop: nothing can pass through a piece.+=.KING write STRAIGHT.pseudoMoves, under the closing } of the knight/king block (above the } that closes the c loop), write the
rook block.Skills: Loops
← Say whose turn it is Bishops and queens →