Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getNumberOfNeighbors(cells, currentCell){
- return [
- [-1,-1], [-1, 0], [-1,1],
- [0,-1], [0,1],
- [1,-1], [1,0], [1,1] ].reduce(function(sum,c){
- var cell = [currentCell[0]+c[0], currentCell[1]+c[1]]
- return sum += cells.contains(cell)?1:0
- }, 0)
- }
- // example:
- console.log(
- getNumberOfNeighbors([ [0,0], [0,2], [2,2] ], [0,1])
- ); // prints 2
Add Comment
Please, Sign In to add comment