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

Six neighbours

What we are doing

To find bubbles of the same color that touch, we need each cell's neighbours. In a honeycomb grid a cell has six: two beside it, two above and two below. Which columns those are depends on whether the row is shifted.

```js
const inGrid = (r, c) => r >= 0 && r < ROWS && c >= 0 && c < cols(r)

// The six cells around (r, c). Odd rows are shifted right, so their neighbours above and below are at c and c + 1;
// even rows' are at c - 1 and c.
function neighbors(r, c) {
  const shift = r % 2 === 0 ? -1 : 0
  return [
    [r, c - 1], [r, c + 1],
    [r - 1, c + shift], [r - 1, c + shift + 1],
    [r + 1, c + shift], [r + 1, c + shift + 1],
  ].filter(([nr, nc]) => inGrid(nr, nc))
}
```

What it means

Your turn

  1. Above the cellPos line, write inGrid.
  2. Under the cellPos line, write the comment and neighbors.

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