in reply to Dice Roll Probabilities in Risk

I thought you'd be interested to see how much it condenses when you generalize it a bit. Instead of lose_one/two/three, there's @lose. Instead of three dice routines, a parameterized dice routine. More things could be turned into loops, but this covered the most glaring ones.
#!/usr/bin/perl use strict; use warnings; # # Everything here is very brute-force and hackish # as befits a combat-related program # sub probability_to_beat { my @defender_roll = sort { $b <=> $a } @{ +shift }; my @attackers = @_; my @lose = (0) x 3; # none, one, two foreach my $attacker_roll (@attackers) { my @attacker_roll = sort { $b <=> $a } @$attacker_roll; my $lost = 0; for (0..(@$attacker_roll > @defender_roll ? $#defender_roll : $#$attacker_roll)) { ++$lost if ($defender_roll[$_] >= $attacker_roll[$_]); } ++$lose[$lost]; } return @lose; } sub dice { my $n = shift; return ($n == 0) ? [[]] : [map { my $first = $_; map [@$first, $_], 1..6 } @{dice($n-1)}] ; } sub run { my @attackers = @{ +shift }; my @defenders = @{ +shift }; my $num_attackers = scalar @{ $attackers[0] }; my $num_defenders = scalar @{ $defenders[0] }; my @total_lose = (0) x 3; foreach my $defender_roll (@defenders) { my @lose = probability_to_beat( $defender_roll, @attackers ); my $total = $lose[0] + $lose[1] + $lose[2]; print join( ', ' => @$defender_roll) . "\t" . sprintf("%2.2f%%, %2.2f%%, %2.2f%%", ($lose[0] / $total) * 100, ($lose[1] / $total) * 100, ($lose[2] / $total) * 100, ) . "\n"; $total_lose[0] += $lose[0]; $total_lose[1] += $lose[1]; $total_lose[2] += $lose[2]; } my $total = $total_lose[0] + $total_lose[1] + $total_lose[2]; print "Totals:\t" . sprintf("%2.2f%%, %2.2f%%, %2.2f%%", ($total_lose[0] / $total) * 100, ($total_lose[1] / $total) * 100, ($total_lose[2] / $total) * 100, ) . "\n"; } for my $a_dice (3,2,1) { for my $d_dice (2,1) { print "$a_dice v $d_dice\n"; run(dice($a_dice), dice($d_dice)); } }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Dice Roll Probabilities in Risk
by hardburn (Abbot) on May 18, 2005 at 18:38 UTC

    Thanks. I was trying to get my head around generalizing the dice-creation subroutines, but I couldn't think of how to do it. I figured a brute-force would work just as well, so I went with that.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.