D
Douglas Livingstone
I've been playing with ruby's playinc cards,and to make life easy my
cards are 1..54, and I've been adding things to Fixnum directly [1].
I've not used a language which lets me redefine numbers before, so not
sure how much trouble I can get my self into
Is this sane?
Douglas
[1] Code listing:
module PlayingCards
def black?
self > 26
end
def suit
case self
when 01..13; :hearts
when 14..26; :dimonds
when 27..39; :clubs
when 40..52; :spades
when 53..54; :joker
end
end
def face
return case n = (self - 1) % 13 + 1
when 2..10; n.to_s.to_sym
when 01; :Ace
when 11; :Jack
when 12; :Queen
when 13; :King
end unless self > 52
return case self
when 53; :Red
when 54; :Black
end unless self > 54
end
def card_name
return "#{face} of #{suit}" unless self > 52
return "#{face} #{suit}" unless self > 54
end
end
class Fixnum
include PlayingCards
end
cards = (1..54).to_a.sort_by{rand}
puts cards.collect{|card| card.card_name}
puts "\nYou've got card! It is the #{cards.pop.card_name}."
cards are 1..54, and I've been adding things to Fixnum directly [1].
I've not used a language which lets me redefine numbers before, so not
sure how much trouble I can get my self into
Is this sane?
Douglas
[1] Code listing:
module PlayingCards
def black?
self > 26
end
def suit
case self
when 01..13; :hearts
when 14..26; :dimonds
when 27..39; :clubs
when 40..52; :spades
when 53..54; :joker
end
end
def face
return case n = (self - 1) % 13 + 1
when 2..10; n.to_s.to_sym
when 01; :Ace
when 11; :Jack
when 12; :Queen
when 13; :King
end unless self > 52
return case self
when 53; :Red
when 54; :Black
end unless self > 54
end
def card_name
return "#{face} of #{suit}" unless self > 52
return "#{face} #{suit}" unless self > 54
end
end
class Fixnum
include PlayingCards
end
cards = (1..54).to_a.sort_by{rand}
puts cards.collect{|card| card.card_name}
puts "\nYou've got card! It is the #{cards.pop.card_name}."