Atölye › Puzzle Bobble Tarzı Balon Atıcı › 22/31. adım
Oyunun sonlarına doğru tahtada artık hiç olmayan bir renk gelirse işe yaramaz, sadece sinir bozar. Nişancı yalnız tahtada hâlâ olan renkleri versin.
```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() → ızgarayı tek bir listeye açar..filter((color) => color >= 0) → yalnız dolu hücreler.new Set(...) → küme her değeri bir kez tutar; [... ] onu yeniden listeye çevirir. Sonuç: tahtadaki farklı
renkler, ör. [0, 3].present.length ? present : COLORS.map((_, i) => i) → tahtada renk varsa onlar, tahta boşsa bütün renkler
([0, 1, 2, 3, 4]).choices[Math.floor(Math.random() * choices.length)] → listeden rastgele bir eleman.pickColor'ın üstüne yorumu yaz ve içindeki tek satırı üç yeni satırla değiştir. Çalıştır.
Beceriler: Diziler ve nesneler