This repository has been archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturn_action.rb
160 lines (132 loc) · 3.39 KB
/
turn_action.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class TurnAction
def self.get name
if name == :handle_bomb
DefuseBomb
elsif name == :smart_heal
SmartHeal
elsif name == :neutralize_enemy
NeutralizeEnemy
elsif name == :handle_captive
HandleCaptive
end
end
def self.perform
new.perform
end
end
class DefuseBomb < TurnAction
def self.bomb= bomb
@bomb = bomb
end
def self.bomb?
not @bomb.empty?
end
def self.bomb
@bomb
end
def perform
if Warrior.current.feel(Warrior.current.direction_of self.class.bomb.first).enemy?
Warrior.current.neutralize_enemy
else
if Warrior.current.bombs_around_me.any?
Warrior.current.rescue!(Warrior.current.direction_of self.class.bomb.first)
else
search_for_bomb
end
end
end
private
def search_for_bomb
Warrior.current.walk!(Warrior.current.direction_of self.class.bomb.first)
end
end
class SmartHeal < TurnAction
def perform
if Warrior.current.im_in_safe_place?
Warrior.current.rest!
else
Warrior.current.escape
end
end
end
class NeutralizeEnemy < TurnAction
def self.enemy_list= enemy_list
@enemy_list = enemy_list
end
def self.any_enemies?
not @enemy_list.empty?
end
def self.enemy_list
@enemy_list
end
def perform
if Warrior.current.healthy?
if Warrior.current.surrounded?
bind_enemy
else
if Warrior.current.enemies_around_me.any?
hit_first_closest_enemy
else
search_for_enemy
end
end
else
Warrior.current.smart_heal
end
end
private
def search_for_enemy
where_to_go = Warrior.current.direction_of self.class.enemy_list.first
#the direction i must go is the same as the stairs
where_to_go = first_empty_direction if Directions::NAMES.select{|direction| Warrior.current.feel(direction).stairs?}.first == Warrior.current.direction_of_stairs
#choose a random direction that is not a "stair" neither a wall
Warrior.current.walk!(where_to_go)
end
def bind_enemy
Warrior.current.bind!(Warrior.current.enemies_around_me.first)
end
def first_empty_direction
Directions::NAMES.select do |direction|
not Warrior.current.feel(direction).wall? and not Warrior.current.feel(direction).stairs? and not Warrior.current.feel(direction).enemy? and not Warrior.current.feel(direction).captive?
end.first
end
def enemy_bound? space
space.captive?
end
def hit_first_closest_enemy
if Warrior.current.enemies_around_me(false).any?
Warrior.current.attack! Warrior.current.enemies_around_me(false).first
elsif Warrior.current.enemies_around_me.any?
Warrior.current.attack! Warrior.current.enemies_around_me.first
end
end
end
class HandleCaptive < TurnAction
def self.captive_list= captive_list
@captive_list = captive_list
end
def self.any_captives?
not @captive_list.empty?
end
def self.captive_list
@captive_list
end
def perform
if self.class.any_captives?
if Warrior.current.captives_around_me.any?
rescue_closest_captive
else
search_for_captive
end
else
Warrior.current.walk_to_stairs
end
end
private
def search_for_captive
Warrior.current.walk!(Warrior.current.direction_of self.class.captive_list.first)
end
def rescue_closest_captive
Warrior.current.rescue!(Warrior.current.captives_around_me.first)
end
end