Class: Card

Inherits:
Object
  • Object
show all
Defined in:
lib/acpc_poker_types/card.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rank, suit) ⇒ Card

Returns a new instance of Card.



53
54
55
56
# File 'lib/acpc_poker_types/card.rb', line 53

def initialize(rank, suit)
  @rank = Rank.new rank
  @suit = Suit.new suit
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



30
31
32
# File 'lib/acpc_poker_types/card.rb', line 30

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



30
31
32
# File 'lib/acpc_poker_types/card.rb', line 30

def suit
  @suit
end

Class Method Details

.acpc_card_number(rank, suit) ⇒ Integer

Returns The numeric ACPC representation of the card.

Returns:

  • (Integer)

    The numeric ACPC representation of the card.



26
27
28
# File 'lib/acpc_poker_types/card.rb', line 26

def self.acpc_card_number(rank, suit)
  rank.to_i * Suit::DOMAIN.length + suit.to_i
end

.cards(acpc_string_of_cards) ⇒ Array<Card>

Parameters:

  • acpc_string_of_cards (String)

    A string of cards in ACPC format

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/acpc_poker_types/card.rb', line 12

def self.cards(acpc_string_of_cards)
  all_ranks = Rank::DOMAIN.map do |rank, rank_properties|
   rank_properties[:acpc_character]
  end.join
  all_suits = Suit::DOMAIN.map do |suit, suit_properties| 
    suit_properties[:acpc_character]
  end.join

  acpc_string_of_cards.scan(/[#{all_ranks}][#{all_suits}]/).inject([]) do |pile, acpc_card|
    pile.push << Card.from_acpc(acpc_card)
  end
end

.from_acpc(acpc_card) ⇒ Object

Returns Card.

Returns:

  • Card



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/acpc_poker_types/card.rb', line 33

def self.from_acpc(acpc_card)
  all_ranks = Rank::DOMAIN.values.map do |card_rank|
    card_rank[:acpc_character]
  end.join
  all_suits = Suit::DOMAIN.values.map do |card_suit| 
    card_suit[:acpc_character] 
  end.join

  if acpc_card.match(/([#{all_ranks}])([#{all_suits}])/)
    rank = $1
    suit = $2

    Card.from_components rank, suit
  else
    raise UnableToParseAcpcCard, acpc_card
  end
end

Instance Method Details

#to_acpcObject



68
69
70
# File 'lib/acpc_poker_types/card.rb', line 68

def to_acpc
  @rank.to_acpc + @suit.to_acpc
end

#to_iObject



58
59
60
# File 'lib/acpc_poker_types/card.rb', line 58

def to_i
  Card.acpc_card_number(@rank, @suit)
end

#to_strObject Also known as: to_s



62
63
64
# File 'lib/acpc_poker_types/card.rb', line 62

def to_str
  @rank.to_s + @suit.to_s
end