Skip to content

Instantly share code, notes, and snippets.

@logaan
Created July 16, 2019 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logaan/c9df26b512fa123f68a0fee7b06d5fe1 to your computer and use it in GitHub Desktop.
Save logaan/c9df26b512fa123f68a0fee7b06d5fe1 to your computer and use it in GitHub Desktop.
class Dude
def initialize(warrior, previous)
@warrior = warrior
@direction = previous[:direction]
@previous = previous
end
def dump_state
{
direction: @direction,
health: @warrior.health
}
end
def hurt?
@warrior.health < 20
end
def previously_unhurt?
@previous[:health] >= 20
end
def taking_damage?
@warrior.health < @previous[:health]
end
def feel
space = @warrior.feel(@direction)
if space.empty?
Empty.new(self)
elsif space.captive?
Captive.new(self)
elsif space.enemy?
Enemy.new(self)
elsif space.wall?
Wall.new(self)
elsif space.ticking?
Ticking.new(self)
elsif space.stairs?
Stairs.new(self)
end
end
OPPOSITE_DIRECTION = {
forward: :backward,
backward: :forward
}
def act(action)
case action
when :advance
@warrior.walk!(@direction)
when :retreat
@warrior.walk!(OPPOSITE_DIRECTION[@direction])
when :turn_around
@warrior.pivot!
when :attack
@warrior.attack!(@direction)
when :rest
@warrior.rest!
when :rescue
@warrior.rescue!(@direction)
end
end
end
class Empty
def initialize(dude)
@dude = dude
end
def choose_action
if @dude.taking_damage? && @dude.previously_unhurt?
:advance
elsif @dude.taking_damage?
:retreat
elsif @dude.hurt?
:rest
else
:advance
end
end
end
class Stairs
def initialize(dude)
@dude = dude
end
def choose_action
:walk!
end
end
class Enemy
def initialize(dude)
@dude = dude
end
def choose_action
:attack
end
end
class Captive
def initialize(dude)
@dude = dude
end
def choose_action
:rescue
end
end
class Wall
def initialize(dude)
@dude = dude
end
def choose_action
:turn_around
end
end
class Ticking
def initialize(dude)
@dude = dude
end
end
class Player
def initialize
@previous = {
health: 20,
direction: :forward
}
end
def play_turn(warrior)
dude = Dude.new(warrior, @previous)
action = dude.feel.choose_action
dude.act(action)
@previous = dude.dump_state
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment