Skip to content

Instantly share code, notes, and snippets.

@Oreolek
Created April 17, 2014 02:43
Show Gist options
  • Save Oreolek/10949313 to your computer and use it in GitHub Desktop.
Save Oreolek/10949313 to your computer and use it in GitHub Desktop.
Rubywarrior epic intermediate AI solution
class Player
def decide_direction
if (@warrior.listen.empty?) then
@direction = @warrior.direction_of_stairs;
return;
end;
@warrior.listen.each do |neighbour|
@direction = @warrior.direction_of(neighbour);
next if (@warrior.feel(@direction).stairs?);
if (@ticking > 0) then
if (neighbour.ticking?) then
@direction_ticking = @direction;
break;
else
next
end
end
if (@enemies > 0) then # если рядом есть враги, не отвлекаться на дальних
next if (not @warrior.feel(@direction).enemy?);
end
return if (neighbour.enemy?);
return if (neighbour.captive? and is_safe?);
end
if (@ticking > 0) then
return if (@warrior.feel(@direction).empty?);
if (@enemies > 1) then
[:forward, :backward, :right, :left].each do |direction|
next if direction == @direction;
@direction = direction;
return if (@warrior.feel(@direction).enemy?);
end
else
return;
end
end
[:forward, :backward, :right, :left].each do |direction|
@direction = direction;
return if (@warrior.feel(@direction).empty? and not @warrior.feel(@direction).stairs?);
end
end
def decide_turn
# боец в безопасности - лечимся, но если только это необходимо
if (is_safe? and need_heal?) then
return @warrior.rest!;
end
if (@warrior.feel(@direction).enemy?) then
if (@ticking > 0 and @enemies > 1 and @direction != @direction_ticking) then
return @warrior.bind!(@direction);
end
if (@enemies > 1) then
return @warrior.bind!(@direction);
end
if (@captives_front == 0 and @warrior.look.first.enemy? and @warrior.look[1].enemy?) then
return @warrior.detonate! if (@enemies_total < 3 and @warrior.health > 4 );
return @warrior.detonate! if (@enemies == 1 and @warrior.health > 10 );
end
return @warrior.attack!(@direction);
end
if (@warrior.feel(@direction).captive? and (@ticking > 0 or @enemies == 0)) then
return @warrior.rescue!(@direction);
end
if (@warrior.feel(@direction).empty?) then
return @warrior.walk!(@direction);
end
end
def is_safe?
return false if (@warrior.health < @health or @enemies > 0);
return true;
end
def need_heal?
return true if (@ticking == 0 and @warrior.health < @max_health);
return true if (@warrior.health < 15 and @enemies_total > 2);
return false;
end
def count_enemies()
@enemies = 0;
@enemies_front = 0;
@enemies_total = 0;
@captives = 0;
@captives_front = 0;
[:left, :right, :forward, :backward].each do |direction|
if (@warrior.feel(direction).enemy?) then
@enemies = @enemies + 1;
end;
if (@warrior.feel(direction).captive?) then
@captives = @captives + 1;
end
end
@warrior.listen.each do |neighbour|
if (neighbour.enemy?) then
@enemies_total = @enemies_total + 1;
end
end
@warrior.look.each do |space|
if (space.enemy?) then
@enemies_front = @enemies_front + 1;
end
if (space.captive?) then
@captives_front = @captives_front + 1;
end
end
end
def count_ticking()
@ticking = 0;
@warrior.listen.each do |neighbour|
if (neighbour.ticking?) then @ticking = @ticking + 1 end;
end
end
def play_turn(warrior)
@warrior = warrior
if (@max_health.nil?) then
@max_health = warrior.health;
@health = warrior.health;
else
if (@health > @max_health) then
@max_health = @health;
end
end
count_enemies();
count_ticking();
decide_direction();
decide_turn();
@health = warrior.health;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment