Hello! I make a game like "Match 3". The functionality is slightly different: when a player clicks on a certain color, all the same neighboring colors should be removed. enter image description here

How can this be implemented? Here is my code. I thought to do this operation using recursion, but it did not work out. Too many recursions - such an error.

findNeighbours: function(bl) { var _self = this; var t = this.getTop(bl.id); if (t) { if (t.color == bl.color) { t.pressed = true; _self.findNeighbours(t); } } var b = this.getBottom(bl.id); if (b) { if (b.color == bl.color) { b.pressed = true; _self.findNeighbours(b); } } var l = this.getLeft(bl.id); if (l) { if (l.color == bl.color) { l.pressed = true; _self.findNeighbours(l); } } var r = this.getRight(bl.id); if (r) { if (r.color == bl.color) { r.pressed = true; _self.findNeighbours(r); } } return; }, 

    1 answer 1

    if (t && !t.pressed) { ... , respectively for b , l and r

    • How can I stop the execution of recursion, if there is nothing and continue the search further? - Reaget
    • As I showed, you will not process cells that have already been processed, and the recursion will stop .. - Igor
    • thanks, earned :) - Reaget