Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save esova-ana/0e18f729a8a679cb518fb2f3a958c1dd to your computer and use it in GitHub Desktop.
Save esova-ana/0e18f729a8a679cb518fb2f3a958c1dd to your computer and use it in GitHub Desktop.
Returning a function (CodeCombat) / helper
// Your goal is to keep at least one flower alive for 60 seconds.
// set 3 new locations
var LOCATIONS = [
{x: 72, y: 44},
{x: 49, y: 59},
{x: 50, y: 25},
{x: 23, y: 43}
];
// loop through all locations
// build a tower on each of them
hero.findLocationsAndBuildTowers = function (nextTower) {
for (var pos; pos = nextTower(); ) hero.buildXY("arrow-tower", pos.x, pos.y);
};
// summon new soldiers if there is enough money
hero.summonSoldiers = function() {
if (hero.gold > hero.costOf("soldier")) {
hero.summon("soldier");
}
};
hero.commandSoldiers = function(nextSoldier, go) {
for (var soldier; soldier = nextSoldier(); ) {
if (!soldier.spot) hero.command(soldier, "defend", soldier.spot = go() || go());
}
};
// collect gold
hero.collectGoldForSoldiers = function() {
var items = hero.findItems();
var item = hero.findNearest(items);
if (item) {
var itemPos = item.pos;
hero.move(itemPos);
}
};
// STRATEGY
var nextPosition = makeGetNext(LOCATIONS);
var nextTower = makeGetNext(LOCATIONS);
while (true) {
var enemies = hero.findEnemies();
hero.collectGoldForSoldiers();
hero.summonSoldiers();
var soldiers = hero.findByType("soldier");
hero.commandSoldiers(makeGetNext(soldiers), nextPosition);
if (soldiers.length >= 12) {
hero.findLocationsAndBuildTowers(nextTower);
}
}
// Helper
function makeGetNext (path) {
var len = path.length + 1;
var position = 0;
return function getNext () {
var location = path[position%len];
position += 1;
return location;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment