Workshop › Candy Crush-style Match 3 › Step 22 of 29
A board can run out of moves: no swap makes a match. To know, try them all: swap each pair of neighbours, look, and
swap back. hasMove returns the first move that works, or null.
```js
// Is there any swap that would make a match? Try each one, look, and swap back.
function hasMove() {
for (let r = 0; r < N; r++) {
for (let c = 0; c < N; c++) {
for (const [dr, dc] of [[0, 1], [1, 0]]) {
if (r + dr >= N || c + dc >= N) continue
const a = { r, c }
const b = { r: r + dr, c: c + dc }
swap(a, b)
const found = findMatches().size > 0
swap(a, b)
if (found) return { a, b }
}
}
}
return null
}
```{ a, b }; if none, null.Write the comment and hasMove above function trySwap(a, b) {.
Skills: Loops, Game states
← Out from under the top Never stuck →