评估骰子滚动符号字符串

发布于 2024-07-25 04:03:34 字数 985 浏览 8 评论 0原文

规则

编写一个接受字符串作为参数的函数,返回 骰子符号中表达式的计算值, 包括加法和乘法。

为了澄清问题,这里是合法表达式的 EBNF 定义:

roll ::= [positive integer], "d", positive integer
entity ::= roll | positive number
expression ::= entity { [, whitespace], "+"|"*"[, whitespace], entity }

示例输入:

  • "3d6 + 12"
  • "4*d12 + 3"
  • "d100"

不禁止使用 eval 函数或类似函数,但我鼓励 不使用这些来解决。 欢迎重新进入。

我无法提供测试用例,因为输出应该是随机的;)。

设置答案标题的格式:语言、n 个字符(重要说明 - 无需评估等)


我的 ruby 解决方案,92 81 个字符,使用 eval:

def f s
eval s.gsub(/(\d+)?d(\d+)/i){eval"a+=rand $2.to_i;"*a=($1||1).to_i}
end

另一个 ruby 解决方案,不短(92 个字符),但我发现它很有趣 - 它仍然使用 eval,但这次以相当有创意的方式。

class Fixnum
def**b
eval"a+=rand b;"*a=self
end
end
def f s
eval s.gsub(/d/,'**')
end

Rules

Write a function that accepts string as a parameter, returning
evaluated value of expression in dice notation,
including addition and multiplication.

To clear the things up, here comes EBNF definition of legal expressions:

roll ::= [positive integer], "d", positive integer
entity ::= roll | positive number
expression ::= entity { [, whitespace], "+"|"*"[, whitespace], entity }

Example inputs:

  • "3d6 + 12"
  • "4*d12 + 3"
  • "d100"

Using eval functions, or similar, is not forbidden, but I encourage
to solving without using these. Re-entrancy is welcome.

I cannot provide test-cases, as output should be random ;).

Format your answers' titles: language, n characters (important notes — no eval, etc.)


My ruby solution, 92 81 characters, using eval:

def f s
eval s.gsub(/(\d+)?d(\d+)/i){eval"a+=rand $2.to_i;"*a=($1||1).to_i}
end

Another ruby solution, not shorter (92 characters), but I find it interesting — it still uses eval but this time in quite creative way.

class Fixnum
def**b
eval"a+=rand b;"*a=self
end
end
def f s
eval s.gsub(/d/,'**')
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

寒尘 2024-08-01 04:03:35

Python 带 eval 时为 124 个字符,不带 eval 时为 154 个字符。

只是为了表明 python 不具备可读性,这里有一个 124 个字符的解决方案,与原始方案具有类似的基于 eval 的方法:

import random,re
f=lambda s:eval(re.sub(r'(\d*)d(\d+)',lambda m:int(m.group(1)or 1)*('+random.randint(1,%s)'%m.group(2)),s))

[编辑] 这是一个没有 eval 的 154 个字符的解决方案:

import random,re
f=lambda s:sum(int(c or 0)+sum(random.randint(1,int(b))for i in[0]*int(a or 1))for a,b,c in re.findall(r'(\d*)d(\d+)(\s*[+-]\s*\d+)?',s))

注意:两者都适用于“2d6 + 1d3 + 5”等输入,但不支持更高级的变体,如“2d3d6”或负骰子(“1d6-4”可以,但“1d6-2d4”不行)(您可以删除 2 个字符,以便在第二个字符中根本不支持负数)

Python 124 chars with eval, 154 without.

Just to show python doesn't have to be readable, here's a 124 character solution, with a similar eval-based approach to the original:

import random,re
f=lambda s:eval(re.sub(r'(\d*)d(\d+)',lambda m:int(m.group(1)or 1)*('+random.randint(1,%s)'%m.group(2)),s))

[Edit] And here's a 154 character one without eval:

import random,re
f=lambda s:sum(int(c or 0)+sum(random.randint(1,int(b))for i in[0]*int(a or 1))for a,b,c in re.findall(r'(\d*)d(\d+)(\s*[+-]\s*\d+)?',s))

Note: both will work for inputs like "2d6 + 1d3 + 5" but don't support more advanced variants like "2d3d6" or negative dice ("1d6-4" is OK, but "1d6-2d4" isn't) (You can shave off 2 characters to not support negative numbers at all in the second one instead)

季末如歌 2024-08-01 04:03:35

python,模糊版本有 197 个字符。

可读版本:369 个字符。 没有评估,直接解析。

import random
def dice(s):
    return sum(term(x) for x in s.split('+'))
def term(t):
    p = t.split('*')
    return factor(p[0]) if len(p)==1 else factor(p[0])*factor(p[1])
def factor(f):
    p = f.split('d')
    if len(p)==1:
        return int(f)
    return sum(random.randint(1, int(g[1]) if g[1] else 6) for \
               i in range(int(g[0]) if g[0] else 1))

压缩版本:258个字符,单字符名称,滥用条件表达式,逻辑表达式中的快捷方式:

import random
def d(s):
 return sum(t(x.split('*')) for x in s.split('+'))
def t(p):
 return f(p[0])*f(p[1]) if p[1:] else f(p[0])
def f(s):
 g = s.split('d')
 return sum(random.randint(1, int(g[1] or 6)) for i in range(int(g[0] or 1))) if g[1:] else int(s)

模糊版本:216个字符,使用reduce,大量映射以避免“def”,“return”。

import random
def d(s):
 return sum(map(lambda t:reduce(lambda x,y:x*y,map(lambda f:reduce(lambda x,y:sum(random.randint(1,int(y or 6)) for i in range(int(x or 1))), f.split('d')+[1]),t.split('*')),1),s.split('+')))

最新版本:197 个字符,折叠在 @Brain 的评论中,添加了测试运行。

import random
R=reduce;D=lambda s:sum(map(lambda t:R(int.__mul__,map(lambda f:R(lambda x,y:sum(random.randint(1,int(y or 6))for i in[0]*int(x or 1)),f.split('d')+[1]),t.split('*'))),s.split('+')))

测试:

>>> for dice_expr in ["3d6 + 12", "4*d12 + 3","3d+12", "43d29d16d21*9+d7d9*91+2*d24*7"]: print dice_expr, ": ", list(D(dice_expr) for i in range(10))
... 
3d6 + 12 :  [22, 21, 22, 27, 21, 22, 25, 19, 22, 25]
4*d12 + 3 :  [7, 39, 23, 35, 23, 23, 35, 27, 23, 7]
3d+12 :  [16, 25, 21, 25, 20, 18, 27, 18, 27, 25]
43d29d16d21*9+d7d9*91+2*d24*7 :  [571338, 550124, 539370, 578099, 496948, 525259, 527563, 546459, 615556, 588495]

该解决方案无法处理没有相邻数字的空格。 所以“43d29d16d21 * 9 + d7d9 * 91 + 2 * d24 * 7”可以工作,但“43d29d16d21 * 9 + d7d9 * 91 + 2 * d24 * 7”不会,因为第二个空格(在“ +”和“之间d”)。 可以通过首先从 s 中删除空格来纠正它,但这将使代码长度超过 200 个字符,所以我将保留该错误。

python, 197 chars in obscured version.

Readable version: 369 chars. No eval, straight forward parsing.

import random
def dice(s):
    return sum(term(x) for x in s.split('+'))
def term(t):
    p = t.split('*')
    return factor(p[0]) if len(p)==1 else factor(p[0])*factor(p[1])
def factor(f):
    p = f.split('d')
    if len(p)==1:
        return int(f)
    return sum(random.randint(1, int(g[1]) if g[1] else 6) for \
               i in range(int(g[0]) if g[0] else 1))

compressed version: 258 chars, single character names, abused conditional expressions, shortcut in logical expression:

import random
def d(s):
 return sum(t(x.split('*')) for x in s.split('+'))
def t(p):
 return f(p[0])*f(p[1]) if p[1:] else f(p[0])
def f(s):
 g = s.split('d')
 return sum(random.randint(1, int(g[1] or 6)) for i in range(int(g[0] or 1))) if g[1:] else int(s)

obscured version: 216 chars, using reduce, map heavily to avoid "def", "return".

import random
def d(s):
 return sum(map(lambda t:reduce(lambda x,y:x*y,map(lambda f:reduce(lambda x,y:sum(random.randint(1,int(y or 6)) for i in range(int(x or 1))), f.split('d')+[1]),t.split('*')),1),s.split('+')))

Last version: 197 chars, folded in @Brain's comments, added testing runs.

import random
R=reduce;D=lambda s:sum(map(lambda t:R(int.__mul__,map(lambda f:R(lambda x,y:sum(random.randint(1,int(y or 6))for i in[0]*int(x or 1)),f.split('d')+[1]),t.split('*'))),s.split('+')))

Tests:

>>> for dice_expr in ["3d6 + 12", "4*d12 + 3","3d+12", "43d29d16d21*9+d7d9*91+2*d24*7"]: print dice_expr, ": ", list(D(dice_expr) for i in range(10))
... 
3d6 + 12 :  [22, 21, 22, 27, 21, 22, 25, 19, 22, 25]
4*d12 + 3 :  [7, 39, 23, 35, 23, 23, 35, 27, 23, 7]
3d+12 :  [16, 25, 21, 25, 20, 18, 27, 18, 27, 25]
43d29d16d21*9+d7d9*91+2*d24*7 :  [571338, 550124, 539370, 578099, 496948, 525259, 527563, 546459, 615556, 588495]

This solution can't handle whitespaces without adjacent digits. so "43d29d16d21*9+d7d9*91+2*d24*7" will work, but "43d29d16d21*9 + d7d9*91 + 2*d24*7" will not, due to the second space (between "+" and "d"). It can be corrected by first removing whitespaces from s, but this will make the code longer than 200 chars, so I'll keep the bug.

无悔心 2024-08-01 04:03:35

perl,无评估,144个字符,多次工作,支持多次掷骰子

sub e{($c=pop)=~y/+* /PT/d;$b='(\d+)';map{$a=0while$c=~s!$b?$_$b!$d=$1||1;$a+=1+int rand$2for 1..$d;$e=$2;/d/?$a:/P/?$d+$e:$d*$e!e}qw(d T P);$c}

扩展版本,注释

sub f {
    ($c = pop); #assign first function argument to $c
    $c =~ tr/+* /PT/d;  #replace + and * so we won't have to escape them later.
                        #also remove spaces
    #for each of 'd','T' and 'P', assign to $_ and run the following
    map {
        #repeatedly replace in $c the first instance of <number> <operator> <number> with
        #the result of the code in second part of regex, capturing both numbers and
        #setting $a to zero after every iteration
        $a=0 while $c =~ s[(\d+)?$_(\d+)][
            $d = $1 || 1;   #save first parameter (or 1 if not defined) as later regex 
                            #will overwrite it
            #roll $d dice, sum in $a
            for (1..$d)
            {
                $a += 1 + int rand $2;
            }
            $e = $2;        #save second parameter, following regexes will overwrite
            #Code blocks return the value of their last statement
            if (/d/)
            {
                $a; #calculated dice throw
            }
            elsif (/P/)
            {
                $d + $e;
            }
            else
            {
                $d * $e;
            }
        ]e;
    } qw(d T P);
    return $c;
}

编辑清理,更新解释到最新版本

perl, no evals, 144 chars, works multiple times, supports multiple dice rolls

sub e{($c=pop)=~y/+* /PT/d;$b='(\d+)';map{$a=0while$c=~s!$b?$_$b!$d=$1||1;$a+=1+int rand$2for 1..$d;$e=$2;/d/?$a:/P/?$d+$e:$d*$e!e}qw(d T P);$c}

Expanded version, with comments

sub f {
    ($c = pop); #assign first function argument to $c
    $c =~ tr/+* /PT/d;  #replace + and * so we won't have to escape them later.
                        #also remove spaces
    #for each of 'd','T' and 'P', assign to $_ and run the following
    map {
        #repeatedly replace in $c the first instance of <number> <operator> <number> with
        #the result of the code in second part of regex, capturing both numbers and
        #setting $a to zero after every iteration
        $a=0 while $c =~ s[(\d+)?$_(\d+)][
            $d = $1 || 1;   #save first parameter (or 1 if not defined) as later regex 
                            #will overwrite it
            #roll $d dice, sum in $a
            for (1..$d)
            {
                $a += 1 + int rand $2;
            }
            $e = $2;        #save second parameter, following regexes will overwrite
            #Code blocks return the value of their last statement
            if (/d/)
            {
                $a; #calculated dice throw
            }
            elsif (/P/)
            {
                $d + $e;
            }
            else
            {
                $d * $e;
            }
        ]e;
    } qw(d T P);
    return $c;
}

EDIT cleaned up, updated explanation to latest version

丢了幸福的猪 2024-08-01 04:03:35

Ruby,166 个字符,无 eval

在我看来相当优雅;)。

def g s,o=%w{\+ \* d}
o[a=0]?s[/#{p=o.pop}/]?g(s.sub(/(\d+)?\s*(#{p})\s*(\d+)/i){c=$3.to_i
o[1]?($1||1).to_i.times{a+=rand c}+a:$1.to_i.send($2,c)},o<<p):g(s,o):s
end

去混淆版本+评论:

def evaluate(string, opers = ["\\+","\\*","d"])
  if opers.empty?
    string
  else
    if string.scan(opers.last[/.$/]).empty? # check if string contains last element of opers array

      # Proceed to next operator from opers array.

      opers.pop
      evaluate(string, opers)

    else # string contains that character...

      # This is hard to deobfuscate. It substitutes subexpression with highest priority with
      # its value (e.g. chooses random value for XdY, or counts value of N+M or N*M), and
      # calls recursively evaluate with substituted string.

      evaluate(string.sub(/(\d+)?\s*(#{opers.last})\s*(\d+)/i) { a,c=0,$3.to_i; ($2 == 'd') ? ($1||1).to_i.times{a+=rand c}+a : $1.to_i.send($2,c) }, opers)

    end
  end
end

Ruby, 166 characters, no eval

In my opinion quite elegant ;).

def g s,o=%w{\+ \* d}
o[a=0]?s[/#{p=o.pop}/]?g(s.sub(/(\d+)?\s*(#{p})\s*(\d+)/i){c=$3.to_i
o[1]?($1||1).to_i.times{a+=rand c}+a:$1.to_i.send($2,c)},o<<p):g(s,o):s
end

Deobfuscated version + comments:

def evaluate(string, opers = ["\\+","\\*","d"])
  if opers.empty?
    string
  else
    if string.scan(opers.last[/.$/]).empty? # check if string contains last element of opers array

      # Proceed to next operator from opers array.

      opers.pop
      evaluate(string, opers)

    else # string contains that character...

      # This is hard to deobfuscate. It substitutes subexpression with highest priority with
      # its value (e.g. chooses random value for XdY, or counts value of N+M or N*M), and
      # calls recursively evaluate with substituted string.

      evaluate(string.sub(/(\d+)?\s*(#{opers.last})\s*(\d+)/i) { a,c=0,$3.to_i; ($2 == 'd') ? ($1||1).to_i.times{a+=rand c}+a : $1.to_i.send($2,c) }, opers)

    end
  end
end
薔薇婲 2024-08-01 04:03:35

Python,压缩版本 452 字节

我不确定这是否很酷、丑陋或愚蠢,但编写它很有趣。

我们所做的如下:我们使用正则表达式(这通常不是处理此类事情的正确工具)将骰子符号字符串转换为小型基于堆栈的语言的命令列表。 该语言有四个命令:

  • mul 将堆栈顶部的两个数字相乘,并将结果压入
  • add 将堆栈顶部的两个数字相加,并将结果压入
  • roll 从堆栈中弹出骰子大小,然后进行计数,将大小面的骰子滚动计数次,并将结果压入
  • 一个数字,将其自身压入堆栈

然后计算此命令列表。

import re, random

def dice_eval(s):
    s = s.replace(" ","")
    s = re.sub(r"(\d+|[d+*])",r"\1 ",s) #seperate tokens by spaces
    s = re.sub(r"(^|[+*] )d",r"\g<1>1 d",s) #e.g. change d 6 to 1 d 6
    while "*" in s:
        s = re.sub(r"([^+]+) \* ([^+]+)",r"\1 \2mul ",s,1)
    while "+" in s:
        s = re.sub(r"(.+) \+ (.+)",r"\1 \2add ",s,1)
    s = re.sub(r"d (\d+) ",r"\1 roll ",s)

    stack = []

    for token in s.split():
        if token == "mul":
            stack.append(stack.pop() * stack.pop())
        elif token == "add":
            stack.append(stack.pop() + stack.pop())
        elif token == "roll":
            v = 0
            dice = stack.pop()
            for i in xrange(stack.pop()):
                v += random.randint(1,dice)
            stack.append(v)
        elif token.isdigit():
            stack.append(int(token))
        else:
            raise ValueError

    assert len(stack) == 1

    return stack.pop() 

print dice_eval("2*d12+3d20*3+d6")

顺便说一句(这已在问题评论中讨论过),此实现将允许像 "2d3d6" 这样的字符串,将其理解为“滚动 d3 两次,然后滚动 d6 的次数与两个卷。”

此外,尽管有一些错误检查,但它仍然需要有效的输入。 例如,传递“*4”将导致无限循环。

这是压缩版本(不太漂亮):

import re, random
r=re.sub
def e(s):
 s=r(" ","",s)
 s=r(r"(\d+|[d+*])",r"\1 ",s)
 s=r(r"(^|[+*] )d",r"\g<1>1 d",s)
 while"*"in s:s=r(r"([^+]+) \* ([^+]+)",r"\1 \2M ",s)
 while"+"in s:s=r(r"(.+) \+ (.+)",r"\1 \2A ",s)
 s=r(r"d (\d+)",r"\1 R",s)
 t=[]
 a=t.append
 p=t.pop
 for k in s.split():
  if k=="M":a(p()*p())
  elif k=="A":a(p()+p())
  elif k=="R":
   v=0
   d=p()
   for i in [[]]*p():v+=random.randint(1,d)
   a(v)
  else:a(int(k))
 return p()

Python, 452 bytes in the compressed version

I'm not sure if this is cool, ugly, or plain stupid, but it was fun writing it.

What we do is as follows: We use regexes (which is usually not the right tool for this kind of thing) to convert the dice notation string into a list of commands in a small, stack-based language. This language has four commands:

  • mul multiplies the top two numbers on the stack and pushes the result
  • add adds the top two numbers on the stack and pushes the result
  • roll pops the dice size from the stack, then the count, rolls a size-sided dice count times and pushes the result
  • a number just pushes itself onto the stack

This command list is then evaluated.

import re, random

def dice_eval(s):
    s = s.replace(" ","")
    s = re.sub(r"(\d+|[d+*])",r"\1 ",s) #seperate tokens by spaces
    s = re.sub(r"(^|[+*] )d",r"\g<1>1 d",s) #e.g. change d 6 to 1 d 6
    while "*" in s:
        s = re.sub(r"([^+]+) \* ([^+]+)",r"\1 \2mul ",s,1)
    while "+" in s:
        s = re.sub(r"(.+) \+ (.+)",r"\1 \2add ",s,1)
    s = re.sub(r"d (\d+) ",r"\1 roll ",s)

    stack = []

    for token in s.split():
        if token == "mul":
            stack.append(stack.pop() * stack.pop())
        elif token == "add":
            stack.append(stack.pop() + stack.pop())
        elif token == "roll":
            v = 0
            dice = stack.pop()
            for i in xrange(stack.pop()):
                v += random.randint(1,dice)
            stack.append(v)
        elif token.isdigit():
            stack.append(int(token))
        else:
            raise ValueError

    assert len(stack) == 1

    return stack.pop() 

print dice_eval("2*d12+3d20*3+d6")

By the way (this was discussed in the question comments), this implementation will allow strings like "2d3d6", understanding this as "roll a d3 twice, then roll a d6 as many times as the result of the two rolls."

Also, although there is some error checking, it still expects a valid input. Passing "*4" for example will result in an infinite loop.

Here is the compressed version (not pretty):

import re, random
r=re.sub
def e(s):
 s=r(" ","",s)
 s=r(r"(\d+|[d+*])",r"\1 ",s)
 s=r(r"(^|[+*] )d",r"\g<1>1 d",s)
 while"*"in s:s=r(r"([^+]+) \* ([^+]+)",r"\1 \2M ",s)
 while"+"in s:s=r(r"(.+) \+ (.+)",r"\1 \2A ",s)
 s=r(r"d (\d+)",r"\1 R",s)
 t=[]
 a=t.append
 p=t.pop
 for k in s.split():
  if k=="M":a(p()*p())
  elif k=="A":a(p()+p())
  elif k=="R":
   v=0
   d=p()
   for i in [[]]*p():v+=random.randint(1,d)
   a(v)
  else:a(int(k))
 return p()
时光沙漏 2024-08-01 04:03:35

Ruby,87 个字符,使用 eval

这是我的 Ruby 解决方案,部分基于 OP。 它短了五个字符,并且仅使用 eval 一次。

def f s
eval s.gsub(/(\d+)?[dD](\d+)/){n=$1?$1.to_i: 1;n.times{n+=rand $2.to_i};n}
end

代码的可读版本:

def f s
    eval (s.gsub /(\d+)?[dD](\d+)/ do
        n = $1 ? $1.to_i : 1
        n.times { n += rand $2.to_i }
        n
    end)
end

Ruby, 87 characters, uses eval

Here's my Ruby solution, partially based on the OP's. It's five characters shorter and only uses eval once.

def f s
eval s.gsub(/(\d+)?[dD](\d+)/){n=$1?$1.to_i: 1;n.times{n+=rand $2.to_i};n}
end

A readable version of the code:

def f s
    eval (s.gsub /(\d+)?[dD](\d+)/ do
        n = $1 ? $1.to_i : 1
        n.times { n += rand $2.to_i }
        n
    end)
end
睡美人的小仙女 2024-08-01 04:03:35

JAVASCRIPT,1399 个字符,没有评估

旧帖子,我知道。 但我尝试贡献

Roll = window.Roll || {};

Roll.range = function (str) {
    var rng_min, rng_max, str_split,
        delta, value;

    str = str.replace(/\s+/g, "");
    str_split = str.split("-");
    rng_min = str_split[0];
    rng_max = str_split[1];

    rng_min = parseInt(rng_min) || 0;
    rng_max = Math.max(parseInt(rng_max), rng_min) || rng_min;

    delta = (rng_max - rng_min + 1);

    value = Math.random() * delta;
    value = parseInt(value);

    return value + rng_min;
};

Roll.rollStr = function (str) {
    var check,
        qta, max, dice, mod_opts, mod,
        rng_min, rng_max,
        rolls = [], value = 0;

    str = str.replace(/\s+/g, "");
    check = str.match(/(?:^[-+]?(\d+)?(?:\/(\d+))?[dD](\d+)(?:([-+])(\d+)\b)?$|^(\d+)\-(\d+)$)/);

    if (check == null) {return "ERROR"}
    qta = check[1];
    max = check[2];
    dice = check[3];
    mod_opts = check[4];
    mod = check[5];
    rng_min = check[6];
    rng_max = check[7];
    check = check[0];

    if (rng_min && rng_max) {return Roll.range(str)}

    dice = parseInt(dice);
    mod_opts = mod_opts || "";
    mod = parseInt(mod) || 0;
    qta = parseInt(qta) || 1;
    max = Math.max(parseInt(max), qta) || qta;

    for (var val; max--;) {
        val = Math.random() * dice;
        val = Math.floor(val) + 1;
        rolls.push(val);
    }

    if (max != qta) {
        rolls.sort(function (a, b) {return a < b});
        rolls.unshift(rolls.splice(0, qta));
    }

    while (rolls[0][0]) {value += rolls[0].shift();}

    if (mod_opts == "-") {value -= mod;}
    else {value += mod;}

    return value
};

if (!window.diceRoll) {window.diceRoll= Roll.rollStr;}

它是单个骰子掷骰,例如“2d8+2”或“4-18”“3/4d6”(4 d6 中的最佳 3 个)

diceRoll("2d8+2"); 
diceRoll("4-18");
diceRoll("3/4d6");

来检查累积掷骰子,更好地循环输入字符串上的匹配结果,例如

r = "2d8+2+3/4d6"
r.match(/([-+])?(\d+)?(?:\/(\d+))?[dD](\d+)(?:([-+])(\d+)\b)?/g);
// => ["2d8+2", "+3/4d6"]
// a program can manage the "+" or "-" on the second one (usually is always an addiction)

JAVASCRIPT, 1399 chars, no eval

old post, i know. but i try to contribute

Roll = window.Roll || {};

Roll.range = function (str) {
    var rng_min, rng_max, str_split,
        delta, value;

    str = str.replace(/\s+/g, "");
    str_split = str.split("-");
    rng_min = str_split[0];
    rng_max = str_split[1];

    rng_min = parseInt(rng_min) || 0;
    rng_max = Math.max(parseInt(rng_max), rng_min) || rng_min;

    delta = (rng_max - rng_min + 1);

    value = Math.random() * delta;
    value = parseInt(value);

    return value + rng_min;
};

Roll.rollStr = function (str) {
    var check,
        qta, max, dice, mod_opts, mod,
        rng_min, rng_max,
        rolls = [], value = 0;

    str = str.replace(/\s+/g, "");
    check = str.match(/(?:^[-+]?(\d+)?(?:\/(\d+))?[dD](\d+)(?:([-+])(\d+)\b)?$|^(\d+)\-(\d+)$)/);

    if (check == null) {return "ERROR"}
    qta = check[1];
    max = check[2];
    dice = check[3];
    mod_opts = check[4];
    mod = check[5];
    rng_min = check[6];
    rng_max = check[7];
    check = check[0];

    if (rng_min && rng_max) {return Roll.range(str)}

    dice = parseInt(dice);
    mod_opts = mod_opts || "";
    mod = parseInt(mod) || 0;
    qta = parseInt(qta) || 1;
    max = Math.max(parseInt(max), qta) || qta;

    for (var val; max--;) {
        val = Math.random() * dice;
        val = Math.floor(val) + 1;
        rolls.push(val);
    }

    if (max != qta) {
        rolls.sort(function (a, b) {return a < b});
        rolls.unshift(rolls.splice(0, qta));
    }

    while (rolls[0][0]) {value += rolls[0].shift();}

    if (mod_opts == "-") {value -= mod;}
    else {value += mod;}

    return value
};

if (!window.diceRoll) {window.diceRoll= Roll.rollStr;}

it's a single dice roll, like "2d8+2" or "4-18" "3/4d6" (best 3 of 4 d6)

diceRoll("2d8+2"); 
diceRoll("4-18");
diceRoll("3/4d6");

to check cumulative rolls, better loop on matched result ove rthe input string like

r = "2d8+2+3/4d6"
r.match(/([-+])?(\d+)?(?:\/(\d+))?[dD](\d+)(?:([-+])(\d+)\b)?/g);
// => ["2d8+2", "+3/4d6"]
// a program can manage the "+" or "-" on the second one (usually is always an addiction)
无可置疑 2024-08-01 04:03:35

PHP,147 个符号,无评估:

preg_match('/(\d+)?d(\d+)[\s+]?([\+\*])?[\s+]?(\d+)?/',$i,$a);$d=rand(1,$a[2])*((!$a[1])?1:$a[1]);$e['+']=$d+$a[4];$e['*']=$d*$a[4];print$e[$a[3]];

$i 包含输入字符串。

编辑:哎呀,忘记了前缀操作。 brb。

PHP, 147 symbols, no eval:

preg_match('/(\d+)?d(\d+)[\s+]?([\+\*])?[\s+]?(\d+)?/',$i,$a);$d=rand(1,$a[2])*((!$a[1])?1:$a[1]);$e['+']=$d+$a[4];$e['*']=$d*$a[4];print$e[$a[3]];

$i contains input string.

Edit: oops, forgot about prefixed operation. brb.

葬シ愛 2024-08-01 04:03:34

C# 类。 它以递归方式计算加法和乘法,从左到右计算链式骰子

编辑:

  • 在每次调用时删除 .Replace(" ","")
  • 添加 .Trim() on int.TryParse
  • 现在所有工作都在单一方法中完成
  • 如果未指定骰子面数,则假设为 6(请参阅 Wiki 文章)
  • 重构冗余调用以解析左侧of "d"
  • 重构不必要的 if 语句

缩小:(411 字节)

class D{Random r=new Random();public int R(string s){int t=0;var a=s.Split('+');if(a.Count()>1)foreach(var b in a)t+=R(b);else{var m=a[0].Split('*');if(m.Count()>1){t=1;foreach(var n in m)t*=R(n);}else{var d=m[0].Split('d');if(!int.TryParse(d[0].Trim(),out t))t=0;int f;for(int i=1;i<d.Count();i++){if(!int.TryParse(d[i].Trim(),out f))f=6;int u=0;for(int j=0;j<(t== 0?1:t);j++)u+=r.Next(1,f);t+=u;}}}return t;}}

扩展形式:

    class D
    {
        /// <summary>Our Random object.  Make it a first-class citizen so that it produces truly *random* results</summary>
        Random r = new Random();

        /// <summary>Roll</summary>
        /// <param name="s">string to be evaluated</param>
        /// <returns>result of evaluated string</returns>
        public int R(string s)
        {
            int t = 0;

            // Addition is lowest order of precedence
            var a = s.Split('+');

            // Add results of each group
            if (a.Count() > 1)
                foreach (var b in a)
                    t += R(b);
            else
            {
                // Multiplication is next order of precedence
                var m = a[0].Split('*');

                // Multiply results of each group
                if (m.Count() > 1)
                {
                    t = 1; // So that we don't zero-out our results...

                    foreach (var n in m)
                        t *= R(n);
                }
                else
                {
                    // Die definition is our highest order of precedence
                    var d = m[0].Split('d');

                    // This operand will be our die count, static digits, or else something we don't understand
                    if (!int.TryParse(d[0].Trim(), out t))
                        t = 0;

                    int f;

                    // Multiple definitions ("2d6d8") iterate through left-to-right: (2d6)d8
                    for (int i = 1; i < d.Count(); i++)
                    {
                        // If we don't have a right side (face count), assume 6
                        if (!int.TryParse(d[i].Trim(), out f))
                            f = 6;

                        int u = 0;

                        // If we don't have a die count, use 1
                        for (int j = 0; j < (t == 0 ? 1 : t); j++)
                            u += r.Next(1, f);

                        t += u;
                    }
                }
            }

            return t;
        }
    }

测试用例:

    static void Main(string[] args)
    {
        var t = new List<string>();
        t.Add("2d6");
        t.Add("2d6d6");
        t.Add("2d8d6 + 4d12*3d20");
        t.Add("4d12");
        t.Add("4*d12");
        t.Add("4d"); // Rolls 4 d6

        D d = new D();
        foreach (var s in t)
            Console.WriteLine(string.Format("{0}\t{1}", d.R(s), s));
    }

C# class. It evaluates recursively for addition and multiplication, left-to-right for chained die rolls

Edits:

  • Removed .Replace(" ","") on each call
  • Added .Trim() on int.TryParse instead
  • All work is now done in single method
  • If die face count is not specified, assumes 6 (see Wiki article)
  • Refactored redundant call to parse left side of "d"
  • Refactored unnecessary if statement

Minified: (411 bytes)

class D{Random r=new Random();public int R(string s){int t=0;var a=s.Split('+');if(a.Count()>1)foreach(var b in a)t+=R(b);else{var m=a[0].Split('*');if(m.Count()>1){t=1;foreach(var n in m)t*=R(n);}else{var d=m[0].Split('d');if(!int.TryParse(d[0].Trim(),out t))t=0;int f;for(int i=1;i<d.Count();i++){if(!int.TryParse(d[i].Trim(),out f))f=6;int u=0;for(int j=0;j<(t== 0?1:t);j++)u+=r.Next(1,f);t+=u;}}}return t;}}

Expanded form:

    class D
    {
        /// <summary>Our Random object.  Make it a first-class citizen so that it produces truly *random* results</summary>
        Random r = new Random();

        /// <summary>Roll</summary>
        /// <param name="s">string to be evaluated</param>
        /// <returns>result of evaluated string</returns>
        public int R(string s)
        {
            int t = 0;

            // Addition is lowest order of precedence
            var a = s.Split('+');

            // Add results of each group
            if (a.Count() > 1)
                foreach (var b in a)
                    t += R(b);
            else
            {
                // Multiplication is next order of precedence
                var m = a[0].Split('*');

                // Multiply results of each group
                if (m.Count() > 1)
                {
                    t = 1; // So that we don't zero-out our results...

                    foreach (var n in m)
                        t *= R(n);
                }
                else
                {
                    // Die definition is our highest order of precedence
                    var d = m[0].Split('d');

                    // This operand will be our die count, static digits, or else something we don't understand
                    if (!int.TryParse(d[0].Trim(), out t))
                        t = 0;

                    int f;

                    // Multiple definitions ("2d6d8") iterate through left-to-right: (2d6)d8
                    for (int i = 1; i < d.Count(); i++)
                    {
                        // If we don't have a right side (face count), assume 6
                        if (!int.TryParse(d[i].Trim(), out f))
                            f = 6;

                        int u = 0;

                        // If we don't have a die count, use 1
                        for (int j = 0; j < (t == 0 ? 1 : t); j++)
                            u += r.Next(1, f);

                        t += u;
                    }
                }
            }

            return t;
        }
    }

Test cases:

    static void Main(string[] args)
    {
        var t = new List<string>();
        t.Add("2d6");
        t.Add("2d6d6");
        t.Add("2d8d6 + 4d12*3d20");
        t.Add("4d12");
        t.Add("4*d12");
        t.Add("4d"); // Rolls 4 d6

        D d = new D();
        foreach (var s in t)
            Console.WriteLine(string.Format("{0}\t{1}", d.R(s), s));
    }
埋葬我深情 2024-08-01 04:03:34

J

在 cobbal 的帮助下,将所有内容压缩为 93 个字符。

$ jconsole
   e=:".@([`('%'"_)@.(=&'/')"0@,)@:(3 :'":(1".r{.y)([:+/>:@?@$) ::(y&[)0".}.y}.~r=.y i.''d'''@>)@;:

   e '3d6 + 12'
20
   e 10$,:'3d6 + 12'
19 23 20 26 24 20 20 20 24 27
   e 10$,:'4*d12 + 3'
28 52 56 16 52 52 52 36 44 56
   e 10$,:'d100'
51 51 79 58 22 47 95 6 5 64

J

With cobbal's help, squeeze everything into 93 characters.

$ jconsole
   e=:".@([`('%'"_)@.(=&'/')"0@,)@:(3 :'":(1".r{.y)([:+/>:@?@$) ::(y&[)0".}.y}.~r=.y i.''d'''@>)@;:

   e '3d6 + 12'
20
   e 10$,:'3d6 + 12'
19 23 20 26 24 20 20 20 24 27
   e 10$,:'4*d12 + 3'
28 52 56 16 52 52 52 36 44 56
   e 10$,:'d100'
51 51 79 58 22 47 95 6 5 64
从﹋此江山别 2024-08-01 04:03:34

JavaScript 解决方案,压缩后 340 个字符(无 eval,支持前缀乘法器和后缀加法):

function comp (s, m, n, f, a) {
    m = parseInt( m );
    if( isNaN( m ) ) m = 1;
    n = parseInt( n );
    if( isNaN( n ) ) n = 1;
    f = parseInt( f );
    a = typeof(a) == 'string' ? parseInt( a.replace(/\s/g, '') ) : 0;
    if( isNaN( a ) ) a = 0;
    var r = 0;
    for( var i=0; i<n; i++ )
        r += Math.floor( Math.random() * f );
    return r * m + a;
};
function parse( de ) {
    return comp.apply( this, de.match(/(?:(\d+)\s*\*\s*)?(\d*)d(\d+)(?:\s*([\+\-]\s*\d+))?/i) );
}

测试代码:

var test = ["3d6 + 12", "4*d12 + 3", "d100"];
for(var i in test)
    alert( test[i] + ": " + parse(test[i]) );

压缩版本(很确定你可以做得更短):

function c(s,m,n,f,a){m=parseInt(m);if(isNaN(m))m=1;n=parseInt(n);if(isNaN(n))n=1;f=parseInt(f);a=typeof(a)=='string'?parseInt(a.replace(/\s/g,'')):0;if(isNaN(a))a=0;var r=0;for(var i=0;i<n;i++)r+=Math.floor(Math.random()*f);return r*m+a;};function p(d){return c.apply(this,d.match(/(?:(\d+)\s*\*\s*)?(\d*)d(\d+)(?:\s*([\+\-]\s*\d+))?/i));}

JavaScript solution, 340 chars when compressed (no eval, supports prefixed multiplicator and suffixed addition):

function comp (s, m, n, f, a) {
    m = parseInt( m );
    if( isNaN( m ) ) m = 1;
    n = parseInt( n );
    if( isNaN( n ) ) n = 1;
    f = parseInt( f );
    a = typeof(a) == 'string' ? parseInt( a.replace(/\s/g, '') ) : 0;
    if( isNaN( a ) ) a = 0;
    var r = 0;
    for( var i=0; i<n; i++ )
        r += Math.floor( Math.random() * f );
    return r * m + a;
};
function parse( de ) {
    return comp.apply( this, de.match(/(?:(\d+)\s*\*\s*)?(\d*)d(\d+)(?:\s*([\+\-]\s*\d+))?/i) );
}

Test code:

var test = ["3d6 + 12", "4*d12 + 3", "d100"];
for(var i in test)
    alert( test[i] + ": " + parse(test[i]) );

Compressed version (pretty sure you can do shorter):

function c(s,m,n,f,a){m=parseInt(m);if(isNaN(m))m=1;n=parseInt(n);if(isNaN(n))n=1;f=parseInt(f);a=typeof(a)=='string'?parseInt(a.replace(/\s/g,'')):0;if(isNaN(a))a=0;var r=0;for(var i=0;i<n;i++)r+=Math.floor(Math.random()*f);return r*m+a;};function p(d){return c.apply(this,d.match(/(?:(\d+)\s*\*\s*)?(\d*)d(\d+)(?:\s*([\+\-]\s*\d+))?/i));}
冧九 2024-08-01 04:03:34

Clojure,原样 854 个字符,缩小了 412 个

只需运行“(roll-dice "input-string")”即可。

(defn roll-dice
  [string]
  (let [parts (. (. (. string replace "-" "+-") replaceAll "\\s" "") split "\\+")
        dice-func (fn [d-notation]
                    (let [bits (. d-notation split "d")]
                      (if (= 1 (count bits))
                        (Integer/parseInt (first bits))  ; Just a number, like 12
                        (if (zero? (count (first bits)))
                          (inc (rand-int (Integer/parseInt (second bits))))  ; Just d100 or some such
                          (if (. (first bits) contains "*")
                            (* (Integer/parseInt (. (first bits) replace "*" ""))
                                (inc (rand-int (Integer/parseInt (second bits)))))
                            (reduce +
                              (map #(+ 1 %)
                                    (map rand-int
                                        (repeat
                                          (Integer/parseInt (first bits))
                                          (Integer/parseInt (second bits)))))))))))]      
    (reduce + (map dice-func parts))))

为了缩小,我将变量设为 1 个字母,将(第一位)/(第二位)移至变量中,将 dice-func 设为匿名函数,为 Integer.parseInt 制作一个名为“i”的包装器,并删除注释和额外的空格。

这应该适用于任何有效的内容,无论有没有空格。 只是不要去询问它“15dROBERT”,它会抛出异常。

他们的工作方式是将字符串分成骰子(即第三行,let)。 所以“5d6+2*d4-17”变成“5d6”,“2*d4”,“-17”。

然后,每个都由函数 dice-func 处理,并将结果相加(这是最后一行的映射/归约)

Dice-func 接受一个小骰子字符串(例如“5d6”)并将其拆分为“d”。 如果只剩下一部分,则它是一个简单的数字(6、-17 等)。

如果第一部分包含 *,我们将该数字乘以一个随机整数,1 到(d 后的数字)(含)。

如果第一部分不包含*,我们将随机滚动第一个数字(就像前一行一样)并将它们相加(这是中间的映射/归约)。

这是一个有趣的小挑战。

Clojure, 854 characters as is, 412 shrunk

Just run "(roll-dice "input-string")".

(defn roll-dice
  [string]
  (let [parts (. (. (. string replace "-" "+-") replaceAll "\\s" "") split "\\+")
        dice-func (fn [d-notation]
                    (let [bits (. d-notation split "d")]
                      (if (= 1 (count bits))
                        (Integer/parseInt (first bits))  ; Just a number, like 12
                        (if (zero? (count (first bits)))
                          (inc (rand-int (Integer/parseInt (second bits))))  ; Just d100 or some such
                          (if (. (first bits) contains "*")
                            (* (Integer/parseInt (. (first bits) replace "*" ""))
                                (inc (rand-int (Integer/parseInt (second bits)))))
                            (reduce +
                              (map #(+ 1 %)
                                    (map rand-int
                                        (repeat
                                          (Integer/parseInt (first bits))
                                          (Integer/parseInt (second bits)))))))))))]      
    (reduce + (map dice-func parts))))

To shrink, I made variables 1 letter, moved the (first bits)/(second bits) into variables, made dice-func an anonymous function, made a wrapper for Integer.parseInt called 'i', and stripped out comments and extra whitespace.

This should work on anything valid, with or without whitespace. Just don't go asking it for "15dROBERT", it will throw an exception.

They way it works is by splitting up the string into dice (that's the 3rd line, the let). So "5d6+2*d4-17" becomes "5d6","2*d4","-17".

Each of those is then processed by the function dice-func, and the results are added up (this is the map/reduce on the last line)

Dice-func takes a little dice string (such a "5d6") and splits it on the "d". If there is only one part left, it was a simple number (6, -17, etc).

If the first part contains a *, we multiply that number by a random interger, 1 to (number after d), inclusive.

If the first part doesn't contain a *, we take first number random rolls (just like previous line) and add them up (this is the map/reduce in the middle).

This was fun little challenge.

夏末染殇 2024-08-01 04:03:34

perl eval 版本,72 个字符

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 0...$1-1;eval$_}

运行,类似于

print e("4*d12+3"),"\n";

基于 ruby​​ 解决方案,只能运行一次(您应该在运行之间undef $a)。

较短的版本,68 个字符,时髦(基于 0)骰子

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=int rand$2for 0...$1-1;eval$_}

编辑

perl 5.8.8 不喜欢以前的版本,这里有一个 73 个字符的版本,可以工作

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 1...$1||1;eval$_}

70 个字符的版本支持多个卷

sub e{$_=pop;s/(\d*)d(\d+)/$a=$1||1;$a+=int rand$a*$2-($a-1)/ieg;eval}

perl eval version, 72 chars

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 0...$1-1;eval$_}

runs like

print e("4*d12+3"),"\n";

Based on the ruby solution, can only run once(you should undef $a between runs).

shorter version, 68 chars, funky (0 based) dice

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=int rand$2for 0...$1-1;eval$_}

EDIT

perl 5.8.8 didn't like the previous version, here's a 73 char version that works

sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 1...$1||1;eval$_}

70 char version supporting multiple rolls

sub e{$_=pop;s/(\d*)d(\d+)/$a=$1||1;$a+=int rand$a*$2-($a-1)/ieg;eval}
瑾兮 2024-08-01 04:03:34

F#( eval 和 正则表达式)

233 个字符

此解决方案应该是完全通用的,因为它可以处理您抛出的几乎任何字符串它,甚至是一些疯狂的事情,例如:

43d29d16d21*9 + d7d9*91 + 2*d24*7

需要全局定义:

let r = new System.Random()

完全模糊版本:

