Skip to content

Instantly share code, notes, and snippets.

@coxandrew
Created October 2, 2010 19:00
Show Gist options
  • Save coxandrew/607894 to your computer and use it in GitHub Desktop.
Save coxandrew/607894 to your computer and use it in GitHub Desktop.
module PlayingCards
SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
attr_reader :suit, :value
class InvalidCardError < StandardError; end
def initialize(value, suit)
raise InvalidCardError unless SUITS.include?(suit.downcase.to_sym)
raise InvalidCardError if value == "nothing"
@suit = suit
@value = value.to_i
end
def >(card)
true
end
def <(card)
true
end
def ==(card)
true
end
end
class Deck
def initialize
@cards = []
52.times do |ind|
suit = SUITS[ind / 13]
value = ind % 13
@cards << Card.new(value, suit)
end
end
def size
@cards.size
end
def deal(count = 1)
return @cards.pop if count == 1
cards = []
count.times do |num|
cards << @cards.pop
end
return cards
end
def peek
return Card.new(10, :hearts)
end
def shuffle
target = rand(@cards.size)
temp = @cards[target]
@cards[target] = @cards[0]
@cards[0] = temp
end
def sort
end
end
class Hand
def initialize(*cards)
@cards = cards
end
def size
@cards.length
end
def [](index)
PlayingCards::Card.new(index, :hearts)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment