dice_stats 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/Dice.rb +6 -2
- data/lib/Dice_Set.rb +56 -11
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d2585fee905bf91f1d0b97de506e244c6a12b552
|
|
4
|
+
data.tar.gz: 990f862e6ffe00adba7941fc08c6b2fd2af88de5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1042285361fee86176d87b58ecacd339e563f083dbea7cfbf8493d1f5dd710e70105672c5afaa19cff1747243e3c911ebb915d6b7dc3f03fc27cab5c2622c84f
|
|
7
|
+
data.tar.gz: 21829b5c09f5f28532a397b9ec54b350f1fa0c68e235eb59a09e56585a04bae603467ce0f090f513961fc55525c95ea11fb379704533a4524cbb0a348faae8b3
|
data/lib/Dice.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Dice_Stats
|
|
|
19
19
|
##
|
|
20
20
|
# Creates a new Dice instance of a number of dice (+dice_count+) with +dice_sides+ faces.
|
|
21
21
|
# All dice are assumed to go from 1 to +dice_sides+.
|
|
22
|
-
def initialize(dice_count, dice_sides)
|
|
22
|
+
def initialize(dice_count, dice_sides, do_calculation=true)
|
|
23
23
|
@count = dice_count
|
|
24
24
|
@sides = dice_sides
|
|
25
25
|
@probability_distribution = {}
|
|
@@ -27,7 +27,11 @@ module Dice_Stats
|
|
|
27
27
|
if (@count < 0 || @sides < 0)
|
|
28
28
|
#error
|
|
29
29
|
else
|
|
30
|
-
|
|
30
|
+
if do_calculation
|
|
31
|
+
@probability_distribution = calculate_probability_distribution
|
|
32
|
+
else
|
|
33
|
+
@probability_distribution = {}
|
|
34
|
+
end
|
|
31
35
|
end
|
|
32
36
|
end
|
|
33
37
|
|
data/lib/Dice_Set.rb
CHANGED
|
@@ -17,6 +17,42 @@ module Dice_Stats
|
|
|
17
17
|
# The constituent separate dice
|
|
18
18
|
attr_accessor :dice
|
|
19
19
|
|
|
20
|
+
##
|
|
21
|
+
# Returns the "clean string" for a dice pattern without actually instantiating the class, calculating the probability, etc.
|
|
22
|
+
# Good to use to check a cache hit if results are being cached
|
|
23
|
+
def self.Get_Clean_String(dice_string, with_constant=true)
|
|
24
|
+
dice = []
|
|
25
|
+
constant = 0
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
split_string = dice_string.split('+')
|
|
29
|
+
|
|
30
|
+
split_string.map!{|i| i.strip }
|
|
31
|
+
|
|
32
|
+
split_string.count.times { |i|
|
|
33
|
+
if /\d+[dD]\d+/.match(split_string[i])
|
|
34
|
+
sub_string_split = split_string[i].downcase.split('d')
|
|
35
|
+
dice << Dice.new(sub_string_split[0].to_i, sub_string_split[1].to_i, false)
|
|
36
|
+
elsif (split_string[i].to_i > 0)
|
|
37
|
+
constant += split_string[i].to_i
|
|
38
|
+
else
|
|
39
|
+
puts "Unexpected paramter: #{split_string[0]}"
|
|
40
|
+
end
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
dice.sort! { |d1,d2| d2.sides <=> d1.sides }
|
|
44
|
+
|
|
45
|
+
formatted_string = ""
|
|
46
|
+
dice.each { |d|
|
|
47
|
+
formatted_string += d.count.to_s + "d" + d.sides.to_s + " + "
|
|
48
|
+
}
|
|
49
|
+
if with_constant && constant > 0
|
|
50
|
+
return (formatted_string + constant.to_s)
|
|
51
|
+
else
|
|
52
|
+
return (formatted_string[0..formatted_string.length-4])
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
20
56
|
##
|
|
21
57
|
# Instantiates a new Dice_Set with the specified +dice_string+ pattern.
|
|
22
58
|
# Examples:
|
|
@@ -24,7 +60,15 @@ module Dice_Stats
|
|
|
24
60
|
# "2d6 + 5"
|
|
25
61
|
# "1d8"
|
|
26
62
|
# "5d4 + 3d10"
|
|
27
|
-
|
|
63
|
+
# If +probability_distribution+ is set, no distributions will be calculated and the provided distribution will be used.
|
|
64
|
+
def initialize(dice_string, probability_distribution=nil)
|
|
65
|
+
|
|
66
|
+
# Reject any predefined probability distributions that don't sum to 1.0.
|
|
67
|
+
# Perhaps in the future I should add slightly better checks, but this is adequate for now.
|
|
68
|
+
if probability_distribution != nil && (probability_distribution.inject(0) { |memo,(k,v)| memo + v }.round(3).to_f != 1.0)
|
|
69
|
+
probability_distribution = nil
|
|
70
|
+
end
|
|
71
|
+
|
|
28
72
|
@dice = []
|
|
29
73
|
@constant = 0
|
|
30
74
|
@input_string = dice_string
|
|
@@ -37,7 +81,9 @@ module Dice_Stats
|
|
|
37
81
|
split_string.count.times { |i|
|
|
38
82
|
if /\d+[dD]\d+/.match(split_string[i])
|
|
39
83
|
sub_string_split = split_string[i].downcase.split('d')
|
|
40
|
-
|
|
84
|
+
|
|
85
|
+
# Skip calculating the sub-die distribution if the total one is already cached
|
|
86
|
+
@dice << Dice.new(sub_string_split[0].to_i, sub_string_split[1].to_i, (probability_distribution == nil))
|
|
41
87
|
elsif (split_string[i].to_i > 0)
|
|
42
88
|
@constant += split_string[i].to_i
|
|
43
89
|
else
|
|
@@ -45,17 +91,16 @@ module Dice_Stats
|
|
|
45
91
|
end
|
|
46
92
|
}
|
|
47
93
|
|
|
48
|
-
|
|
49
|
-
# if the n-ary cartesian product has to process more than 10,000,000 combinations it can take quite a while to finish...
|
|
50
|
-
@aborted_probability_distribution = true
|
|
51
|
-
else
|
|
52
|
-
@dice.sort! { |d1,d2| d2.sides <=> d1.sides }
|
|
94
|
+
@dice.sort! { |d1,d2| d2.sides <=> d1.sides }
|
|
53
95
|
|
|
54
|
-
|
|
96
|
+
if probability_distribution != nil
|
|
97
|
+
@probability_distribution = probability_distribution
|
|
98
|
+
else
|
|
99
|
+
@probability_distribution = combine_probability_distributions
|
|
100
|
+
end
|
|
55
101
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
end
|
|
102
|
+
if (@probability_distribution.inject(0) { |memo,(k,v)| memo + v }.round(3).to_f != 1.0)
|
|
103
|
+
#puts "Error in probability distrubtion."
|
|
59
104
|
end
|
|
60
105
|
end
|
|
61
106
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dice_stats
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthew Foy
|
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
62
62
|
version: '0'
|
|
63
63
|
requirements: []
|
|
64
64
|
rubyforge_project:
|
|
65
|
-
rubygems_version: 2.5.
|
|
65
|
+
rubygems_version: 2.5.2
|
|
66
66
|
signing_key:
|
|
67
67
|
specification_version: 4
|
|
68
68
|
summary: Dice statistics utility.
|