Skip to content

Instantly share code, notes, and snippets.

@zastrow zastrow/gist:9623710
Created Mar 18, 2014

Embed
What would you like to do?
JSWarrior
jsWarrior.turn = function(warrior) {
var action = null;
if(!warrior.game_started){
jsWarrior.firstSquareReached(warrior);
}
if(warrior.check() == "diamond") {
action = warrior.collect;
}
else if(warrior.check() == "enemy") {
action = warrior.attack;
}
else if( jsWarrior.isBeingAttackedAtRange(warrior) && warrior.getHealth() <= 11 ) {
action = warrior.retreat;
}
else if(warrior.getHealth() >= 20 || jsWarrior.isBeingAttackedAtRange(warrior) ) {
action = warrior.walk;
}
else {
action = warrior.rest;
}
//Save health to reference next turn
warrior.previousHealth = warrior.getHealth();
action();
}
jsWarrior.isBeingAttackedAtRange = function(warrior){
return warrior.getHealth() < warrior.previousHealth;
}
//Move back one space and turn forward again
jsWarrior.retreat = function(warrior){
warrior.walk('backward');
}
//Move to the first square in the room
jsWarrior.moveToFirstSquare = function(warrior){
if(warrior.check('backward') == 'diamond'){
warrior.collect('backward');
}
else if(warrior.check('backward') == 'empty'){
warrior.walk('backward');
}else{
warrior.firstSquareReached = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.