Created
October 18, 2015 11:02
-
-
Save karapetyan/f1c8e9f219c0d70e38b0 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 | |
def initialize | |
@directions = [:forward, :right, :backward, :left] | |
end | |
def play_turn(warrior) | |
@warrior = warrior | |
@move_direction = find_way | |
survive | |
end | |
def survive #universal survivor algorithm | |
case | |
when next_space.enemy? | |
if around_enemies.size >= 1 | |
@warrior.bind!(around_enemies.last) | |
else | |
case | |
when @warrior.health <= 3 | |
@warrior.bind!(@move_direction) | |
when can_explode? | |
@warrior.detonate!(@move_direction) | |
else | |
@warrior.attack!(@move_direction) | |
end | |
end | |
when next_space.captive? | |
if @warrior.health >= 6 | |
@warrior.rescue!(@move_direction) | |
else | |
@warrior.rest! | |
end | |
else | |
if @warrior.health < 8 | |
@warrior.rest! | |
else | |
@warrior.walk!(@move_direction) | |
end | |
end | |
end | |
private | |
def find_way | |
if @warrior.listen.size > 0 | |
ticking_places = @warrior.listen.select { |space| space.ticking? } | |
if ticking_places.size > 0 | |
@warrior.direction_of(ticking_places.last) | |
else | |
@warrior.direction_of(@warrior.listen.last) | |
end | |
else | |
@warrior.direction_of_stairs | |
end | |
end | |
def can_explode? | |
if mined_spaces.size == 0 && @warrior.health > 6 | |
true | |
elsif mined_spaces.any? { |mined_space| @warrior.distance_of(mined_space) > 2 } && @warrior.health > 4 | |
true | |
else | |
false | |
end | |
end | |
def mined_spaces | |
@warrior.listen.select { |space| space.ticking? } | |
end | |
def next_space | |
@warrior.feel(@move_direction) | |
end | |
def near_enemies | |
@directions.select { |d| @warrior.feel(d).enemy? } | |
end | |
def near_captives | |
@directions.select { |d| @warrior.feel(d).captive? } | |
end | |
def around_enemies | |
@directions.select { |d| d != @move_direction && @warrior.feel(d).enemy? } | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment