This is for Ruby Warrior Level 6.
On first glance, it looks like level 6 introduces one new aspect: we have to go backward to save the captive. If we just use our level 5 code and play through, we’ll see that not only is our captive left behind, but our warrior is too bold. In level 5, the warrior did not rest if under attack (from archers). But in this level, charging straight ahead is no good either. I started with a flow chart to organize my thoughts.
There was one error in my decision flow chart, no captives –> something in front –> poor health –> run. If I put in this code, the warrior will try to run away from the archer when warrior health is low. However, because of the archer’s range, it’s too late to run!! The warrior will die before getting out of the archer’s range. If something is right in front of you, you just fight no matter what.
A few essential questions for the warrior are: Am I under attack? Are there captives I need to save? Is there something in front of me? Should I rest? (Do I go forward or rest.) Should I flee? (Do I keep going forward or go backward.)
I moved a few of these out into different functions. Should I rest: warrior should rest if not under attack and health is less than minimum health. Should I flee: warrior should flee if under attack and health is very poor. Am I under attack: this is true if the warrior.health is less than @health after the computer’s turn.
At the very beginning of the turn, we check to see if there are captives. If there are, we need to go backward to get them. Once captives_saved is no longer 0, direction will be forward. If there’s nothing in the upcoming square, the warrior decides between resting, fleeing (walking in the opposite direction), and walking. If there’s something in the upcoming square, we check to see if it’s a captive. If so, we rescue the captive. If not, we attack!
The should_rest min_health is set to 15, because that’s the minimum health points our warrior needs before attacking the sludge monster. The should_flee flee_health variable is set to 7 because that’s the minimum health points our warrior needs to successfully flee the archer. (With 7 points, we can take two steps back while taking – 6 damage total, and then take one final step back to be out of attack range.)