RosettaCodeData/Task/Playing-cards/Ruby/playing-cards.rb

52 lines
976 B
Ruby
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
class Card
2015-11-18 06:14:39 +00:00
# class constants
SUITS = %i[ Clubs Hearts Spades Diamonds ]
PIPS = %i[ 2 3 4 5 6 7 8 9 10 Jack Queen King Ace ]
# class variables (private)
@@suit_value = Hash[ SUITS.each_with_index.to_a ]
@@pip_value = Hash[ PIPS.each_with_index.to_a ]
attr_reader :pip, :suit
def initialize(pip,suit)
@pip = pip
@suit = suit
end
def to_s
"#{@pip} #{@suit}"
end
# allow sorting an array of Cards: first by suit, then by value
def <=>(other)
(@@suit_value[@suit] <=> @@suit_value[other.suit]).nonzero? or
@@pip_value[@pip] <=> @@pip_value[other.pip]
end
2013-04-10 23:57:08 -07:00
end
class Deck
2015-11-18 06:14:39 +00:00
def initialize
@deck = Card::SUITS.product(Card::PIPS).map{|suit,pip| Card.new(pip,suit)}
end
def to_s
2016-12-05 22:15:40 +01:00
@deck.inspect
2015-11-18 06:14:39 +00:00
end
def shuffle!
@deck.shuffle!
self
end
def deal(*args)
@deck.shift(*args)
end
2013-04-10 23:57:08 -07:00
end
deck = Deck.new.shuffle!
puts card = deck.deal
hand = deck.deal(5)
puts hand.join(", ")
puts hand.sort.join(", ")