Workshop › Puzzle Bobble-style Bubble Shooter › Step 22 of 31
Near the end, a color that is no longer on the board is useless. The shooter now only offers colors still there.
```js
// The colors still on the board, so the shooter never offers a useless one.
function pickColor() {
const present = [...new Set(grid.flat().filter((color) => color >= 0))]
const choices = present.length ? present : COLORS.map((_, i) => i)
return choices[Math.floor(Math.random() * choices.length)]
}
```grid.flat() makes one list of every cell; filter keeps the bubbles; a Set keeps each color once.choices.Add the comment above pickColor and replace its body.
Skills: Arrays and objects
← Show the score Count the shots →