Workshop › Pool › Step 20 of 33
Pocketing the cue ball is a scratch: it costs an extra shot, and when everything has stopped the cue ball comes back on its spot. If another ball sits there, it moves left until it has room.
```js
balls = balls.filter((b) => {
if (!pocketed(b)) return true
if (b.cue) shots += 1 // a scratch costs a shot
return false
})
// The cue ball comes back on its spot, or as close to it as there is room.
function respot() {
cue = ball(CUE_START.x, CUE_START.y, '#f8fafc', 0)
while (balls.some((b) => Math.hypot(b.x - cue.x, b.y - cue.y) < R * 2)) cue.x -= R
balls.unshift(cue)
}
if (!balls.includes(cue)) respot()
```filter function now has a body: a ball that is not pocketed stays; a pocketed cue ball adds a shot; every pocketed
ball is dropped.while repeats as long as its condition is true: while the new cue ball overlaps any ball, move it left by R.unshift puts the cue ball back at the front of balls.respot runs if the cue ball is no longer in balls.step, turn the filter line into the version with a body.function update() { write respot with its comment.update, write the respot line between if (moving) return and state = 'aiming'.Skills: Game states