class PokerEngine::Cards
Constants
- COLOR_BY_FIRST_LETTER
Attributes
cards[R]
Public Class Methods
new(cards)
click to toggle source
# File lib/poker_engine/cards.rb, line 24 def initialize(cards) @cards = cards end
parse(str_cards)
click to toggle source
# File lib/poker_engine/cards.rb, line 12 def self.parse(str_cards) cards = str_cards .split(',') .map do |str| Card.new str.to_i, COLOR_BY_FIRST_LETTER.fetch(str[-1]) end new(cards) end
Public Instance Methods
+(other)
click to toggle source
# File lib/poker_engine/cards.rb, line 36 def +(other) Cards.new(cards + other.cards) end
combination(x)
click to toggle source
# File lib/poker_engine/cards.rb, line 49 def combination(x) cards.combination(x).map { |c| Cards.new(c) } end
each(&block)
click to toggle source
# File lib/poker_engine/cards.rb, line 32 def each(&block) cards.each(&block) end
sort()
click to toggle source
TODO: Make it work with block, too
# File lib/poker_engine/cards.rb, line 41 def sort cards.sort_by(&:value) end
sorted_values()
click to toggle source
# File lib/poker_engine/cards.rb, line 45 def sorted_values cards.map(&:value).sort end
to_s()
click to toggle source
# File lib/poker_engine/cards.rb, line 28 def to_s cards.map(&:to_s).join(', ') end
values_desc_by_occurency()
click to toggle source
Make descending order primary by occurency and secondary by value
# File lib/poker_engine/cards.rb, line 54 def values_desc_by_occurency values = cards.map(&:value) values.sort do |a, b| coefficient_occurency = (values.count(a) <=> values.count(b)) coefficient_occurency.zero? ? -(a <=> b) : -coefficient_occurency end end