-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarrior.rb
78 lines (68 loc) · 1.64 KB
/
warrior.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
class Player
def initialize
@health = 20
@turn = 1
@already_in_first_step = false
end
def play_turn(warrior)
if warrior.feel.wall?
warrior.pivot!
@turn -= 1
elsif at_first_step_in_first_turn?(warrior) || @already_in_first_step
go_forward(warrior)
elsif !@already_in_first_step
go_backward(warrior)
end
@health = warrior.health
@turn += 1
end
def go_forward warrior
if has_enemy_in_three_step?(warrior.look)
warrior.shoot!
elsif is_attacked?(warrior.health) && need_back?(warrior.health)
warrior.walk!(:backward)
elsif !is_attacked?(warrior.health) && need_health?(warrior.health) && warrior.feel.empty?
warrior.rest!
elsif warrior.feel.empty?
warrior.walk!
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.attack!
end
end
def go_backward warrior
if warrior.feel(:backward).empty?
warrior.walk!(:backward)
elsif warrior.feel(:backward).captive?
warrior.rescue!(:backward)
elsif warrior.feel(:backward).wall?
@already_in_first_step = true
go_forward warrior
end
end
def need_health? health
health < 20
end
def is_attacked? health
health < @health
end
def at_first_step_in_first_turn? warrior
if @turn == 1 && warrior.feel(:backward).wall?
@already_in_first_step = true
return true
else
return false
end
end
def need_back? health
health < 10
end
def has_enemy_in_three_step? spaces
spaces.each do |space|
return false if space.captive?
return true if space.enemy?
end
false
end
end