Created
March 18, 2014 16:29
-
-
Save zastrow/9623710 to your computer and use it in GitHub Desktop.
JSWarrior
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) { | |
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