How do I get neighboring blocks of the same color?

I'm trying to create a simple game for matching colors, and I want to find a way to select groups of blocks of the same color.

This is fiddle. I work, and if you run it, you will see that there are problems when I try to mouse over the elements around the edges of the game area, telling me that it is trying to use undefined variables

If you note below, in the function parse_quad_tree()you will see that I am handling the case of undefined vars, but since it slows down, it means that I’m wrong somewhere ...

thank you for your time

+3
source share
4 answers

2- - . , , , :

var n = !!grid[x][y-1]?grid[x][y-1]:false;
var s = !!grid[x][y+1]?grid[x][y+1]:false;
var e = !!grid[x-1]?grid[x-1][y]:false;   // Instead of !!grid[x-1][y]
var w = !!grid[x+1]?grid[x+1][y]:false;   // Instead of !!grid[x+1][y]

, array[x][y], javascript array[x], [y] . undefined (grid[x-1]), grid[x-1][y]. undefined , undefined.

: http://jsfiddle.net/jtbowden/qWktv/

, $(c).addClass('active') parse_quad_tree, , `.addClass('active') , .

: http://jsfiddle.net/jtbowden/qWktv/1/

+5

, var , change,

var e = grid[x-1]&&!!grid[x-1][y]?grid[x-1][y]:false;
var w = grid[x+1]&&!!grid[x+1][y]?grid[x+1][y]:false;
+2

var n = !! (y > 0 && grid[x][y - 1]) ? grid[x][y - 1] : false;
var s = !! (y < grid[x].length-1 && grid[x][y + 1]) ? grid[x][y + 1] : false;
var e = !! (x > 0 && grid[x - 1][y]) ? grid[x - 1][y] : false;
var w = !! (x < grid.length-1 && grid[x + 1][y]) ? grid[x + 1][y] : false;

http://jsfiddle.net/gaby/yHKkg/64/

+2

Instead of using var colors = ['red', 'green', 'blue'];for your colors, use three different css classes with a set of colors. Thus, when you need to select all of them, it is as simple as $(".blue").

0
source

All Articles