Skip to content

Instantly share code, notes, and snippets.

@taptapdan
Created January 1, 2016 18:40
Show Gist options
  • Select an option

  • Save taptapdan/86c1693a8a3cb194291f to your computer and use it in GitHub Desktop.

Select an option

Save taptapdan/86c1693a8a3cb194291f to your computer and use it in GitHub Desktop.
CodeCombat: Backwoods Treasure Solution
// Returns Best Coin to Move Toward
this.bestCoin = function() {
var bestCoin = null;
var coinRank = 0;
var coins = this.findItems();
for (var i=0; i < coins.length; i++) {
if ( coins[i].value / this.distanceTo(coins[i]) > coinRank ) {
coinRank = coins[i].value / this.distanceTo(coins[i]);
bestCoin = coins[i];
}
}
return bestCoin;
};
// Can Cast/Cast Wrapper
this.castSpell = function(spellName, target) {
// check that spell name is in list of castable spells
if (this.spellNames.indexOf(spellName) == -1) {
return false;
}
// check that spell can be cast against target
if (this.canCast(spellName, target)) {
// if so, then cast spell
this.cast(spellName, target);
return true;
}
return false;
};
loop {
// Move Towards Best Coin
var coin = this.bestCoin();
if (coin) {
this.move(coin.pos);
}
var corpse = this.findNearest(this.findCorpses());
if (corpse && this.distanceTo(corpse) < this.spells['raise-dead'].radius && this.canCast("raise-dead")) {
this.cast("raise-dead");
}
// But, Attack Enemies if any exist
var enemy = this.findNearest(this.findEnemies());
if (enemy) {
if ( this.castSpell("fear", enemy) ) continue;
if ( this.castSpell("drain-life", enemy) ) continue;
if ( this.castSpell("magic-missle", enemy) ) continue;
if ( this.distanceTo(enemy) < this.attackRange ) {
this.attack(enemy);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment