Skip to content

Instantly share code, notes, and snippets.

@distinctgrey
Last active December 24, 2015 15:39
Show Gist options
  • Save distinctgrey/6822675 to your computer and use it in GitHub Desktop.
Save distinctgrey/6822675 to your computer and use it in GitHub Desktop.
JSWarrior by FusionCharts - solution lvl7 - see http://jswarrior.fusioncharts.com/
jsWarrior.turn = function (warrior) {
warrior.underAttack = warrior.previousHealth > warrior.getHealth();
warrior.timeToRetreat = warrior.getHealth() <= 10;
warior.retreating = warrior.retreating || false;
// Start
if (!warrior.started) {
warrior.started = true;
warrior.pivot();
return;
}
// Normal move
if (warrior.check() == 'wall') {
warrior.pivot();
} else if (warrior.check() == 'diamond') {
warrior.collect();
} else if (warrior.check() == "enemy") {
warrior.attack();
} else {
if ((warrior.timeToRetreat && !warrior.retreating) || warrior.retreating) {
warrior.retreating = true;
if (warrior.underAttack) {
warrior.walk('backward');
} else {
warrior.rest();
if (warrior.getHealth() == 20) {
warrior.retreating = false;
}
}
} else {
warrior.walk();
}
}
// #LT
warrior.previousHealth = warrior.getHealth();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment