Skip to content

Instantly share code, notes, and snippets.

@sudocrystal
Last active August 29, 2015 14:22
Show Gist options
  • Save sudocrystal/686eb0d1c2f1001a22e6 to your computer and use it in GitHub Desktop.
Save sudocrystal/686eb0d1c2f1001a22e6 to your computer and use it in GitHub Desktop.
Ruby Warrior - Crystal
class Player
@health
@wall
def play_turn(warrior)
# cool code goes here
if look_for_captives(warrior) == false
if warrior.health < 20
if rest_or_run(warrior) == false
move_or_attack(warrior)
end
else
move_or_attack(warrior)
end
end
@health = warrior.health
end
def look_for_captives(warrior)
if warrior.feel.captive? # Save captives in front of me!
warrior.rescue!
elsif warrior.feel(:backward).captive? # Save captives behind me!
warrior.rescue!(:backward)
elsif !warrior.feel(:backward).wall? && @wall == nil # Walk till I've hit the wall once
warrior.walk!(:backward)
@wall = true
else
return false # Warrior didn't act in this method
end
return true # Warrior did act in this method
end
def rest_or_run(warrior)
if warrior.health >= @health # I'm not getting hit
warrior.rest!
elsif warrior.health < 10 # Health in danger!!
warrior.walk!(:backward)
else
return false # Warrior didn't act in this method
end
return true # Warrior did act in this method
end
def move_or_attack(warrior)
if warrior.feel.empty? # Anyone in front of me?
warrior.walk!
else
warrior.attack!
end
return true
end
end
class Player
@health
@wall
@front_tile
@back_tile
def play_turn(warrior)
# cool code goes here
@front_tile = warrior.feel
@back_tile = warrior.feel(:backward)
if !turn?(warrior) # if I need to turn around, then turn around
if !look_for_captives?(warrior) # look for captives to save!
if warrior.health < 20 # when I heal, heal up to 20
if !rest_or_run?(warrior) # do I need to rest or run?
move_or_attack(warrior) # if I don't, move or attack
end
else
move_or_attack(warrior) # move or attack
end
end
end
@health = warrior.health # remember my health so I can check if it changes
end
def look_for_captives?(warrior)
if @front_tile.captive? # Save captives in front of me!
warrior.rescue!
elsif @back_tile.captive? # Save captives behind me!
warrior.rescue!(:backward)
else
return false # Warrior didn't act in this method
end
return true # Warrior did act in this method
end
def rest_or_run?(warrior)
if warrior.health >= @health # I'm not getting hit
warrior.rest!
elsif warrior.health < 10 # Health in danger!!
warrior.walk!(:backward)
else
return false # Warrior didn't act in this method
end
return true # Warrior did act in this method
end
def move_or_attack(warrior)
if warrior.feel.empty? # Anyone in front of me?
warrior.walk!
else
warrior.attack!
end
return true
end
def turn?(warrior)
if @front_tile.wall? # if there's a wall in front of me, turn around!
warrior.pivot!(:backward)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment