Repository navigation
Expand file tree
/
Copy pathroll.d
More file actions
105 lines (92 loc) · 2.26 KB
/
Copy pathroll.d
File metadata and controls
105 lines (92 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import std.stdio;
import std.random;
import std.array;
import std.string;
import std.conv;
void main(string[] argv) {
if(argv.length==1) {
help();
return;
}
if(argv[1] == "-test") {
foreach(arg; argv[2..$]) {
test(to!int(arg));
}
return;
}
try {
foreach(arg; argv[1..$]) {
int val = parse(arg);
stdout.writeln(arg, "\t => \t",val);
}
}
catch(Throwable e) {
stdout.writeln(" (!) Error: argumentos incorrectos.");
help();
}
}
void help() {
stdout.writeln(" roll: un lanzador de dados minimalista");
stdout.writeln(" Uso: roll [<n>{a|d}<dado>[+{<n>{a|d}<dado>|modificador}]+]+");
return;
}
int roll(int d, bool ace = false) {
int ret = uniform(1, d+1);
if(ace) {
int tmp = ret;
while(tmp == d) {
tmp = uniform(1, d+1);
ret += tmp;
}
}
return ret;
}
int parse(string str) {
string[] parts = split(str, "+");
int total = 0;
foreach(part; parts) {
string[] subs = split(part, "-");
total+= parsePart(subs[0]);
subs.popFront();
foreach(sub; subs) {
total -= parsePart(sub);
}
}
return total;
}
private int parsePart(string str) {
int total = 0;
auto i = indexOf(str, "d", CaseSensitive.no);
if(i>0) { // Roll normally
total += parseNroll(str,i,false);
} else {
i = indexOf(str, "a", CaseSensitive.no);
if(i>0) { // Roll, with acing
total += parseNroll(str,i,true);
} else { // Is is not a roll but a modifier
total += to!int(str);
}
}
return total;
}
private int parseNroll(string str, long i, bool ace = false) {
int total = 0;
int nb = to!int(str[0..i]);
int d = to!int(str[i+1..$]);
for(int n = 0; n < nb; n++)
total += roll(d, ace);
return total;
}
void test(int d, bool ace = false) {
int[] res;
res.length=d+1;
int r;
stdout.writeln("Resultados para un dado de ", d, " en porcentaje");
for(int i = 0; i<1000000; i++) {
r = roll(d,ace);
res[r]++;
}
foreach(int die, int perc; res[1..$]) {
stdout.writeln(die, "\t =>\t", perc/10000.0);
}
}