srand();
$msg = "メタ文字:dragonD&D[]//\d\w()あとD6と3D6とD6+10と3d6+10と1+d4と6+3d6と1+d4+1と2+3d6+4です";
$msg = "メタ文字:dragonD&D[]//\d\w()あとD6と3D6とD6+10と3d6+10と1+d4と6+3d6と1+d4+1と2+3d6+4です";
$msg =~ s/\+/@/g;
while($msg =~ /(\d*@?\d*d\d+@?\d*)/){
$msg = &dice_roll($msg);
}
$msg =~ s/@/\+/g;
$msg =~ s/DDD/d/g;
$msg =~ s/DDD/d/g;
print $msg."\n";
#print
"x= $x , y= $y , z= $z \n , roll= $roll";
sub dice_roll{
local($msg)=@_; local($w,$x,$y,$z,$d,$roll,$tot);
$msg =~ /(\d*@?\d*d\d+@?\d*)/; $dcode=$1;
$x = $1 if $dcode =~ /(\d+)d/; $x = 1 if $dcode !~ /(\d+)d/;
$dcode =~ /d(\d+)/; $y = $1;
$z = 0; $z = $1 if $dcode =~ /@(\d+)$/;
$w = 0; $w = $1 if $dcode =~ /^(\d+)@[d\d]/;
$roll = $dcode." <strong>="; $roll .= " ".$w." @" if $w != 0; $roll .= " \["; $roll =~ s/d/DDD/; $tot = 0;
for(1..$x){
$d = int(rand()*$y+1);
$roll .= " ".$d if $tot == 0;
$roll .= " @ ".$d if $tot != 0;
$tot += $d;
}
$roll .= " \]"; $roll .= " @ ".$z if $z != 0; $tot += $w; $tot += $z; $roll .= " = $tot<\/strong>";
$msg =~ s/(\d*@?\d*d\d+@?\d*)/$roll/;
return($msg);
}