Skip to content

Instantly share code, notes, and snippets.

@detj
Created July 5, 2012 13:32
Show Gist options
  • Save detj/3053676 to your computer and use it in GitHub Desktop.
Save detj/3053676 to your computer and use it in GitHub Desktop.
Finds blocks of same property recursively
// 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