Last active
December 24, 2015 15:39
-
-
Save distinctgrey/6822675 to your computer and use it in GitHub Desktop.
JSWarrior by FusionCharts - solution lvl7 - see http://jswarrior.fusioncharts.com/
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
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