Workshop › Candy Crush-style Match 3 › Step 9 of 29
A move is two clicks. The second one decides: a neighbour of the selected gem swaps with it; the same gem again unselects it; any other gem becomes the new selection.
```js
function swap(a, b) {
const gem = board[a.r][a.c]
board[a.r][a.c] = board[b.r][b.c]
board[b.r][b.c] = gem
}
function trySwap(a, b) {
if (Math.abs(a.r - b.r) + Math.abs(a.c - b.c) !== 1) return false
swap(a, b)
return true
}
if (selected && trySwap(selected, cell)) selected = null
else selected = selected && selected.r === cell.r && selected.c === cell.c ? null : cell
```swap keeps a's gem in gem, puts b's into a, then the kept one into b.Math.abs drops the minus sign.function cellAt(event) { write swap and trySwap.selected = cell with the two lines.Skills: Keyboard and mouse input, Functions
← Show the selection Find every match →