Fixnum playing cards

  • Thread starter Douglas Livingstone
  • Start date
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}."
 
G

gabriele renzi

Douglas Livingstone ha scritto:

just a thing: you don't need to convert 1..54 to an array before you use
sort_by, sort_by is available in Ranges too.
 
J

Jacob Fugal

[1] Code listing:

module PlayingCards

def black?
self > 26
end

# ...snip...
def face
# ...snip...
return case self
when 53; :Red
when 54; :Black
end unless self > 54
end

# ...snip...
end

It looks like 53 (Red Joker) would be classified as black (53.black?
== true) since 53 > 26. Maybe change to:

def black?
self > 26 and self != 53
end

Also, if we're defining black? why not define red? as well:

def red?
not black?
end

Jacob Fugal
 
M

Martin DeMello

Jacob Fugal said:
It looks like 53 (Red Joker) would be classified as black (53.black?
== true) since 53 > 26. Maybe change to:

def black?
self > 26 and self != 53
end

Or even neater, change the range from 0..53, and make it [joker, cards,
joker]

(are there red and black jokers? thought the two were identical)

martin
 
E

Eliah Hecht

(are there red and black jokers? thought the two were identical)

There are big and little jokers, at least, that are differentiated for
the purposes of certain games which are escaping me at the moment.
-Eliah.
 
A

Adelle Hartley

I've had some packs of cards that had red and black jokers. I've never
differentiated between them in any games except bartok, but they're still
red and black.

Adelle.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top