Class: GamesAndRpgParadise::Dice

Inherits:
Base
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb

Overview

GamesAndRpgParadise::Dice

Constant Summary collapse

SHALL_WE_DEBUG =
#

SHALL_WE_DEBUG

#
false
Die =
#

GamesAndRpgParadise::Die

#
Dice

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#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
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      @internal_hash[:report_at_once] = false
    end
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

GamesAndRpgParadise::Dice[]

Usage example:

GamesAndRpgParadise::Dice[6] # A six-sided die.
10.times { e GamesAndRpgParadise::Dice[6] }
#


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

#

GamesAndRpgParadise::Dice.calculate (complex tag)

Usage examples:

x = GamesAndRpgParadise::Dice.new; x.calc_complex('3W+5')
GamesAndRpgParadise::Dice.new.calc_complex(Dice.generate_random_complex)
100.times { GamesAndRpgParadise::Dice.new.calc_complex(GamesAndRpgParadise::Dice.generate_random_complex) }
GamesAndRpgParadise::Dice.calc_complex('1W+6')
GamesAndRpgParadise::Dice.calc_complex('5W+6')
#


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_complexObject

#

GamesAndRpgParadise::Dice.generate_random_complex

This method will generate a possible die roll, such as 12W5 or 11W8.

GamesAndRpgParadise::Dice.generate_random_complex # => "13W8"
#


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

#

GamesAndRpgParadise::Dice.silently_roll_die

This method can be used to silently roll the die. Silently here means that this method will not output anything.

Usage examples:

100.times { e GamesAndRpgParadise::Dice.silently_roll_die(20) }
GamesAndRpgParadise::Dice.silently_roll_die(20)
GamesAndRpgParadise::Dice.silently_roll_die(12)
GamesAndRpgParadise::Dice.silently_roll_die(12) { :min }
GamesAndRpgParadise::Dice.silently_roll_die('1D10')
#


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

#

calculate_to_s

#


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

#complex_formula_to_use?Boolean Also known as: formula?, complex_dice?, complex_dice

#

complex_formula_to_use?

#

Returns:

  • (Boolean)


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

#maxObject

#

max

#


390
391
392
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 390

def max
  roll(complex_dice?) { :max }
end

#minObject

#

min

#


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

#

modifier?

#

Returns:

  • (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
  # ======================================================================= #
  # First we must determine the modifier.
  # ======================================================================= #
  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 # This is for e. g. 2-12. Here we assume equidistance.
      @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.

#

Returns:

  • (Boolean)


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

#

report_at_once?

#

Returns:

  • (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_sidesObject 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_resultObject Also known as: show_the_result, pshow_number

#

report_the_result

#


290
291
292
# File 'lib/games_and_rpg_paradise/utility_scripts/dice/dice.rb', line 290

def report_the_result
  e result?
end

#resetObject

#

reset (reset tag)

#


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
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  reset_the_internal_hash
  # ======================================================================= #
  # === :report_at_once
  # ======================================================================= #
  @internal_hash[:report_at_once] = true
  reset_the_internal_state
end

#reset_the_internal_stateObject

#

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
  # ======================================================================= #
  # === :the_die_has_n_sides
  #
  # The default is a six-sided die.
  # ======================================================================= #
  @internal_hash[:the_die_has_n_sides] = 6
  # ======================================================================= #
  # === :how_many_dice_should_be_used
  # ======================================================================= #
  @internal_hash[:how_many_dice_should_be_used] = 1
  # ======================================================================= #
  # === :modifier
  # ======================================================================= #
  @internal_hash[:modifier] = 0
  # ======================================================================= #
  # === :complex_formula_to_uUse
  # ======================================================================= #
  @internal_hash[:complex_formula_to_use] = '1W6'
  # ======================================================================= #
  # === :result
  # ======================================================================= #
  @internal_hash[:result] = nil
  # ======================================================================= #
  # === :range
  # ======================================================================= #
  @internal_hash[:range] = nil
end

#result?Boolean Also known as: result, to_s

#

result?

#

Returns:

  • (Boolean)


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_rangeObject 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 # Pass through getting a random number
    min, max = range.split('-')
    return rand(max.to_i + min.to_i)
  end
  return complex_dice?
end

#roll_a_new_resultObject Also known as: determine_the_result, roll, roll_anew, roll_n_sided_die, do_roll_the_dice

#

roll_a_new_result

#


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_msgObject

#

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

#runObject

#

run

#


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

#set_complex_formula_to_use(i) ⇒ Object Also known as: set_complex_dice, complex_dice=

#

set_complex_formula_to_use

#


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

#

set_modifier

#


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

#

set_n_sides

#


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

#

set_result

#


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

#

silently_roll_die

#


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

#

the_die_has_n_sides?

#

Returns:

  • (Boolean)


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

#

verbose_roll_die

#


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