1
+ module IanTerrell
2
+ HEALTH_BUFFER = 1.5
3
+
4
+ def move
5
+ if i_can_be_killed?
6
+ if those_who_can_kill_me . size == 1 && i_can_kill? ( those_who_can_kill_me . first )
7
+ [ :attack , those_who_can_kill_me . first ]
8
+ else
9
+ [ :rest ]
10
+ end
11
+ else
12
+ if those_killable . empty?
13
+ if stats [ :health ] > HEALTH_BUFFER * opponents_max_health
14
+ [ :attack , random_low_health_opponent ]
15
+ else
16
+ [ :rest ]
17
+ end
18
+ else
19
+ [ :attack , those_killable . first ]
20
+ end
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ "Ian Terrell"
26
+ end
27
+
28
+ protected
29
+ def i_can_be_killed?
30
+ opponents . any? { |player | can_kill? ( player , self ) }
31
+ end
32
+
33
+ def i_can_kill? ( defender )
34
+ can_kill? ( self , defender )
35
+ end
36
+
37
+ def expected_damage ( aggressor , defender )
38
+ aggressor . stats [ :strength ] - defender . stats [ :defense ] /2
39
+ end
40
+
41
+ def can_kill? ( aggressor , defender )
42
+ expected_damage ( aggressor , defender ) > defender . stats [ :health ]
43
+ end
44
+
45
+ def opponents
46
+ Game . world [ :players ] . select { |player | player != self }
47
+ end
48
+
49
+ def opponents_max_health
50
+ opponents . map { |player | player . stats [ :health ] } . max
51
+ end
52
+
53
+ def opponents_min_health
54
+ opponents . map { |player | player . stats [ :health ] } . min
55
+ end
56
+
57
+ def those_killable
58
+ opponents . select { |player | can_kill? ( self , player ) }
59
+ end
60
+
61
+ def those_who_can_kill_me
62
+ opponents . select { |player | can_kill? ( player , self ) }
63
+ end
64
+
65
+ def those_with_low_health
66
+ lowest_health = opponents_min_health
67
+ opponents . select { |player | player . stats [ :health ] == lowest_health }
68
+ end
69
+
70
+ def random_low_health_opponent
71
+ low_health_opponents = those_with_low_health
72
+ low_health_opponents [ rand ( low_health_opponents . size ) ]
73
+ end
74
+ end
0 commit comments