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

Re: [FWP] APL vs. Perl. A query from a lazy programmer.



John Porter wrote:
> 
> Keith Calvert Ivey wrote:
> >
> > But I still don't think the method of distributing cards is
> > right. Like Rick Delaney, you're going through one suit at a
> > time and distributing each card to a random player.  You've
> > added a check to make sure you no player gets more than 13
> > cards, so the difference from a real deal is more subtle, but
> > it's still there.
> 
> Indeed, that approach is fundamentally flawed.

I do not see how it "fundamentally" flawed.  Admittedly, my answer was
stupid since it doesn't come close to allowing a real game of bridge,
but a simple loop around it should fix it.

Expanding a bit:

@s=qw/S H D C/;
{
    @n=@x=();
    for$s(@s){
        $_='AKQJT98765432';
        for$i(4,3,2,1){
            my$c;
            s/./rand$i>1?$&:($c.=$&,$n[$i-1]++,"")/ge;
            ($j,$f)=$i%2?($i,"%15s:%s"):(2,"%s:%-25s");
            $x[$j]{$s}.=sprintf$f,$s,$c
        }
    }
    redo if grep$_!=13,@n;
}
$,=$\="\n";
print@{$_}{@s}for@x;

Now, this isn't an efficient algorithm, nor does it win at golf, but it
seems accurate to me.  The main chunk in the middle randomly distributes
52 cards across 4 hands.  The redo line rejects all of those
distributions where all the hands do not have 13 cards.

-- 
Rick Delaney
rick.delaney@home.com

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