class Brigitte::Commands::Pile::AddCards

Command that evaluates and adds cards on pile

Public Class Methods

process(player, cards, pile, removed_cards = []) click to toggle source
# File lib/brigitte/commands/pile.rb, line 10
def process(player, cards, pile, removed_cards = [])
  return false unless valid?(player, cards, pile)

  pile.push(*cards.dup.map { |c| player.hand.delete(c) })
  removed_cards.push(*pile.pop(pile.count)) if clear_pile?(pile)

  true
end
valid?(player, cards, pile) click to toggle source
# File lib/brigitte/commands/pile.rb, line 19
def valid?(player, cards, pile)
  # player has cards
  return false unless (cards - player.hand).empty?
  # all cards are equal
  return false unless cards.uniq(&:weight).count == 1
  return true if pile.empty?
  # wild cards
  return true if [2, 10].include?(cards.first.weight)

  can_put_on_card?(cards.first, pile.last)
end

Private Class Methods

can_put_on_card?(top_card, down_card) click to toggle source
# File lib/brigitte/commands/pile.rb, line 33
def can_put_on_card?(top_card, down_card)
  operator = down_card.weight == 7 ? '<=' : '>='
  top_card.weight.send(operator, down_card.weight)
end
clear_pile?(pile) click to toggle source
# File lib/brigitte/commands/pile.rb, line 38
def clear_pile?(pile)
  return true if pile.last.weight == 10
  return false unless pile.count >= 4

  pile.last(4).uniq(&:weight).count == 1
end