Created
December 1, 2011 04:23
-
-
Save bil-bas/1413598 to your computer and use it in GitHub Desktop.
Rubywarrior (Beginner) code that completes tower
This file contains 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
require 'forwardable' | |
=begin | |
Rubywarrior beginner level: | |
Level 1: S | |
Level 2: A | |
Level 3: A | |
Level 4: A | |
Level 5: A | |
Level 6: B | |
Level 7: B | |
Level 8: S | |
Level 9: A | |
SPOILERS IF YOU READ FURTHER | |
SHAME ON YOU | |
IT WILL COMPLETELY SPOIL THE GAME IF YOU HAVEN'T ALREADY FINISHED IT! | |
=end | |
ENEMY_DATA = { | |
"Sludge" => { health: 12, damage: 3, range: 1 }, | |
"Thick Sludge" => { health: 24, damage: 3, range: 1 }, | |
"Archer" => { health: 7, damage: 2, range: 2 }, | |
"Wizard" => { health: 3, damage: 11, range: 2 }, | |
"Captive" => { health: 1, damage: 0, range: 0 }, | |
} | |
DAMAGE = 5 # How much damage the player does. | |
MIN_SAFE_HEALTH = 16 # Minimum health to ensure you heal up to stay alive (bit of an arbitrary number, sadly). | |
class Player | |
def play_turn(turn) | |
@warrior ||= Warrior.new(turn) | |
@warrior.play_turn(turn) | |
end | |
end | |
class Warrior | |
extend Forwardable | |
def_delegators :@turn, :walk!, :rest!, :attack!, :rescue!, :shoot!, :pivot! | |
def_delegators :@turn, :feel, :health, :look | |
def initialize(initial_turn) | |
@turn = initial_turn | |
@last_health = health | |
end | |
def looking_at(direction) | |
look(direction).find {|s| not s.empty? } | |
end | |
def min_health_to_defeat(enemy) | |
# TODO: Doesn't care how far away we start at, but nevermind. | |
data = ENEMY_DATA[enemy] | |
x = (data[:damage] * (data[:range] - 1)) + # Time to walk into range. | |
(data[:damage] * (data[:health] / DAMAGE)) + # Time to kill in melee. Round down because we don't take damage on the last turn. | |
+ 1 # Need to be alive at the end. | |
end | |
def play_turn(turn) | |
@turn = turn | |
act | |
@last_health = health | |
end | |
def damage_taken | |
@last_health - health | |
end | |
def act | |
if feel(:backward).captive? | |
rescue! :backward | |
elsif looking_at(:backward).to_s == "Captive" | |
walk! :backward | |
elsif feel(:forward).wall? | |
pivot! | |
elsif feel(:forward).empty? | |
# If being shot at, we need to do something about it. | |
looking_at = looking_at(:forward).to_s | |
if damage_taken > 0 or looking_at == "Wizard" | |
if looking_at == "Wizard" | |
shoot! :forward | |
elsif looking_at == "Archer" and health < min_health_to_defeat(looking_at) | |
walk! :backward | |
else | |
walk! :forward | |
end | |
else | |
if health < MIN_SAFE_HEALTH and not (feel(:forward).stairs? or looking_at == "wall") | |
rest! | |
else | |
walk! :forward | |
end | |
end | |
elsif feel(:forward).captive? | |
rescue! :forward | |
else | |
attack! :forward | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment