class Shithead::AI::Offer

Constants

EMPTY_SET

Attributes

player[R]
top[R]

Public Class Methods

call(player, top) click to toggle source
# File lib/shithead/ai/offer.rb, line 4
def self.call(player, top)
  new(player, top).call
end
new(player, top) click to toggle source
# File lib/shithead/ai/offer.rb, line 8
def initialize(player, top)
  @player = player
  @top    = top
end

Public Instance Methods

call() click to toggle source
# File lib/shithead/ai/offer.rb, line 13
def call
  draw_from_hidden if empty_hand?
  return EMPTY_SET if lowest_possible.nil?

  available.take lowest_possible
end

Private Instance Methods

available() click to toggle source
# File lib/shithead/ai/offer.rb, line 24
def available
  @available ||= player.hand.empty? ? player.visible : player.hand
end
draw_from_hidden() click to toggle source
# File lib/shithead/ai/offer.rb, line 28
def draw_from_hidden
  player.hidden.shuffle!
  player.add player.hidden.shift
end
empty_hand?() click to toggle source
# File lib/shithead/ai/offer.rb, line 33
def empty_hand?
  player.hand.empty? && player.visible.empty?
end
lowest_possible() click to toggle source
# File lib/shithead/ai/offer.rb, line 41
def lowest_possible
  @lowest_possible ||= if play_lowest?
    ordered.first
  else
    ordered.detect { |set| set.rank <= top.rank }
  end
end
ordered() click to toggle source
# File lib/shithead/ai/offer.rb, line 49
def ordered
  @ordered ||= available.sets.sort_by(&:rank).reverse
end
play_lowest?() click to toggle source
# File lib/shithead/ai/offer.rb, line 37
def play_lowest?
  top.nil? || top.value == "2"
end