Workshop › Battleship › Step 31 of 35
The arrow keys move the cursor. Going off one edge brings it back on the other.
```js
const moves = { ArrowUp: [-1, 0], ArrowDown: [1, 0], ArrowLeft: [0, -1], ArrowRight: [0, 1] }
if (moves[event.key]) {
const [dr, dc] = moves[event.key]
cursor = { r: (cursor.r + dr + N) % N, c: (cursor.c + dc + N) % N }
} else if (event.key === 'h' || event.key === 'H') showHeat = !showHeat
```moves is a table: each arrow key gives a row change and a column change.const [dr, dc] = ... takes the two numbers out of the pair.(cursor.r + dr + N) % N wraps around: from row 0 going up gives 9.At the top of the keydown listener, write moves and the arrow block, and turn the H line into } else if.
Skills: Keyboard and mouse input
← A keyboard cursor Fire with Enter, play again →