Skip to content

Instantly share code, notes, and snippets.

@esova-ana
Last active April 19, 2016 01:13
Show Gist options
  • Save esova-ana/297550888c1b63327c7985694525d3fe to your computer and use it in GitHub Desktop.
Save esova-ana/297550888c1b63327c7985694525d3fe to your computer and use it in GitHub Desktop.
Functions (CodeCombat)
// Practice using modulo to loop over an array
this.pickUpNearestCoin = function() {
var items = this.findItems();
var nearestCoin = this.findNearest(items);
if(nearestCoin) {
this.move(nearestCoin.pos);
}
};
var summonTypes = ["soldier", "archer", "archer", "soldier"];
this.summonTroops = function() {
var summonType = summonTypes[this.built.length % summonTypes.length];
if (this.gold >= this.costOf(summonType)) {
this.summon(summonType);
}
};
this.commandSoldiers = function() {
var friends = this.findFriends();
for(var i=0; i < friends.length; i++) {
var enemy = friends[i].findNearestEnemy();
if(enemy) {
this.command(friends[i],"attack", enemy);
}
}
};
loop {
this.pickUpNearestCoin();
this.summonTroops();
this.commandSoldiers();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment