Workshop › Puzzle Bobble-style Bubble Shooter › Step 16 of 31
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))
}
```inGrid checks that a row and column exist (odd rows have one cell less).shift is -1 on even rows and 0 on odd rows; the cells above and below are at c + shift and c + shift + 1.filter drops the neighbours that would be outside the grid.cellPos line, write inGrid.cellPos line, write the comment and neighbors.Skills: Arrays and objects