Workshop › Puzzle Bobble-style Bubble Shooter › Step 3 of 31

A grid of cells

What we are doing

The bubbles hang in rows. We keep them in a 2D array, grid[r][c]: a list of rows, each a list of cells. A cell holds a color number, or -1 when it is empty. A new game fills the top five rows with random colors.

```js
const COLS = 10 // bubbles in an even row; odd rows have one less and sit half a bubble to the right
const ROWS = 14

let grid // grid[r][c]: a color index, or -1 for an empty cell

function reset() {
  grid = []
  for (let r = 0; r < ROWS; r++) {
    grid.push([])
    for (let c = 0; c < COLS; c++) grid[r].push(r < 5 ? Math.floor(Math.random() * COLORS.length) : -1)
  }
}

reset()
```

What it means

Your turn

  1. Under R write COLS and ROWS.
  2. Under COLORS, leave an empty line and write let grid and reset.
  3. Write reset() just above the last line, requestAnimationFrame(loop).

Skills: Arrays and objects

All steps

  1. The night sky
  2. One bubble
  3. A grid of cells
  4. Draw the grid
  5. Bubbles that nest
  6. The shooter
  7. Aim upwards
  8. Turn with the arrows
  9. Aim with the pointer
  10. Fire!
  11. Fly
  12. Bounce off the walls
  13. Touching something?
  14. Stick into the grid
  15. Smaller steps
  16. Six neighbours
  17. Flood fill
  18. Three in a row pop
  19. Bubbles that fall
  20. Cut them loose
  21. Show the score
  22. Only useful colors
  23. Count the shots
  24. The ceiling comes down
  25. Win or lose
  26. The danger line
  27. The end message
  28. Play again
  29. Best score
  30. A dotted guide
  31. Build it yourself: swap bubbles