-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.rb
executable file
·457 lines (399 loc) · 11 KB
/
dice.rb
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/ruby
require 'pp'
=begin ToDo list
Rolls:
[x] normal die
[x] minimum die
[x] maximum die
[x] Action Dice
[x] extra (bonus die on value >= X)
[x] open (bonus die on value >= X, on bonus dice too)
Modifications:
[x] highest X values
[x] lowest X values
[x] hits (count values >= X)
=end
#Define vars
DEBUG = true
#/Define vars
#General Method Definitions
def flip_coin
if (rand(2) == 0) then return "heads" else return "tails" end
end
def roll(dicestring="1d20")
if dicestring =~ /(\d+)?d(\d+)(\+\d+|\-\d+)?(\..+)?/i
puts "$1: #{$1}; $2: #{$2}; $3: #{$3}; $4: #{$4}" if DEBUG == "verbose"
times = (($1.nil?)?1:$1).to_i
sides = (($2.nil?)?20:$2).to_i
mod = (($3.nil?)?0:$3).to_i
options = []
if $4 #scan for options
$4.scan(/\.[^.]*/i).each do
|m|
if m =~ /\((.*)\)/i
puts "options[:#{m.scan(/[^.(]*/i)[1]}] => #{m.match(/\((.*)\)/i)[1] }" if DEBUG == "verbose"
options.push({ m.scan(/[^.(]*/i)[1].to_sym => m.match(/\((.*)\)/i)[1].split(",")})
else
puts "options[:#{m.scan(/[^.(]*/i)[1]}] => nil}" if DEBUG == "verbose"
options.push({ m.scan(/[^.(]*/i)[1].to_sym => nil})
end
end
end
#die = dicestring.match(/\d*d\d+((\+|\-)\d+)?/)
a_throw = Throw.new(times,sides,mod,options,dicestring)
else
puts "Invalid String!"
end
for i in 1..a_throw.times
a_throw.dice.push(rand(a_throw.sides) + 1)
a_throw.total += a_throw.dice.last
end
puts "a_throw.output 1:" if DEBUG
pp(a_throw) if DEBUG
a_throw.output[:text] = a_throw.dicestring.to_s + ": " +a_throw.total.to_s
a_throw.total += a_throw.mod
puts "a_throw[:output] 2:" if DEBUG
pp(a_throw) if DEBUG
if a_throw.options then a_throw.evaloptions(a_throw.options) end
#output = a_throw[:dice].join(", ") + " + " + a_throw[:mod].to_s + " = " + a_throw.total.to_s
puts "a_throw.output 3:" if DEBUG
pp(a_throw) if DEBUG
puts a_throw.output[:text] if a_throw.output[:text]
return a_throw.total
end
def color(s,c)
color = "["
case c
when :red then color += "31"
when :green then color += "32"
end
color += "m"
return color + s.to_s + " [0m"
end
#End of General Method Definitions
#Class Definitions
class Throw
attr_accessor :times, :sides, :mod, :options, :rolls, :total, :output, :dicestring
def initialize(dicestring="1d20")
if dicestring =~ /(\d+)?d(\d+)(\+\d+|\-\d+)?/i
puts "$1: #{$1}; $2: #{$2}; $3: #{$3}; $4: #{$4}" if DEBUG == "verbose"
@times = (($1.nil?)?1:$1).to_i
@sides = (($2.nil?)?20:$2).to_i
@mod = (($3.nil?)?0:$3).to_i
@options = []
end
@rolls = []
@total = 0
@output = { :text => "" }
@dicestring = dicestring
end
def roll(times=1)
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
@rolls[i].push(rand(@sides) + 1)
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def minroll(min, times=1)
if min == nil then puts "you need to give a minimum roll."; return -1 end
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
while @rolls[i][j].to_i < min do @rolls[i][j] = rand(@sides) + 1 end
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def maxroll(max, times=1)
if max == nil then puts "you need to give a maximum roll."; return -1 end
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
while @rolls[i][j].to_i > max || @rolls[i][j].to_i == 0 do @rolls[i][j] = rand(@sides) + 1 end
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def actiondice(times=1)
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
@rolls[i][j] = rand(@sides) + 1
tmp = [@rolls[i][j]]
while tmp.last == 1 || tmp.last == @sides do
tmp.push(rand(@sides) + 1)
end
tmp = tmp[0] if tmp.size == 1
@rolls[i][j] = tmp
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def extra(bonus,times=1)
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
@rolls[i][j] = rand(@sides) + 1
if @rolls[i][j] >= bonus then
tmp = [@rolls[i][j]]
tmp.push(rand(@sides) + 1)
tmp = tmp[0] if tmp.size == 1
@rolls[i][j] = tmp
end
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def open(bonus,times=1)
for i in 0...times do
@rolls[i] = Dice.new
for j in 0...@times
@rolls[i][j] = rand(@sides) + 1
tmp = [@rolls[i][j]]
while tmp.last >= bonus do
tmp.push(rand(@sides) + 1)
end
tmp = tmp[0] if tmp.size == 1
@rolls[i][j] = tmp
end
end
puts "@rolls:" if DEBUG == "verbose"
pp(@rolls) if DEBUG == "verbose"
return @rolls
end
def total
@total = 0
@dice.each do |d|
@total += d
end
@total += self[:mod]
return @total
end
def full
self.total
output = ""
@output.each do
|k,v|
if k == :text then next end
puts "k = " if
pp(k) if DEBUG
puts "v = " if DEBUG
pp(v) if DEBUG
output += v.to_s
output += "\n"
end
puts "dicestring in full:" if DEBUG
pp(@dicestring) if DEBUG
output += @dicestring+": "
output += @dice.join(", ")
output += " + "+@mod.to_s if @mod > 0
output += " - "+(@mod * -1).to_s if @mod < 0
output += " = "+@total.to_s
@output[:text] = output
end
def highest(*args)
return @rolls.last.highest if args.nil?
return @rolls.last.highest(*args)
end
def lowest(*args)
return @rolls.last.lowest if args.nil?
return @rolls.last.lowest(*args)
end
end
class Dice < Array
def lowest(val=3,type=nil)
if (self.size < val)
range = 0...self.size
puts "self: " if DEBUG == "verbose"
puts "range = "+ range.to_s if DEBUG == "verbose"
else
range = 0...val
puts "val = " + val.to_s if DEBUG == "verbose"
puts "range = " + range.to_s if DEBUG == "verbose"
end
tmp = self.clone
values = tmp.sort[range]
indices = []
tmp2 = []
for i in range
indices.push(tmp.index(values.shift))
tmp[indices.last] = 0
end
if type == "index"
return indices
else
pp(indices) if DEBUG == "verbose"
indices.sort.each { |n| tmp2.push(self[n]) }
return tmp2
end
end
def highest(val=3, type=nil)
if (self.size < val)
range = 0...self.size
puts "self: " if DEBUG == "verbose"
puts "range = "+ range.to_s if DEBUG == "verbose"
else
range = 0...val
puts "val = " + val.to_s if DEBUG == "verbose"
puts "range = " + range.to_s if DEBUG == "verbose"
end
tmp = self.clone
values = tmp.sort.reverse[range]
indices = []
tmp2 = []
for i in range
indices.push(tmp.index(values.shift))
tmp[indices.last] = 0
end
if type == "index"
return indices
else
pp(indices) if DEBUG == "verbose"
indices.sort.each { |n| tmp2.push(self[n]) }
return tmp2
end
end
def hits(limit=15,type=nil)
indices = Array.new
for i in 0...self.size
if self[i] >= limit
indices.push(i)
end
end
if type == "index"
return indices
elsif type == "numbers"
output = []
indices.each { |v| output.push(self[v]) }
return output
else
return indices.size
end
end
end
#/Class Definitions
def menu(rolls)
menu =<<EOT
Rolls:
\t[a][#{rolls["type"] == "normal" ? "X" : " "}] normal die
\t[b][#{rolls["type"] == "min" ? "X" : " "}] minimum die
\t[c][#{rolls["type"] == "max" ? "X" : " "}] maximum die
\t[d][#{rolls["type"] == "AD" ? "X" : " "}] Action Dice
\t[e][#{rolls["type"] == "extra" ? "X" : " "}] extra (bonus die on value >= X)
\t[f][#{rolls["type"] == "open" ? "X" : " "}] open (bonus die on value >= X, on bonus dice too)
Modifications:
\t[g][#{rolls["opts"]["highest"] ? "X" : " "}] highest X values
\t[h][#{rolls["opts"]["lowest"] ? "X" : " "}] lowest X values
\t[i][#{rolls["opts"]["hits"] ? "X" : " "}] hits (count values >= X)
Values:
\t[j] Roll x times: #{rolls["times"] ? rolls["times"] : " "}
\t[k] Dice: #{rolls["dice"] ? rolls["dice"] : " "}
Actions:
\t[r] Roll Dice
\t[z] Exit
EOT
end
if __FILE__ == $0 #main
if DEBUG == "verbose"
puts "------------------ Entering Main ------------------"
puts "\n\nRolls:"
puts "------------------- Flip Coin: --------------------"
puts flip_coin
puts "---------------------------------------------------"
puts "------------------ Normal Roll: -------------------"
test = Throw.new("5d20+1")
test.roll(2)
pp(test.rolls)
puts "---------------------------------------------------"
puts "-------------- Minimum Roll (5d4+1): --------------"
mintest = Throw.new("5d4+1")
mintest.minroll(2,2)
pp(mintest.rolls)
puts "---------------------------------------------------"
puts "------------- Maximum Roll (5d20+1): --------------"
maxtest = Throw.new("5d20+1")
maxtest.maxroll(4,2)
pp(maxtest.rolls)
puts "---------------------------------------------------"
puts "--------------- Action Dice (5d4): ----------------"
adtest = Throw.new("5d4")
adtest.actiondice(2)
pp(adtest.rolls)
puts "---------------------------------------------------"
puts "-------------- Extra Roll: (5d20): ----------------"
extratest = Throw.new("5d20")
extratest.extra(15,2)
pp(extratest.rolls)
puts "---------------------------------------------------"
puts "--------------- Open Roll: (5d20): ----------------"
opentest = Throw.new("5d20+1")
opentest.open(15,2)
pp(opentest.rolls)
puts "------------------- End of Rolls ------------------"
puts "-------------------- Modifiers: -------------------"
puts "rolls to modify:"
pp(test.rolls)
puts "--------------------- Lowest: ---------------------"
puts "Roll 1:"
pp(test.rolls[0].lowest)
puts "Roll 2:"
pp(test.lowest)
puts "--------------------- Highest: --------------------"
puts "Roll 1:"
pp(test.rolls[0].highest)
puts "Roll 2:"
pp(test.highest)
puts "---------------------- Hits: ----------------------"
puts "Roll 1:"
pp(test.rolls[0].hits)
puts "Roll 2:"
pp(test.rolls[1].hits)
puts "----------------- End of Modifiers ----------------"
puts "------------------- End of Main -------------------"
else
rolls = {}
rolls["opts"] = {}
quit = false
while quit != true
puts menu(rolls)
choice = gets.chomp
case choice
when 'a' then rolls["type"] = "normal"
when 'b' then rolls["type"] = "min"
when 'c' then rolls["type"] = "max"
when 'd' then rolls["type"] = "AD"
when 'e' then rolls["type"] = "extra"
when 'f' then rolls["type"] = "open"
when 'g' then
if rolls["opts"]["highest"] then rolls["opts"]["highest"] = false
else rolls["opts"]["highest"] = true; rolls["opts"]["lowest"] = false
end
when 'h' then
if rolls["opts"]["lowest"] then rolls["opts"]["lowest"] = false
else rolls["opts"]["lowest"] = true; rolls["opts"]["highest"] = false
end
when 'i' then
if rolls["opts"]["hits"] then rolls["opts"]["hits"] = false
else rolls["opts"]["hits"] = true end
when 'j' then
tmp = gets.chomp.to_i
if tmp =~ /\d/ then yield end
when 'k' then
when 'r' then
when 'z' then quit = true
end
end
end
end