Skip to content

Instantly share code, notes, and snippets.

@ctide
Created January 7, 2012 01:54
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 ctide/1573464 to your computer and use it in GitHub Desktop.
Save ctide/1573464 to your computer and use it in GitHub Desktop.
Intermediate ruby warrior code, hideous mess since I never bothered to refactor anything
class Player
attr_accessor :directions, :fled, :inverse
def initialize
@directions = [:forward, :left, :right, :backward]
@inverse = {:forward => :backward, :left => :right, :right => :left, :backward => :forward}
@fled = false
end
def play_turn(warrior)
if (warrior.health < 5 && !@fled && enemies_left?(warrior) && enemies_nearby?(warrior))
return flee(warrior)
end
if (need_rest?(warrior))
return warrior.rest!
end
return true if find_ticking_captive(warrior)
if (!bomb?(warrior, warrior.direction_of_stairs))
return true if check_for_monsters(warrior)
return true if check_for_captive(warrior)
if (@fled)
warrior.walk!(@inverse[@fled])
@fled = false
else
return true if find_captives(warrior)
return true if find_monsters(warrior)
warrior.walk!(warrior.direction_of_stairs)
end
end
end
def check_for_monsters(warrior)
enemies = []
self.directions.each do |d|
if (warrior.feel(d).enemy?)
enemies.push(d)
end
end
return false if (enemies.empty?)
if (enemies.length == 1)
warrior.attack!(enemies[0])
else
warrior.bind!(enemies[1])
end
return true
end
def check_for_captive(warrior)
self.directions.each do |d|
if (warrior.feel(d).captive?)
if (warrior.feel(d).unit.character == "C")
warrior.rescue!(d)
else
warrior.attack!(d)
end
return true
end
end
return false
end
def flee(warrior)
self.directions.each do |d|
if (warrior.feel(d).empty?)
warrior.walk!(d)
@fled = d
return true
end
end
return false
end
def find_captives(warrior)
dudes = warrior.listen
dudes.each do |d|
if (d.captive?)
if (warrior.feel(warrior.direction_of(d)).stairs?)
self.directions.each do |dir|
if (dir != warrior.direction_of(d) && warrior.feel(dir).empty?)
warrior.walk!(dir)
break
end
end
else
warrior.walk!(warrior.direction_of(d))
end
return true
end
end
return false
end
def find_ticking_captive(warrior)
dudes = warrior.listen
dudes.each do |d|
if (d.captive? && d.ticking?)
if (warrior.feel(warrior.direction_of(d)).stairs?)
self.directions.each do |dir|
if (dir != warrior.direction_of(d) && warrior.feel(dir).empty?)
warrior.walk!(dir)
break
end
end
else
if (warrior.feel(warrior.direction_of(d)).captive?)
warrior.rescue!(warrior.direction_of(d))
return true
end
if (warrior.feel(warrior.direction_of(d)).empty?)
warrior.walk!(warrior.direction_of(d))
else
if (warrior.feel(warrior.direction_of(d)).enemy?)
self.directions.each do |dir|
if (warrior.feel(dir).enemy? && (dir != warrior.direction_of(d)))
warrior.bind!(dir)
return true
end
end
if (!bomb?(warrior, warrior.direction_of(d)))
warrior.attack!(warrior.direction_of(d))
end
else
self.directions.each do |dir|
if (dir != warrior.direction_of(d) && warrior.feel(dir).empty?)
warrior.walk!(dir)
break
end
end
end
end
end
return true
end
end
return false
end
def find_monsters(warrior)
dudes = warrior.listen
dudes.each do |d|
if (d.enemy?)
warrior.walk!(warrior.direction_of(d))
return true
end
end
return false
end
def need_rest?(warrior)
return false if warrior.health == 20
dudes = warrior.listen
dudes.each do |d|
if (d.ticking? && warrior.health >= 8)
return false
end
end
self.directions.each do |dir|
if (warrior.feel(dir).enemy?)
return false
end
end
return true if (enemies_left?(warrior) && warrior.health < 20)
return false
end
def bomb?(warrior, direction)
enemies = warrior.look(direction)
if (enemies[0].enemy? && enemies[1].enemy? && warrior.health > 5)
warrior.detonate!(direction)
return true
end
return false
end
def enemies_left?(warrior)
warrior.listen.each do |d|
if (d.enemy?)
return true
end
end
return false
end
def enemies_nearby?(warrior)
directions.each do |d|
if (warrior.feel(d).enemy?)
return true
end
end
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment