Class: CARPS::Rule
- Inherits:
-
Object
- Object
- CARPS::Rule
- Defined in:
- lib/carps/mod/rule.rb
Overview
A rule.
This class uses the TemplateMethod pattern.
Subclasses should provide the following methods
-
‘dice’ method which takes needed parameters (ie from apply and show_odds) and returns a Dice
Instance Method Summary collapse
-
#apply(*args) ⇒ Object
Apply the rule to the arguments.
-
#initialize ⇒ Rule
constructor
Create the rule.
-
#show_odds(*args) ⇒ Object
Print the odds.
Constructor Details
#initialize ⇒ Rule
Create the rule
30 31 32 |
# File 'lib/carps/mod/rule.rb', line 30 def initialize @actions = {} end |
Instance Method Details
#apply(*args) ⇒ Object
Apply the rule to the arguments
35 36 37 38 39 40 41 |
# File 'lib/carps/mod/rule.rb', line 35 def apply *args d = dice *args result = d.roll action_klass = choose_action_class result action = action_klass.new action.apply result, *args end |
#show_odds(*args) ⇒ Object
Print the odds
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/carps/mod/rule.rb', line 44 def show_odds *args d = dice *args odds = {} d.odds.each do |result, chance| action = choose_action_class result odds[action] = odds[action].to_f + chance end puts "The odds are approximately:" puts "\n" # Sort the odds odds = odds.to_a.sort {|oda, odb| oda[1] <=> odb[1]} odds = odds.reverse odds.each do |action_klass, chance| action = action_klass.new percent = (chance * 100.0).to_i puts "#{percent}% chance:" puts "\t#{action.summary}" end end |