[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Perl Card Games



On Tue, Aug 03, 1999 at 06:20:57PM +0000, Bennett Todd wrote:
> That is serious fun; thanks for sharing!

No, thank *you* for sharing.

> It's most interesting seeing other ways of doing things. Shaking up your
> assumptions and so forth.

That's a pretty polite way of saying you hate my coding style :)

> For instance, your base representation is that a deck, or a hand, is an array
> of references to card objects, each with suit and value. It never would have
> occurred to me to structure things that way; I've always assumed that when
> programming a deck of cards would be modelled by an array of integers, and
> things like suits and values just show up in display.
> 
> But your approach is very pretty; I may try it next time. 

Ahem. Maybe I just didn't think of that. Or maybe I'm a gratuitous user of
OOP. But at least some of the OOP arguments make sense here. As you said,
it's definitely easier to read the code when you have real suits & names,
rather than 0..51 (also, I'd probably keep making off-by-one errors and
counting 1..52).

Anyway, the advantage of the Artistic licence is that you don't *have* to
try it that way next time; it's already been done for you.

[hours of back-breaking coding labor callously snipped]

> I've never seen a shuffle like yours before; yours is way sexier than many

A colleague of mine always used to get offended when I called coding sexy.
In fact, a fair definition of a hacker may be that they can call code sexy.
Would other readers of the list agree?  Unrelatedly, a Danish code developer
recently posted a message saying he had coded something but, "don't get
aroused yet. It still doesn't totally work." I think it was a
mistranslation, but maybe he's just a quintessential hacker.

> 	@shuffled = ();
> 	push @shuffled, splice @src, rand(@src), 1 while @src;

OK. How about 

%a = map { $_ => undef} (0..51);
@shuffled = @src[keys %a];

I guess that might make playing a second game rather boring, but it's, well,
*kind of* random. I had the severely evil idea of:

@shuffled = sort {rand - .5} @shuffled;

But the sort docs say:

    The comparison function is required to behave.  If it returns
    inconsistent results (sometimes saying $x[1] is less than $x[2] and
    sometimes saying the opposite, for example) the Perl interpreter will
    probably crash and dump core.

I guess dumping core doesn't usually qualify as FWP. I had a couple other
cute ideas, but they relied on getting 52 random integers from 1 to 52,
which isn't how rand works, unless you use a sort, which is what I'm doing
already.


Anyway, as far as the performance issue goes, I guess I had figured that I
can waste space with the OOP because we're dealing with decks, which are
small. A problem if you want to play 1000 simultaneous games of rummy, I
suppose. And I didn't worry about speed either, since I figure the slowest
part of the code will be either waiting for a user to do something. Or
perhaps the computer AI or fantastically rendered GUI that I'll be putting
on top of this, in order to create my new PerlCasino startup company. Maybe
if you help me code it up, I'll let you in on the ground floor for the IPO.

-Amir

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe