Last active
December 23, 2015 15:29
-
-
Save twidi/6655641 to your computer and use it in GitHub Desktop.
Code for winning 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) { | |
// init status | |
if (!warrior._initialized) { | |
warrior._side_done = false; | |
warrior._last_health = 20; | |
warrior._initialized = true; | |
} | |
// check conditions | |
var health = warrior.getHealth(), | |
under_attack = (health < warrior._last_health), | |
low_health = (health < 10), // (3 turns to take refuge) | |
full_health = (health >= 19), // consider 19 as full | |
check = warrior.check(), | |
check_backward, on_the_side; | |
// there is a bug in the game, an exception is raised when checking | |
// backward if we have a wall behind | |
try { | |
check_backward = warrior.check('backward'); | |
} catch(e) { | |
check_backward = "wall"; | |
} | |
on_the_side = (check_backward == "wall"); | |
// we hit the wall, we walked in the wrong direction | |
if (check == "wall") { | |
warrior.pivot(); | |
// under attack, nearly dying, run | |
} else if (low_health && under_attack) { | |
warrior.walk('backward'); | |
// not under attack but lost some blood, rest a moment | |
} else if (!under_attack && !full_health) { | |
warrior.rest(); | |
// ok, the paradise, ready to kill gangsta* | |
} else if(check == "enemy") { | |
warrior.attack(); | |
// or to check diamons, forever* | |
} else if(check_backward == "diamond") { | |
warrior.collect('backward'); | |
} else if(check == "diamond") { | |
warrior.collect(); | |
// go check the side if not done | |
} else if (!warrior._side_done && !on_the_side) { | |
warrior.walk('backward'); | |
// let's walk to the mall, today* | |
} else { | |
// mark if we are on the side | |
if (!warrior._side_done && on_the_side) { | |
warrior._side_done = true; | |
} | |
warrior.walk(); | |
} | |
// save current health to see next turn if we are under atack | |
warrior._last_health = health; | |
} // * yes i a have a terrible sense of humor |
I think the
warrior.getHealth() == warrior._health_variable
should be warrior.getHealth() >= warrior._health_variable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks good, but when i do "warrior._health_variable = warrior.getHealth();" ( warrior._last_health = health;) it doesnt seem to store it correctly... im stuck at level 4 with
jsWarrior.turn = function(warrior) {
if(warrior.check() == "enemy") {
warrior.attack();
}
else {
if ( warrior.getHealth() < 20 && warrior.getHealth() == warrior._health_variable ) {
warrior.rest();
} else {
warrior.walk();
}
}
warrior._health_variable = warrior.getHealth();
}
this code heals for 1 turn, then moves forward 1, then heals, and that doesnt work...