a ruby programming game
module ScorpionKing
class Pitbull < Tournament::Agent
attr_accessor :enemies, :prey
def after_start
@enemies = []
@enemy_size = 2.0/3.0
end
def think
each_population do |population|
@current = population
begin
been_attacked?
@view = @current.look_around
if field = hunted_prey || found_enemy
hunting = true
@current.move_to field
elsif field = no_enemie
hunting = false
@current.split field
else field = best_resource
hunting = false
@current.move_to field
end
end while(hunting)
end
end
def no_enemie
if @view.all?{|f| f.has_no_population? || !@enemies.include?(f.agent) }
best_resource
end
end
# goes the the biggest free resource
def best_resource
@view.select{|f| f.has_no_population? || myfield?(f) }.max_by{|f| f.resource}
end
def found_enemy
if found_on_field = @view.select do |f|
f.has_population? && @enemies.include?(f.agent) && @current.size * @enemy_size > f.population
end.min_by do |f|
f.population
end
@prey = found_on_field.agent
info "found enemy #{@prey}, hunt begins"
found_on_field
end
end
def hunted_prey
if @prey
if hunted_prey = @view.find do|f|
f.has_population? && f.agent == @prey && @current.size * @enemy_size > f.population
end
info "hunting down #{hunted_prey.agent}"
hunted_prey
else
info "prey #{prey} lost or too big"
nil
end
end
end
def been_attacked?
each_event do |event|
if event.instance_of? AttackedEvent
unless @enemies.include? event.attacker
@enemies << event.attacker
info "keep in mind enemy #{event.attacker}"
end
end
end
end
def myfield? field
field.x == 0 && field.y == 0
end
end
end