forked from Humblemonk/DiceMaiden
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseventhSea.rb
More file actions
138 lines (118 loc) · 3.27 KB
/
seventhSea.rb
File metadata and controls
138 lines (118 loc) · 3.27 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'pp'
class Array
def contains_all? other
other = other.dup
each{|e| if i = other.index(e) then other.delete_at(i) end}
other.empty?
end
def combine(prods)
prods.map(&:flatten)
.each_with_object(Hash.new {|h,k| h[k]=[]}) { |(k,v),h| h[k] << v }
.map { |k,v| { k=>v } }
end
end
def roll()
(rand(10)).floor + 1
end
def rollNDice(count)
rolls = []
(1..count).each do |i|
rolls.push(roll())
end
return rolls
end
def getRollCounts(roll)
rollCounts = Hash.new 0
roll.each do |die|
rollCounts[die] += 1
end
return rollCounts
end
# Count all the Tens
def getCritTen(roll)
critTen = roll.select{|e| e == 10}
removedTens = roll.delete_if{|e| e == 10}
# return nonTenRoll
return critTen, removedTens
end
# puts critTen
# Remove the 10's from the roll, and use the subset roll
# Find all permutations of exact Tens
# Start with exact Tens (x+y==10)
# Increase to (x+y=11), etc up to 18 (9+9)
# Loop through remaining roll
def getValidPerms(roll)
validPerms = []
(2..(roll.length/2 +1)).each do |i|
# Loop through permutations starting with 2 (1 perms are perfect 10s)
roll.combination(i).to_a.each do |j|
if(j.reduce(:+) >= 10)
validPerms.push(j)
end
end
end
return createPermHash(validPerms)
end
def getValidPerms15(roll)
validPerms = []
(2..(roll.length/2 +1)).each do |i|
# Loop through permutations starting with 2 (1 perms are perfect 10s)
roll.combination(i).to_a.each do |j|
if(j.reduce(:+) >= 15)
validPerms.push(j)
end
end
end
return createPermHash15(validPerms)
end
def createPermHash(validPerms)
permHash = Hash.new 0
(10..18).each do |val|
permHash[val] = (validPerms.select {|e| e.reduce(:+) == val}).uniq
end
return permHash
end
def createPermHash15(validPerms)
permHash = Hash.new 0
(10..20).each do |val|
permHash[val] = (validPerms.select {|e| e.reduce(:+) == val}).uniq
end
return permHash
end
# Loop through valid perms and decrement the hash value
def calculateRaises(permHash, nonTenRoll, rollCounts)
finalRoll = []
permHash.each do |k,v|
v.each do |i|
i.each_slice(i.length) do |e|
if(nonTenRoll.contains_all?(e))
finalRoll.push(e) # Push the pair in
e.each do |j|
next if (rollCounts[j] <= 0)
rollCounts[j] -= 1
nonTenRoll.slice!(nonTenRoll.index(j))
end
end
end
end
end
return finalRoll, nonTenRoll
end
def calculateRaises15(permHash, roll15, rollCounts)
finalRoll = []
permHash.each do |k,v|
v.each do |i|
i.each_slice(i.length) do |e|
if(roll15.contains_all?(e))
finalRoll.push(e) # Push the pair in
e.each do |j|
next if (rollCounts[j] <= 0)
rollCounts[j] -= 1
roll15.slice!(roll15.index(j))
end
end
end
end
end
return finalRoll, roll15
end