[DGD] File access question

Felix A. Croes felix at dworkin.nl
Sat Jul 14 04:00:00 CEST 2007


"Kurt Nordstrom" <kurt at blar.net> wrote:

> I've got a situation that I'm trying to figure out the best way to
> approach.  I was wondering if maybe any of you could give your thoughts on
> how you might tackle it.
>
> I'm wanting to code a simulated card game, with maybe 1000 unique cards
> (they'll have various words on them), but only 200 of which will be used
> in a given game.  So, essentially it will need to read a file of 1000
> entries, pick out 200 by random (which it will store in memory), and then
> close the file.

It sounds like it would be easiest to have a file format with one line
for each card, read the whole file in one go, explode the string by
newline, and then work on the resulting array:

    file = read_file("/path/to/cardsfile");
    cards = explode(file, "\n");

After that, pick 200 random ones using the method suggested by Mikael
Lind.  I think that the tick cost would not be excessive.  Sometimes,
the simplest solution is best.

Another separator could be used if multiple lines per card are desired.

Regards,
Dworkin



More information about the DGD mailing list