Guest User

Untitled

a guest
Apr 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. class Character
  2. attr_accessor :strength, :level, :agility
  3. def initialize(level, strength, agility)
  4. @level, @strength, @agility = level, strength, agility
  5. @stat_boosts = {}
  6. end
  7.  
  8. def attack_power
  9. stat_boost :attack_power
  10. end
  11.  
  12. def stat_boost(stat)
  13. @stat_boosts[stat] || 0
  14. end
  15.  
  16. def add_stat_boost(stat, amount)
  17. @stat_boosts[stat] = amount
  18. end
  19.  
  20. def remove_stat_boost(stat)
  21. @stat_boosts.delete(stat)
  22. end
  23. end
  24.  
  25. class Rogue < Character
  26. def attack_power
  27. strength + agility + (level * 2) - 20 + super
  28. end
  29. end
  30.  
  31. class Druid < Character
  32. VALID_FORMS = [:bear, :cat, :moonkin]
  33. def attack_power
  34. super + case form
  35. when :bear
  36. (strength * 2) + (level * 3) - 20
  37. when :cat
  38. (strength * 2) + agility + (level * 2) - 20
  39. when :moonkin
  40. (strength * 2) + (level * 1.5) - 20
  41. end
  42. end
  43.  
  44. def form=(form)
  45. @form = VALID_FORMS.find {|f| f == (form) } || raise("no such form")
  46. end
  47. attr_reader :form
  48. end
  49.  
  50. progue = Rogue.new(120, 700, 70)
  51. progue.add_stat_boost :attack_power, 200
  52. puts progue.attack_power
  53. # => 1190
  54. progue.remove_stat_boost :attack_power
  55. puts progue.attack_power
  56. # => 990
  57.  
  58.  
  59. durid = Druid.new(200, 600, 70)
  60. durid.form = :cat
  61. puts durid.attack_power
  62. # => 1650
Add Comment
Please, Sign In to add comment