[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fixnum playing cards

Douglas Livingstone

4/13/2005 6:39:00 PM

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}."



5 Answers

gabriele renzi

4/13/2005 6:59:00 PM

0

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.



Jacob Fugal

4/13/2005 9:01:00 PM

0

On 4/13/05, Douglas Livingstone <rampant@gmail.com> wrote:
> [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



Martin DeMello

4/14/2005 5:18:00 AM

0

Jacob Fugal <lukfugl@gmail.com> wrote:
>
> 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

Eliah Hecht

4/15/2005 1:39:00 AM

0

On 4/13/05, Martin DeMello <martindemello@yahoo.com> wrote:
> (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.



Adelle Hartley

4/15/2005 2:32:00 AM

0

> On 4/13/05, Martin DeMello <martindemello@yahoo.com> wrote:
> > (are there red and black jokers? thought the two were identical)

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.