#!/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)); } }