我正在尝试导入一些使用不同骰子卷文本的数据。例如,你会有2-4,2-12,5-8,2-5。我正在尝试写一个翻译器,将他们转换为适当的骰子卷。到目前为止,我将翻译它,但我最终得到了一些奇怪的滚动。例如,在5-8上,它会得到5d1+3,而在2-8这样的其他东西上,它会给我一个合理的2d4。我在努力想办法让它总是用“真正的”骰子。
目前我正在使用这个perl脚本。您只需将您想要修复的骰子字符串传递给它,它就会返回它认为正确的掷骰子。我的问题是我不能想出如何将骰子“边”限制在这2,3,4,6,8,12,20边。
# turn 2-7 into 1d6+1 or 2-8 into 2d4
sub fix_DiceRolls {
my($diceRoll) = @_;
use POSIX;
if ($diceRoll =~ /^(\d+)(\-)(\d+)\b/i) {
# 2-5
#Group 1. 0-1 `2`
#Group 2. 1-2 `-`
#Group 3. 2-3 `5`
my($count) = $1;
my($size) = $3;
if ($count == 1) {
$diceRoll = "$1d$3";
} else {
my ($newSize) = $size/$count;
my ($remainder) = $size % $count;
my ($round_remainder) = ceil($remainder);
my ($round_newSize) = floor($newSize);
if ($remainder == 0) {
$diceRoll = $count."d".$newSize;
} else {
$diceRoll = $count."d".$round_newSize."+".$round_remainder;
}
}
}
return $diceRoll;
}
发布于 2017-02-15 09:42:43
以下内容可能会对您有所帮助。它不知道如何做1d6/2
,但它正确地将4-19
转换为3d6+1 | 5d4-1
。
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
my @valid_dice = (4, 6, 8, 10, 12, 20);
sub fix_dice_roll {
my ($dice_roll) = @_;
my ($from, $to) = split /-/, $dice_roll;
my $range = $to - $from + 1;
my @translations;
for my $dice (@valid_dice) {
if (0 == ($range - 1) % ($dice - 1)) {
my $times = ($range - 1) / ($dice - 1);
my $plus = sprintf '%+d', $to - $times * $dice;
$plus = q() if '+0' eq $plus;
push @translations, [ $dice, $times, $plus ];
}
}
@translations = sort { $a->[1] <=> $b->[1] } @translations;
return map "$_->[1]d$_->[0]$_->[2]", @translations;
}
say join ' | ', fix_dice_roll($_) while <>;
https://stackoverflow.com/questions/42238151
复制相似问题