Class: GamesAndRpgParadise::Dice
- Inherits:
-
Base
- Object
- Base
- GamesAndRpgParadise::Dice
show all
- Defined in:
- lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb
Overview
GamesAndRpgParadise::Dice
Constant Summary
collapse
- SHALL_WE_DEBUG =
false
- Die =
#
GamesAndRpgParadise::Die
#
Dice
Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS
CommonExtensions::CONTROL_C_CODE, CommonExtensions::N
Class Method Summary
collapse
Instance Method Summary
collapse
-
#calculate_to_s(i) ⇒ Object
(also: #as_string)
# === calculate_to_s ========================================================================= #.
-
#calculates(i = '') ⇒ Object
(also: #calculate, #calc_complex)
# === calculates.
-
#complex_formula_to_use? ⇒ Boolean
(also: #formula?, #complex_dice?, #complex_dice)
-
#initialize(i = ARGV, run_already = true, &block) ⇒ Dice
constructor
# === initialize.
-
#max ⇒ Object
# === max ========================================================================= #.
-
#min ⇒ Object
# === min ========================================================================= #.
-
#modifier? ⇒ Boolean
# === modifier? ========================================================================= #.
-
#parse_into_the_correct_constituents(i = ) ⇒ Object
(also: #set_complex_die, #complex_die=, #determine_dice)
# === parse_into_the_correct_constituents ========================================================================= #.
-
#probability(percent = 75) ⇒ Object
# === probability To use probability:.
-
#range? ⇒ Boolean
(also: #range)
# === range?.
-
#report_at_once? ⇒ Boolean
# === report_at_once? ========================================================================= #.
-
#report_n_sides ⇒ Object
(also: #sides)
# === report_n_sides.
-
#report_the_result ⇒ Object
(also: #show_the_result, #pshow_number)
# === report_the_result ========================================================================= #.
-
#reset ⇒ Object
# === reset (reset tag) ========================================================================= #.
-
#reset_the_internal_state ⇒ Object
# === reset_the_internal_state ========================================================================= #.
-
#result? ⇒ Boolean
(also: #result, #to_s)
# === result? ========================================================================= #.
-
#reverse_determine_the_range ⇒ Object
(also: #determine_range)
# === reverse_determine_the_range.
-
#roll_a_new_result ⇒ Object
(also: #determine_the_result, #roll, #roll_anew, #roll_n_sided_die, #do_roll_the_dice)
# === roll_a_new_result ========================================================================= #.
-
#roll_msg ⇒ Object
# === roll_msg.
-
#run ⇒ Object
# === run ========================================================================= #.
-
#set_complex_formula_to_use(i) ⇒ Object
(also: #set_complex_dice, #complex_dice=)
-
#set_modifier(i) ⇒ Object
# === set_modifier ========================================================================= #.
-
#set_n_sides(i) ⇒ Object
# === set_n_sides ========================================================================= #.
-
#set_result(i) ⇒ Object
# === set_result ========================================================================= #.
-
#silently_roll_die(i = ARGV, &block) ⇒ Object
# === silently_roll_die ========================================================================= #.
-
#the_die_has_n_sides? ⇒ Boolean
(also: #n_sides?, #n_sides)
# === the_die_has_n_sides? ========================================================================= #.
-
#verbose_roll_die(complex_formula = '1W6') ⇒ Object
# === verbose_roll_die ========================================================================= #.
ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow
#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments
#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into
Constructor Details
#initialize(i = ARGV, run_already = true, &block) ⇒ Dice
#
initialize
How many sides should a die have?
n_sides -> how many sides does this die have
Dice.new(12).show_number
h={};h.default=0;1000.times {|x| h[Dice.new(20).show_number] += 1}
#
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 42
def initialize(
i = ARGV,
run_already = true,
&block
)
set_commandline_arguments(i)
reset
if block_given?
yielded = yield
case yielded
when :be_quiet
@internal_hash[:report_at_once] = false
end
end
run if run_already
end
|
Class Method Details
.[](i = ARGV) ⇒ Object
495
496
497
498
499
500
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 495
def self.[](i = ARGV)
if i.is_a? Array
i = i.join
end
new.calculate('1W'+i.to_s)
end
|
.calculate(i, &block) ⇒ Object
476
477
478
479
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 476
def self.calculate(i, &block)
e swarn('DEBUG: Input war `'+i.to_s+'`') if SHALL_WE_DEBUG
new.calc_complex(i, &block)
end
|
.generate_random_complex ⇒ Object
#
GamesAndRpgParadise::Dice.generate_random_complex
This method will generate a possible die roll, such as 12W5 or 11W8.
GamesAndRpgParadise::Dice.generate_random_complex
#
456
457
458
459
460
461
462
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 456
def self.generate_random_complex
_ = ''.dup
_ << (rand(13)+1).to_s
_ << 'W'
_ << (rand(10)+1).to_s
return _
end
|
.silently_roll_die(complex_formula = '1W6', &block) ⇒ Object
429
430
431
432
433
434
435
436
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 429
def self.silently_roll_die(
complex_formula = '1W6',
&block
)
dice = GamesAndRpgParadise::Dice.new(complex_formula) { :be_quiet }
dice.roll_a_new_result
return dice.result?
end
|
Instance Method Details
#calculate_to_s(i) ⇒ Object
Also known as:
as_string
186
187
188
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 186
def calculate_to_s(i)
calculates(i).to_s
end
|
#calculates(i = '') ⇒ Object
Also known as:
calculate, calc_complex
#
calculates
Note that the alias calc_complex refers to a complex formula, such as “3W6+6”. Since as of March 2023 that name is a slight misnomer, as we can also use simpler formulas such as “1W6” - or simply “6”, which will be equivalent to “1W6”.
#
176
177
178
179
180
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 176
def calculates(i = '')
parse_into_the_correct_constituents(i)
roll_a_new_result
return result?
end
|
162
163
164
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 162
def complex_formula_to_use?
@internal_hash[:complex_formula_to_use]
end
|
#max ⇒ Object
390
391
392
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 390
def max
roll(complex_dice?) { :max }
end
|
#min ⇒ Object
383
384
385
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 383
def min
roll(complex_dice?) { :min }
end
|
#modifier? ⇒ Boolean
112
113
114
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 112
def modifier?
@internal_hash[:modifier]
end
|
#parse_into_the_correct_constituents(i = ) ⇒ Object
Also known as:
set_complex_die, complex_die=, determine_dice
#
parse_into_the_correct_constituents
#
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
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 193
def parse_into_the_correct_constituents(
i = @internal_hash[:complex_formula_to_use]
)
reset_the_internal_state
i = i.to_s
@internal_hash[:complex_formula_to_use] = i
if i.include? '+'
splitted = i.split('+')
set_modifier('+'+splitted.last.to_s)
i = splitted[0 .. -2].join
elsif i.include? '-'
if i.include?('W') or i.include?('D') or i.include?('w')
splitted = i.split('-')
set_modifier('-'+splitted.last.to_s)
i = splitted[0 .. -2].join
else @internal_hash[:the_die_has_n_sides] = nil
@internal_hash[:how_many_dice_should_be_used] = nil
@internal_hash[:range] = i.split('-').map(&:to_i)
set_modifier('')
return
end
else
set_modifier('')
end
token_to_split_on = 'W'
token_to_split_on = 'D' if i.to_s.include? 'D'
token_to_split_on = 'w' if i.to_s.include? 'w'
splitted = i.to_s.split(token_to_split_on)
@internal_hash[:the_die_has_n_sides] = splitted.last.to_i
@internal_hash[:how_many_dice_should_be_used] = splitted.first.to_i
end
|
#probability(percent = 75) ⇒ Object
#
probability
To use probability:
probability 55 do
e "hello world"
end
Or
probability 88 { e "you died" }
Dice.new.probability(88) { e "You died." }
#
153
154
155
156
157
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 153
def probability(percent = 75)
random_value = rand
result = random_value < (percent.to_f / 100.0)
yield if result
end
|
#range? ⇒ Boolean
Also known as:
range
#
range?
Currently we will not return a negative result. This may have to be changed eventually - let’s see.
#
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 358
def range?
if @internal_hash[:range]
@internal_hash[:range].first.to_s+'-'+
@internal_hash[:range].last.to_s
else
_ = ''.dup
start_position = @internal_hash[:how_many_dice_should_be_used]
end_position = @internal_hash[:how_many_dice_should_be_used] * the_die_has_n_sides?
modifier = modifier?.to_s
if modifier.start_with?('+')
start_position += modifier.to_i.abs
end_position += modifier.to_i.abs
elsif modifier.start_with?('-')
start_position -= modifier.to_i.abs
end_position -= modifier.to_i.abs
end
start_position = 0 if start_position < 0
end_position = 0 if end_position < 0
return start_position.to_s+'-'+end_position.to_s
end
end
|
#report_at_once? ⇒ Boolean
127
128
129
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 127
def report_at_once?
@internal_hash[:report_at_once]
end
|
#report_n_sides ⇒ Object
Also known as:
sides
#
report_n_sides
This method reports how many sides the die has.
#
300
301
302
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 300
def report_n_sides
e n_sides?
end
|
#report_the_result ⇒ Object
Also known as:
show_the_result, pshow_number
290
291
292
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 290
def report_the_result
e result?
end
|
#reset ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 65
def reset
reset_the_internal_hash
@internal_hash[:report_at_once] = true
reset_the_internal_state
end
|
#reset_the_internal_state ⇒ Object
#
reset_the_internal_state
#
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
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 80
def reset_the_internal_state
@internal_hash[:the_die_has_n_sides] = 6
@internal_hash[:how_many_dice_should_be_used] = 1
@internal_hash[:modifier] = 0
@internal_hash[:complex_formula_to_use] = '1W6'
@internal_hash[:result] = nil
@internal_hash[:range] = nil
end
|
#result? ⇒ Boolean
Also known as:
result, to_s
275
276
277
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 275
def result?
@internal_hash[:result]
end
|
#reverse_determine_the_range ⇒ Object
Also known as:
determine_range
#
reverse_determine_the_range
Usage example:
die = Dice.new; die.determine_range('8-18')
die.complex_dice?; die.calc_complex; die.range
pp die.determine_dice('2-12')
#
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
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 323
def reverse_determine_the_range
_ = formula?
range = range?
case range
when '2-10'
set_complex_dice '1W9+1'
when '2-16'
set_complex_dice '2W8'
when '3-12'
set_complex_dice '3W4'
when '4-16'
set_complex_dice '4W4'
when '3-18'
set_complex_dice '3W6'
when '4-24'
set_complex_dice '4W6'
when '6-16'
set_complex_dice '2W6+4'
when '8-18'
set_complex_dice '8-18'
when '8-20'
set_complex_dice '1W13+7'
else min, max = range.split('-')
return rand(max.to_i + min.to_i)
end
return complex_dice?
end
|
#roll_a_new_result ⇒ Object
Also known as:
determine_the_result, roll, roll_anew, roll_n_sided_die, do_roll_the_dice
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 249
def roll_a_new_result
_ = 0
if @internal_hash[:range]
_ = rand(@internal_hash[:range].first .. @internal_hash[:range].last)
else
@internal_hash[:how_many_dice_should_be_used].times {
_ += rand(@internal_hash[:the_die_has_n_sides])+1
}
modifier = modifier?.to_s
if modifier.start_with?('+')
_ += modifier.to_i.abs
elsif modifier.start_with?('-')
_ -= modifier.to_i.abs
end
end
set_result(_)
return _
end
|
#roll_msg ⇒ Object
#
roll_msg
The message you get by rolling the dice.
#
309
310
311
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 309
def roll_msg
e "You roll the #{n_sides?}-sided die."
end
|
#run ⇒ Object
397
398
399
400
401
402
403
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 397
def run
if first?
parse_into_the_correct_constituents(first?.to_s)
do_roll_the_dice
report_the_result if report_at_once?
end
end
|
119
120
121
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 119
def set_complex_formula_to_use(i)
@internal_hash[:complex_formula_to_use] = i
end
|
#set_modifier(i) ⇒ Object
134
135
136
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 134
def set_modifier(i)
@internal_hash[:modifier] = i
end
|
#set_n_sides(i) ⇒ Object
242
243
244
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 242
def set_n_sides(i)
@internal_hash[:the_die_has_n_sides] = i
end
|
#set_result(i) ⇒ Object
283
284
285
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 283
def set_result(i)
@internal_hash[:result] = i
end
|
#silently_roll_die(i = ARGV, &block) ⇒ Object
441
442
443
444
445
446
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 441
def silently_roll_die(
i = ARGV,
&block
)
::GamesAndRpgParadise::Dice.silently_roll_die(i, &block)
end
|
#the_die_has_n_sides? ⇒ Boolean
Also known as:
n_sides?, n_sides
234
235
236
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 234
def the_die_has_n_sides?
@internal_hash[:the_die_has_n_sides]
end
|
#verbose_roll_die(complex_formula = '1W6') ⇒ Object
408
409
410
411
412
|
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 408
def verbose_roll_die(complex_formula = '1W6')
parse_into_the_correct_constituents(complex_formula.to_s)
do_roll_the_dice
print "The result of this roll is: "+result?.to_s+N
end
|