Skip to content

Instantly share code, notes, and snippets.

@taptapdan
Created December 26, 2015 19:17
Show Gist options
  • Select an option

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

Select an option

Save taptapdan/e1a63d2e1e6506f88c1a to your computer and use it in GitHub Desktop.
CodeCombat: Backwoods Brawl Solution
// Stay alive for one minute.
// If you win, it gets harder (and more rewarding).
// If you lose, you must wait a day before you can resubmit.
// Remember, each submission gets a new random seed.
// Determines if Warrior should use cleave
// When?
// - If cleave is ready AND
// - If there are more than 2 enemies within cleave range
this.shouldCleave = function() {
if ( this.enemiesWithinRange(10) > 2 && this.isReady("cleave") ) {
return true;
} else {
return false;
}
};
// returns the number of enemies within the specified distance
this.enemiesWithinRange = function(distance) {
var enemies = this.findEnemies();
var num = 0;
for (var i = 0; i < enemies.length; i++) {
if (this.distanceTo(enemies[i]) <= distance) {
num++;
}
}
return num;
};
// find the nearest thrower type unit
this.findNearestThrower = function() {
var enemies = this.findEnemies();
var throwers = []; // array of existing throwers
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].type == 'thrower') {
throwers.push(enemies[i]);
}
}
return this.findNearest(throwers);
};
loop {
thrower = this.findNearestThrower();
// attack throwers first
if (thrower) {
if (this.shouldCleave()) {
this.cleave(thrower);
} else {
this.attack(thrower);
}
// attack other enemies
} else {
enemy = this.findNearest(this.findEnemies());
if (enemy) {
if (this.shouldCleave()) {
this.cleave(enemy);
} else {
this.attack(enemy);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment