Skip to content

Commit 209ff82

Browse files
author
slxh
committed
Merge remote-tracking branch 'upstream/master'
2 parents d725da6 + c8b812a commit 209ff82

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

players/ian_terrell.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

players/shannon.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Shannon
2+
def to_s
3+
'A Tabby Cat'
4+
end
5+
6+
def move
7+
@opponent = select_opponent
8+
9+
advantage? ? [:attack, @opponent] : [:rest]
10+
end
11+
12+
private
13+
14+
def select_opponent
15+
rat? || weakest_player
16+
end
17+
18+
def rat?
19+
Game.world[:players].select { |p| p.to_s == 'rat' }.first
20+
end
21+
22+
def weakest_player
23+
players = Game.world[:players].select { |p| p != self }
24+
players.min { |a, b| a.stats[:health] <=> b.stats[:health] }
25+
end
26+
27+
def advantage?
28+
self.stats[:health] > @opponent.stats[:health]
29+
end
30+
end

0 commit comments

Comments
 (0)