Created
April 19, 2016 23:34
-
-
Save esova-ana/b9d42a81023b0330b64b597134215e0e to your computer and use it in GitHub Desktop.
Strategy (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
this.chooseStrategy = function () { | |
var enemies = this.findEnemies(); | |
if (this.gold >= this.costOf("griffin-rider")) { | |
this.summon("griffin-rider"); | |
return "griffin-rider"; | |
} else if (enemies) { | |
for (var i = 0; i < enemies.length; i++) { | |
var enemy = enemies[i]; | |
if (enemy && enemy.type === "fangrider") { | |
return "fight-back"; | |
} | |
} | |
} else { | |
return "collect-coins"; | |
} | |
}; | |
this.commandAttack = function (friend) { | |
var enemies = this.findEnemies(); | |
var friends = this.findFriends(); | |
for (i=0; i < friends.length; i++) { | |
friend = friends[i]; | |
if (friend && friend.type === "griffin-rider") { | |
var griffin = friend; | |
} | |
} | |
for (j=0; j < enemies.length; j++) { | |
var enemy = enemies[j]; | |
if (enemy && enemy.type === "ogre") { | |
var ogre = enemy; | |
} | |
} | |
if (griffin) { | |
if (ogre) { | |
this.command(griffin, "attack", ogre); | |
} else if (enemy) { | |
this.command(griffin, "defend", {x: 76, y: 38}); | |
} | |
} | |
}; | |
this.pickUpCoin = function () { | |
var items = this.findItems(); | |
var nearestCoin = this.findNearest(items); | |
if (nearestCoin) { | |
this.move(nearestCoin.pos); | |
} | |
}; | |
this.heroAttack = function () { | |
var enemies = this.findEnemies(); | |
for (var i = 0; i < enemies.length; i++) { | |
var enemy = enemies[i]; | |
if (enemy && enemy.type === "fangrider" && enemy.pos.x < 36) { | |
this.attack(enemy); | |
} | |
} | |
}; | |
loop { | |
this.commandAttack(); | |
var strategy = this.chooseStrategy(); | |
if ("fight-back") { | |
this.heroAttack(); | |
} | |
else if ("griffin-rider") { | |
this.commandAttack(); | |
} | |
else { | |
this.say("pickup"); | |
this.pickUpCoin(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment