Created
July 28, 2013 09:03
-
-
Save uu59/6098029 to your computer and use it in GitHub Desktop.
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
class Player | |
attr_reader :w | |
def play_turn(w) | |
@w = w | |
# can't @foo ||= :bar | |
if @dir.nil? | |
@dir = :backward | |
end | |
if @turn.nil? | |
@turn = 1 | |
end | |
level1 | |
#level2 | |
#level3 | |
#level4 | |
#level5 | |
#level6 | |
#level7 | |
if w.respond_to?(:health) | |
@last_health = w.health | |
end | |
@turn += 1 | |
end | |
def level1 | |
@level = 1 | |
w.walk! | |
end | |
def level2 | |
@level = 2 | |
feel_and_action | |
end | |
def level3 | |
@level = 3 | |
if damaged? && !damaged_last_turn? | |
w.rest! | |
else | |
feel_and_action | |
end | |
end | |
def level4 | |
@level = 4 | |
if damaged? && !damaged_last_turn? | |
w.rest! | |
else | |
feel_and_action | |
end | |
end | |
def level5 | |
@level = 5 | |
if damaged? && !damaged_last_turn? | |
w.rest! | |
else | |
feel_and_action | |
end | |
end | |
def level6 | |
@level = 6 | |
if @turn < 50 && w.health < 20 | |
if damaged? && damaged_last_turn? | |
w.walk!(:backward) | |
else | |
w.rest! | |
end | |
else | |
feel_and_action | |
end | |
end | |
def level7 | |
@level = 7 | |
if @turn < 50 && w.health < 20 | |
if damaged? && damaged_last_turn? | |
w.walk!(:right) | |
else | |
w.rest! | |
end | |
else | |
feel_and_action | |
end | |
end | |
def feel_and_action() | |
obj = w.feel(current_dir) | |
is = proc {|meth| obj.respond_to?(meth) && obj.send(meth) ? obj : nil} | |
case obj | |
when is[:captive?] | |
w.rescue! current_dir | |
when is[:enemy?] | |
w.attack! current_dir | |
when is[:empty?], is[:stairs?] | |
w.walk! current_dir | |
when is[:wall?] | |
w_pivot | |
else | |
w.walk! current_dir | |
end | |
end | |
def damaged_last_turn? | |
w.health < @last_health | |
end | |
def damaged? | |
w.health < 20 # TODO: how to get full of health? | |
end | |
def w_pivot | |
if current_dir == :left | |
@current_dir = :right | |
else | |
@current_dir = :left | |
end | |
if @level >= 7 | |
w.pivot!(current_dir) | |
end | |
end | |
def current_dir | |
if @current_dir == :left || @current_dir == :backward | |
@level <= 6 ? :backward : :left | |
else | |
@level <= 6 ? :forward : :right | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment