One of the things I like most about Damian Conway's books is his use of clear, resonant, verisimilar code examples. I try to emulate his writing when I answer questions on PerlMonks.
The other day, in a reply to another monk's inquiry, I recommended he add a control break to his program. I decided afterwards to write an example Perl script to demonstrate simple control break processing. Dealing straight poker hands seemed like a good, familiar real-world process to use as the basis of the example.
#!perl
use strict;
use warnings;
use utf8;
use open qw( :encoding(UTF-8) :std );
use List::Util qw( shuffle );
@ARGV == 1 or die "Usage: perl $0 <total rounds>\n";
my $TOTAL_ROUNDS = shift;
my @players = qw( Bob Carol Ted Alice );
my $player_to_dealers_right = $players[-1]; # Alice
my $round = 1;
my %hand_of;
my @deck;
my @cards;
for my $rank (qw( 2 3 4 5 6 7 8 9 10 J Q K A )) {
for my $suit (qw( ♠ ♥ ♦ ♣ )) {
push @cards, $rank . $suit;
}
}
ROUND:
while ($round <= $TOTAL_ROUNDS) {
@deck = shuffle(@cards);
SHUFFLE:
while (@deck) {
PLAYER:
for my $player (@players) {
push @{ $hand_of{$player} }, pop @deck;
}
if (@{ $hand_of{$player_to_dealers_right} } == 5) {
print "Round $round\n";
for my $player (@players) {
my $hand = join ' ', @{ $hand_of{$player} };
print "$player has $hand\n";
}
print "\n";
%hand_of = ();
last ROUND if ++$round > $TOTAL_ROUNDS;
}
}
}
__END__
Round 1
Bob has 7♥ 8♠ Q♥ 2♦ 9♥
Carol has 6♦ Q♠ 2♠ A♠ 3♦
Ted has K♥ 10♦ 5♦ 7♠ A♥
Alice has J♠ 10♥ 6♣ 6♥ K♠
Round 2
Bob has A♣ A♦ 9♣ 4♣ 2♥
Carol has 7♣ 4♦ 4♥ 8♥ 8♣
Ted has 6♠ J♣ K♦ 3♣ 9♠
Alice has 9♦ 7♦ Q♦ 8♦ 10♣
Round 3
Bob has 5♠ K♣ 3♠ 7♦ 7♥
Carol has 5♣ Q♣ 2♣ 9♣ Q♦
Ted has 3♥ 4♠ 10♠ J♦ 5♥
Alice has J♦ 5♥ J♥ A♠ 10♠
Round 4
Bob has K♣ J♥ 7♠ 4♠ J♠
Carol has 10♦ 9♦ Q♥ 9♥ 6♥
Ted has 10♣ 4♦ 4♣ 3♣ K♥
Alice has A♣ 5♦ 2♦ 3♦ 8♣
Round 5
Bob has 5♣ 2♣ 6♠ K♦ 2♠
Carol has A♦ 5♠ 6♣ 10♥ 4♥
Ted has 2♥ 9♠ A♥ Q♠ 7♣
Alice has 3♥ Q♣ K♠ 8♦ 8♠
Round 6
Bob has J♣ 2♠ J♥ 9♥ 6♠
Carol has 8♥ 10♣ 9♣ 5♣ 4♦
Ted has 3♠ K♥ K♣ A♦ Q♣
Alice has 6♦ 9♠ 3♥ 8♦ 9♦
Round 7
Bob has Q♥ 8♥ Q♦ 5♥ 3♣
Carol has 6♥ 10♥ 5♦ 7♣ 2♦
Ted has 10♦ 4♠ 4♥ 8♠ 7♦
Alice has J♠ 7♥ J♦ K♦ 2♣
Round 8
Bob has J♣ Q♠ 5♠ A♠ Q♠
Carol has 6♣ 8♣ A♥ A♣ 5♦
Ted has 10♠ 3♦ 3♠ 4♣ 6♥
Alice has 2♥ 6♦ K♠ 7♠ 7♠
Round 9
Bob has 3♥ 9♥ 8♦ 2♦ 7♣
Carol has 10♠ 6♦ 2♣ 4♣ J♠
Ted has 5♣ 4♥ 4♠ 3♠ 3♣
Alice has J♦ 7♦ 2♠ 8♥ Q♣
Round 10
Bob has 7♥ 10♦ J♥ A♣ 8♣
Carol has 9♦ 5♥ K♠ A♥ K♥
Ted has 5♠ 8♠ 4♦ 9♣ 2♥
Alice has A♦ 9♠ 6♠ 3♦ J♣
I don't play poker myself, so I got some of the poker lingo from the Wikipedia Poker article (for example, "round").
It occurred to me only after I had written the script that perhaps a computer program to deal straight poker hands isn't the best, most typical example of control break processing. What do you think?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Example of Control Break Processing
by ww (Archbishop) on Sep 10, 2011 at 10:45 UTC | |
by Jim (Curate) on Sep 10, 2011 at 15:14 UTC | |
|
Re: Perl Example of Control Break Processing
by zentara (Cardinal) on Sep 10, 2011 at 12:39 UTC | |
by Jim (Curate) on Sep 10, 2011 at 14:56 UTC |