Skip to content

Instantly share code, notes, and snippets.

@zastrow
Created March 18, 2014 16:29
Show Gist options
  • Save zastrow/9623710 to your computer and use it in GitHub Desktop.
Save zastrow/9623710 to your computer and use it in GitHub Desktop.
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