-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.rb
More file actions
51 lines (38 loc) · 863 Bytes
/
environment.rb
File metadata and controls
51 lines (38 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Environment
def initialize(warrior)
@warrior = warrior
end
def archer_backward?
space = detect_closest_space(:backward)
unit = space && space.unit
unit && unit.kind_of?(RubyWarrior::Units::Archer)
end
def enemy_forward?
closest_space = detect_closest_space
closest_space && closest_space.enemy?
end
def wall_forward?
warrior.feel.wall?
end
def nothing_forward?
warrior.feel.empty?
end
def safe_attack?
no_captives_detected? && enemy_detected?
end
def captive_forward?
warrior.feel.captive?
end
private
attr_reader :warrior
def detect_closest_space(direction = :forward)
look = warrior.look(direction)
look.detect { |space| !space.empty? }
end
def no_captives_detected?
!warrior.look.any?(&:captive?)
end
def enemy_detected?
warrior.look.any?(&:enemy?)
end
end