let f(s:string)=let g d o i p (t:string)=t.Split([|d|])|>Array.fold(fun s x->o s (p x))i in g '+'(+)0(g '*' (*) 1 (fun s->let b=ref true in g 'd'(+)1(fun t->if !b then b:=false;(if t.Trim()=""then 1 else int t)else r.Next(int t))s))s

可读版本:

let f (s:string) =
    let g d o i p (t:string) =
        t.Split([|d|]) |> Array.fold (fun s x -> o s (p x)) i
    g '+' (+) 0 (g '*' (*) 1 (fun s ->
                                        let b = ref true
                                        g 'd' (+) 1 (fun t ->
                                        if !b then b := false; (if t.Trim() = "" then 1 else int t)
                                        else r.Next(int t)) s)) s

我正在挑战某人击败这个解决方案(以任何语言)使用< code>eval 或正则表达式。 我认为这可能是可能的,但我仍然有兴趣看到这种方法。

F# (no eval and no regex)

233 characters

This solution should be fully generic, in that it can handle just about any string you throw at it, even something crazy such as:

43d29d16d21*9 + d7d9*91 + 2*d24*7

Need to define this globally:

let r = new System.Random()

Fully obfuscated version:

let f(s:string)=let g d o i p (t:string)=t.Split([|d|])|>Array.fold(fun s x->o s (p x))i in g '+'(+)0(g '*' (*) 1 (fun s->let b=ref true in g 'd'(+)1(fun t->if !b then b:=false;(if t.Trim()=""then 1 else int t)else r.Next(int t))s))s

Readable version:

let f (s:string) =
    let g d o i p (t:string) =
        t.Split([|d|]) |> Array.fold (fun s x -> o s (p x)) i
    g '+' (+) 0 (g '*' (*) 1 (fun s ->
                                        let b = ref true
                                        g 'd' (+) 1 (fun t ->
                                        if !b then b := false; (if t.Trim() = "" then 1 else int t)
                                        else r.Next(int t)) s)) s

I'm challenging someone to beat this solution (in any language) without using eval or regular expressions. I think it's likely to be possible, but I am interested in seeing the approach still.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文