Skip to content
This repository
tree: 54cc2b577a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 90 lines (67 sloc) 1.478 kb
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
module Kabutomushi
  def to_s
    'Kabutomushi'
  end

  def move
    return kill_rat if rats_are_alive?
    return deliver_killing_blow if d6 > 4 && someone_is_killable?
    return rest if people_are_passive?
    return attack strongest_player if healthy?
    rest
  end

  def horde
    true
  end

  private

  # actions

  def attack(player)
    [:attack, player]
  end

  def rest
    [:rest]
  end

  def kill_rat
    attack find_player('rat')
  end

  def deliver_killing_blow
    attack killable_opponents.first
  end

  # conditions

  def rats_are_alive?
    find_player 'rat'
  end

  def someone_is_killable?
    killable_opponents.any?
  end

  def healthy?
    stats[:health] == 100
  end

  def players_are_careful?
    weakest_player.stats[:health] > 70
  end

  # information

  def d6
    rand(6) + 1
  end

  def weakest_player
    players.min { |a, b| a.stats[:health] <=> b.stats[:health] }
  end

  def strongest_player
    players.max { |a, b| a.stats[:experience] <=> b.stats[:experience] }
  end

  def killable_opponents
    players.select { |player| killable?(player) }
  end

  def potential_damage(player)
    stats[:strength] - player.stats[:defense]/2
  end

  def killable?(player)
    player.stats[:health] <= potential_damage(player)
  end

  def find_player(player_name)
    players.select { |player| player.to_s == player_name }.first
  end

  def players
    Game.world[:players].select { |player| player != self && player.alive }
  end
end
Something went wrong with that request. Please try again.