Workshop › Puzzle Bobble-style Bubble Shooter › Step 20 of 31
After a pop, bubbles that no longer hang from the ceiling (through other bubbles) drop, worth 20 each. The same flood fill finds what is still held: start from every bubble in the top row.
```js
// Whatever no longer hangs from the ceiling drops, worth double.
const top = []
for (let c2 = 0; c2 < cols(0); c2++) if (grid[0][c2] >= 0) top.push([0, c2])
const held = new Set(connected(top, (nr, nc) => grid[nr][nc] >= 0).map(([hr, hc]) => hr * COLS + hc))
const loose = []
for (let r2 = 0; r2 < ROWS; r2++) for (let c2 = 0; c2 < cols(r2); c2++) {
if (grid[r2][c2] >= 0 && !held.has(r2 * COLS + c2)) loose.push([r2, c2])
}
remove(loose, 20)
```top lists the bubbles in row 0, the ones touching the ceiling.connected(top, ...) spreads through any bubble (>= 0): everything it reaches is held.c2 and r2 keep them apart from the r and c of the new bubble.In attach, write the lines inside the if block, under remove(group, 10).
Skills: Loops, Arrays and objects
← Bubbles that fall Show the score →