in reply to Re: 52 Perl Pickup
in thread 52 Perl Pickup

My previous post was a non-sequitur, sorry. It would be easier if the dealer wasn't colour-blind. We could factor in the backs.
#!/usr/bin/perl -w # Re-sorting a pile of four decks. Unfazed by missing cards. # Using tr to avoid the unfathomable card-sort map. # Mock names for easy sorting my @suit = qw(f g h i); my @rank = (2..9, qw(a b c d e)); my @pile= (); sub shuffle { for (my $i = $#_ ; $i > 0 ; $i--) { @_[$_, $i] = @_[$i, $_] for rand $i+1 } return @_; } sub readable { my $string = join(" ", @_); $string =~ tr/abcdefghi/TJQKACDHS/; return $string; } sub separate { my @cards = sort @_; my @box = (); my $deck = 0; my $prevcard = ""; for my $card (@cards) { if ($card ne $prevcard) {$deck = 0}; push @{$box[$deck++]}, $card; $prevcard = $card; } return @box; } for (1..4) { push (@pile, (map {$rank = $_; map {"$_$rank"} @suit} @rank)); } shuffle @pile; print "\nFour decks shuffled together\n"; print (readable @pile); my @decks = separate @pile; print "\n\nFour decks separated\n"; foreach (@decks) { print (readable @$_); print "\n"; }

Replies are listed 'Best First'.
Re^3: 52 Perl Pickup
by Upstairs (Novice) on Aug 27, 2007 at 11:59 UTC
    Which can be whittled down to a more useful PNB dealer which would normally be written to a file. I did it to feed the the GIB command-line bridge engine.
    #!/usr/bin/perl ## Dealing cards in PBN (Portable Bridge Notation) ## Using tr to avoid the awesome card-sort map ## mock names for easy sorting my @suit = qw(f g h i); my @rank = (2..9, qw(a b c d e)); my @seps = qw(g. h. i.); sub shuffle { for (my $i = $#_ ; $i > 0 ; $i--) { @_[$_, $i] = @_[$i, $_] for rand $i+1 } return @_; } ## reformats one hand at a time sub pbnFormat { push (@_, @seps) ; local $string = join(``, sort{$b cmp $a}@_); $string =~ tr/abcdefghi/TJQKA/d; return $string; } my @deck = shuffle(map{$rank=$_; map{"$_$rank"}@suit}@rank); for(1..4) { print pbnFormat splice @deck,0,13; print "\n"; }