Module: Fifthcard

Defined in:
lib/fifthcard.rb,
lib/fifthcard/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.decode(hand) ⇒ Object



20
21
22
23
24
25
# File 'lib/fifthcard.rb', line 20

def self.decode(hand)
  base_card = hand.cards.first
  counting_cards = hand.cards[1..-1]
  diff = sort_cards(counting_cards).permutation.to_a.index(counting_cards) + 1
  base_card + diff
end

.encode(hand) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fifthcard.rb', line 5

def self.encode(hand)
  by_suit = hand.group_by(&:suit)
  big_suit, suited_cards = by_suit.find{|suit, cards| cards.size > 1}
  base_card, target_card = suited_cards.sort.each_cons(2).find do |low, high|
    high.to_i - low.to_i <= 6
  end
  counting_cards = sort_cards(hand.cards - [target_card, base_card])
  diff = target_card.to_i - base_card.to_i
  Hand.new.tap do |encoding|
    [base_card, *counting_cards.permutation.to_a[diff - 1]].each do |card|
      encoding << card
    end
  end
end

.sort_cards(cards) ⇒ Object



27
28
29
# File 'lib/fifthcard.rb', line 27

def self.sort_cards(cards)
  cards.sort_by {|card| [card.to_i, card.suit] }
end