forked from reedlaw/ruby-mmo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module IanTerrell | ||
HEALTH_BUFFER = 1.5 | ||
|
||
def move | ||
if i_can_be_killed? | ||
if those_who_can_kill_me.size == 1 && i_can_kill?(those_who_can_kill_me.first) | ||
[:attack, those_who_can_kill_me.first] | ||
else | ||
[:rest] | ||
end | ||
else | ||
if those_killable.empty? | ||
if stats[:health] > HEALTH_BUFFER * opponents_max_health | ||
[:attack, random_low_health_opponent] | ||
else | ||
[:rest] | ||
end | ||
else | ||
[:attack, those_killable.first] | ||
end | ||
end | ||
end | ||
|
||
def to_s | ||
"Ian Terrell" | ||
end | ||
|
||
protected | ||
def i_can_be_killed? | ||
opponents.any?{ |player| can_kill?(player, self) } | ||
end | ||
|
||
def i_can_kill?(defender) | ||
can_kill?(self, defender) | ||
end | ||
|
||
def expected_damage(aggressor, defender) | ||
aggressor.stats[:strength] - defender.stats[:defense]/2 | ||
end | ||
|
||
def can_kill?(aggressor, defender) | ||
expected_damage(aggressor, defender) > defender.stats[:health] | ||
end | ||
|
||
def opponents | ||
Game.world[:players].select{ |player| player != self} | ||
end | ||
|
||
def opponents_max_health | ||
opponents.map{ |player| player.stats[:health] }.max | ||
end | ||
|
||
def opponents_min_health | ||
opponents.map{ |player| player.stats[:health] }.min | ||
end | ||
|
||
def those_killable | ||
opponents.select{ |player| can_kill?(self, player) } | ||
end | ||
|
||
def those_who_can_kill_me | ||
opponents.select{ |player| can_kill?(player, self) } | ||
end | ||
|
||
def those_with_low_health | ||
lowest_health = opponents_min_health | ||
opponents.select{ |player| player.stats[:health] == lowest_health } | ||
end | ||
|
||
def random_low_health_opponent | ||
low_health_opponents = those_with_low_health | ||
low_health_opponents[rand(low_health_opponents.size)] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Shannon | ||
def to_s | ||
'A Tabby Cat' | ||
end | ||
|
||
def move | ||
@opponent = select_opponent | ||
|
||
advantage? ? [:attack, @opponent] : [:rest] | ||
end | ||
|
||
private | ||
|
||
def select_opponent | ||
rat? || weakest_player | ||
end | ||
|
||
def rat? | ||
Game.world[:players].select { |p| p.to_s == 'rat' }.first | ||
end | ||
|
||
def weakest_player | ||
players = Game.world[:players].select { |p| p != self } | ||
players.min { |a, b| a.stats[:health] <=> b.stats[:health] } | ||
end | ||
|
||
def advantage? | ||
self.stats[:health] > @opponent.stats[:health] | ||
end | ||
end |