Created
April 15, 2011 19:13
-
-
Save danramteke/922278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PlayingCards | |
class Card | |
attr_accessor :rank, :suit | |
def initialize(a_rank, a_suit) | |
@rank = parse_rank a_rank | |
@suit = parse_suit a_suit | |
end | |
def > other | |
@rank > other.rank | |
end | |
def < other | |
@rank < other.rank | |
end | |
def == other | |
@rank == other.rank | |
end | |
private | |
def parse_rank a_rank | |
case a_rank | |
when Fixnum | |
return a_rank | |
when String | |
if (a_rank == "K") | |
return 13 | |
elsif (a_rank.to_s == "Q") | |
return 12 | |
elsif (a_rank.to_s == "J") | |
return 11 | |
elsif (a_rank.to_i < 11 && a_rank.to_i > 0) | |
return a_rank.to_i | |
else | |
raise InvalidCardError | |
end | |
end | |
end | |
def parse_suit a_suit | |
new_suit = a_suit.to_s.downcase.to_sym | |
raise InvalidCardError unless [:hearts, :clubs, :diamonds, :spades].include? new_suit | |
return new_suit | |
end | |
class InvalidCardError < Exception | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'card' | |
module PlayingCards | |
class Deck | |
def initialize | |
@cards = [1,2,3,4,5,6,7,8,9,10,11,12,13].collect do |r| | |
[:spades, :hearts, :clubs, :diamonds].collect do |s| | |
PlayingCards::Card.new(r,s) | |
end | |
end.flatten | |
end | |
def size | |
@cards.size | |
end | |
def deal number_of_cards=1 | |
return @cards.pop if number_of_cards == 1 | |
result = [] | |
number_of_cards.times do | |
result << @cards.pop | |
end | |
result | |
end | |
def peek | |
@cards.last | |
end | |
def shuffle | |
end | |
def sort | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment