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