[Wed, Aug 04, 1999 at 09:53:45AM -0400] Amir Karger: > > HtAAoF! > > Um, what's that? Think introduction to the Camel. > [much snippage] > > A couple minor points: > > > my %cards = ( > > 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, > > 8 => 8, 9 => 9, J => 10, Q => 11, K => 12, A => 13, > > ); > > Oops! What about "10"? Otherwise your deck has only 48 or 50 cards. Youch! Well: 8 => 8, 9 => 9, 10 => 10, J => 11, Q => 12, K => 13, A => 13, > > sub compare_cards { my ($a, $b) = map { /.(.)/ } @_; > > $cards{$a} <=> $cards{$b} } > > Once you add 10, which is two characters, this doesn't work :( So... Change the regex to /.(.+)/ > > sub score { my $c; $c += $cards{ (/.(.)/)[0] } for @_; $c } Same with this one. > Nice idea for resolving a tie. Another option would be a fistfight. And for my next program: perlfistfight! :) I just realized this no longer applies, though. This was from an earlier version where I had each player go through the deck exactly once. Since the winner now obtains the entire deck, this is useless. Hmmm, maybe it should be left in for ties. > > while ( @deck ) { > > push @a, splice(@deck, rand @deck, 1); > > push @b, splice(@deck, rand @deck, 1); > > } > > This neatly steps around the shuffling in place discussion. Of course, it > won't work for rummy, say. True. And, in that case, the original module would probably work a lot better. I'm not too happy with the solution, as it involves two redundant statements. Perhaps: while ( @deck ) { for my $to ( \@a, \@b ) { push @{$to}, splice(@deck, rand @deck, 1); } } While it isn't much of an improvement over the two pushslices, if you added another deck it would scale much more nicely. OH! ... if ( @a < 4 ) { printf " : | %sA has too few cards, giving B [@a]\n", ' 'x4x($depth+1); push @b, @b, splice @a; ++$b_wins; } elsif ( @b < 4 ) { printf " : | %sB has too few cards, giving A [@b]\n", ' 'x4x($depth+1); push @a, @a, splice @b; ++$a_wins; } else { ... DOH! Make that push @b, @pot, splice @a; push @a, @pot, splice @b; Patch attached. > -Amir --Matthew -- 1a2,3 > # 8/3/1999 - Initial version, sent to 'FWP' <fwp@technofile.org> > # 8/4/1999 - First revision, fixes several bugs. 6,7c8,10 < 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, < 8 => 8, 9 => 9, J => 10, Q => 11, K => 12, A => 13, --- > 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, > 8 => 8, 9 => 9, J => 11, Q => 12, K => 13, A => 14, > 10 => 10 13c16 < sub compare_cards { my ($a, $b) = map { /.(.)/ } @_; --- > sub compare_cards { my ($a, $b) = map { /.(.+)/ } @_; 15c18 < sub score { my $c; $c += $cards{ (/.(.)/)[0] } for @_; $c } --- > sub score { my $c; $c += $cards{ (/.(.+)/)[0] } for @_; $c } 24,25c27,29 < push @a, splice(@deck, rand @deck, 1); < push @b, splice(@deck, rand @deck, 1); --- > for my $to ( \@a, \@b ) { > push @{$to}, splice(@deck, rand @deck, 1); > } 35c39 < printf "%-6d: %2s = %2s | %sTHIS MEANS WAR\n", $rounds, $a, $b, ' 'x4x$depth; --- > printf "%-6d: %-3s = %3s | %sTHIS MEANS WAR\n", $rounds, $a, $b, ' 'x4x$depth; 38,39c42,43 < printf " : | %sA has too few cards, giving B [@a]\n", ' 'x4x($depth+1); < push @b, @b, splice @a; --- > printf " : | %sA has too few cards, giving B [@a]\n", ' 'x4x($depth+1); > push @b, @pot, splice @a; 42,43c46,47 < printf " : | %sB has too few cards, giving A [@b]\n", ' 'x4x($depth+1); < push @a, @a, splice @b; --- > printf " : | %sB has too few cards, giving A [@b]\n", ' 'x4x($depth+1); > push @a, @pot, splice @b; 51c55 < printf "%-6d: %2s > %2s | %sA wins [@pot]\n", $rounds, $a, $b, ' 'x4x$depth; --- > printf "%-6d: %-3s > %3s | %sA wins [@pot]\n", $rounds, $a, $b, ' 'x4x$depth; 55c59 < printf "%-6d: %2s < %2s | %sB wins [@pot]\n", $rounds, $a, $b, ' 'x4x$depth; --- > printf "%-6d: %-3s < %3s | %sB wins [@pot]\n", $rounds, $a, $b, ' 'x4x$depth; 63c67,69 < print "Game considered a tie after 10_000 rounds.\n"; --- > print "\nGame considered a tie after 10_000 rounds.\n"; > printf "Side A had: %s cards, with a score of %d.\n", scalar @a, score(@a); > printf "Side B had: %s cards, with a score of %d.\n", scalar @b, score(@b); 71c77 < printf "Round : A ~ B |\n"; --- > printf "Round : A ~ B |\n"; 81,83d86 < print "Side A has: ", scalar @a, " cards, with a score of ", score(@a), "\n" if @a; < print "Side B has: ", scalar @b, " cards, with a score of ", score(@b), "\n" if @b; < ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe