Last active
April 19, 2016 01:13
-
-
Save esova-ana/297550888c1b63327c7985694525d3fe to your computer and use it in GitHub Desktop.
Functions (CodeCombat)
This file contains hidden or 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
// 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