Class: Brigitte::Commands::Pile::AddCards

Inherits:
Object
  • Object
show all
Defined in:
lib/brigitte/commands/pile.rb

Overview

Command that evaluates and adds cards on pile

Class Method Summary collapse

Class Method Details

.process(player, cards, pile, removed_cards = []) ⇒ Object



10
11
12
13
14
15
16
17
# 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) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# 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