Created
July 5, 2012 13:32
-
-
Save detj/3053676 to your computer and use it in GitHub Desktop.
Finds blocks of same property recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ig.game.* are global objects being used to store results | |
var findGemBlocks = function () { | |
var connectedGems = [], | |
finalGemBlocks = [], | |
getConnectedGems = function (gem) { | |
if (!_.include(connectedGems, gem)) { | |
connectedGems.push(gem); | |
if (_.include(_.flatten(finalGemBlocks), gem)) return false; | |
var candidates = ig.game.getAdjacentGemsOfSameColor(gem); | |
candidates = _.filter(candidates, function (each) { | |
getConnectedGems(each); | |
if (_.include(connectedGems, each)) return false; | |
connectedGems.push(each); | |
}); | |
} | |
return connectedGems; | |
}; | |
ig.game.availableGemBlocks = []; | |
ig.game.singleGemBlocks = []; | |
ig.game.dualGemBlocks = []; | |
_.each(ig.game.gemEntities, function (gem) { | |
connectedGems = []; | |
connectedGems = getConnectedGems(gem); | |
if (connectedGems) { | |
if (connectedGems.length > 2) { | |
ig.game.findStrongestGem(connectedGems); | |
ig.game.availableGemBlocks.push(connectedGems); | |
} else if (connectedGems.length == 2) { | |
ig.game.dualGemBlocks.push(connectedGems); | |
} else if (connectedGems.length == 1) { | |
ig.game.singleGemBlocks.push(connectedGems); | |
} | |
finalGemBlocks.push(connectedGems); | |
} | |
}); | |
return finalGemBlocks; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment