[Lazarus] Last-minute 0.9.28 nominations

Alexander Klenin klenin at gmail.com
Fri Sep 25 03:45:19 CEST 2009


On Mon, 21 Sep 2009 23:15:44 -0400, waldo kitty wrote about [Lazarus]
exists "card game" library?:

> i'm working on converting a TP6 "card game" application to FPC (to
> start with) and then to Lazarus for the graphical side... what i'm
> looking for, so as to not try to "recreate the wheel", is a "card
> object" such that i can create X "decks" of objects of 52 items with
> Y shuffle(s) of those X decks and/or similar... i have a method of
> keeping up with the decks and play but i'm looking for how to draw
> the cards and also be able to draw them fast... [see aside]

Hi Mark,

I started thinking about this problem yesterday. It is actually quite
difficult to solve satisfactorily on a 32-bit or 64-bit machine. I did
some algebra (combinatorics, to be more precise) and immediately came
to the conclusion that you will need to use 256-bit arithmetic for a
statistically satisfactory solution. This is because the number of
distinct permutations ("shuffles") of a 52-card deck is 52!, which
evaluates to:

80658175170943878571660636856403766975289505440883277824000000000000

This number requires at least 226 bits for unsigned representation.

Many years ago I developed an algorithm to form permutations from
ordinal numbers within the permutation space. [I have not seen this
algorithm elsewhere, so it could well be original -- but it's nothing
special.] Using this algorithm, I was able to generate several hundred
shuffled decks per second, even on a 5-year-old Athlon MP box. This was
running 256-bit arithmetic on a 32-bit processor.

To do this, I used the GMP (Gnu Multiple Precision) library. Since I
do  not [yet] have any Pascal bindings for this library, my test code
is written in C. However, there are some arbitrary precision packages
in the contributions area of the FPC Web site, any of which could be a
viable alternative to GMP.

The alternative to using arbitrary precision arithmetic is to make
multiple passes through your random number generator, which is both
computationally more intensive and produces statistically less
satisfactory results than using a 256-bit random number generator.

> [aside: i ran the original DOS TP6 version along side the FPC windows
> version and the original version ran at one "lost hand" per second or
> so... the speed differences are hugely astounding... one item of
> contention may be that the DOS version uses "direct screen writes"
> and not "OS" screen writes...]

I'm not sure what you mean by "lost hand" in the above. Did you mean
that you had 4 aces and the machine drew a straight flush? ... :-))

> i need a way to select each card via mouse click or keyboard
> selection, add it to a "collection" that sums to one of a set of
> possible additions and then remove the selected cards to another card
> deck...

That is fairly easy to code in Lazarus classes.

> i have the base program written in basic CP850 characters and
> "colorations" but have nothing specific for drawing "graphical cards
> on a card playing table"...

For pretty pictures that are fairly portable, I would recommend SVG
(Scalable Vector Graphics), which are similar in philosophy, but rather
different in implementation, to Windows and OS/2 metafiles.

You will also find a "dingbat" font quite handy, as the suit logos can
be coded as characters from the dingbats in the font. On Windows, the
"Wingdings" TrueType font is ubiquitous; on systems that prefer
Postscript fonts (OS/2, MacOS, UNIX/Linux, etc.), the popular choice is
"Zapf Dingbats".

HTH
-- 
Regards,

Dave  [RLU #314465]
=======================================================================
david.w.noon at ntlworld.com (David W Noon)
=======================================================================




More information about the Lazarus mailing list