Class: MiddleEarthCombat
- Inherits:
-
Object
- Object
- MiddleEarthCombat
- Defined in:
- lib/middle_earth_challenge_simulator.rb
Instance Attribute Summary collapse
-
#character1 ⇒ Object
readonly
Returns the value of attribute character1.
-
#character2 ⇒ Object
readonly
Returns the value of attribute character2.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#victor ⇒ Object
readonly
Returns the value of attribute victor.
Class Method Summary collapse
Instance Method Summary collapse
- #do_round(a1 = Attack.new(@character1.challenge_rank), a2 = Attack.new(@character2.challenge_rank), d = MiddleEarthCombat.get_damage(a1, a2)) ⇒ Object
- #fight ⇒ Object
-
#initialize(c1, c2) ⇒ MiddleEarthCombat
constructor
A new instance of MiddleEarthCombat.
- #to_s ⇒ Object
Constructor Details
#initialize(c1, c2) ⇒ MiddleEarthCombat
Returns a new instance of MiddleEarthCombat.
82 83 84 85 86 87 88 |
# File 'lib/middle_earth_challenge_simulator.rb', line 82 def initialize (c1, c2) raise ArgumentError if c1.health <= 0 || c2.health <= 0 @character1 = c1 @character2 = c2 @victor = nil @results = [] end |
Instance Attribute Details
#character1 ⇒ Object (readonly)
Returns the value of attribute character1.
80 81 82 |
# File 'lib/middle_earth_challenge_simulator.rb', line 80 def character1 @character1 end |
#character2 ⇒ Object (readonly)
Returns the value of attribute character2.
80 81 82 |
# File 'lib/middle_earth_challenge_simulator.rb', line 80 def character2 @character2 end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
80 81 82 |
# File 'lib/middle_earth_challenge_simulator.rb', line 80 def results @results end |
#victor ⇒ Object (readonly)
Returns the value of attribute victor.
80 81 82 |
# File 'lib/middle_earth_challenge_simulator.rb', line 80 def victor @victor end |
Class Method Details
.get_damage(a1, a2) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/middle_earth_challenge_simulator.rb', line 91 def MiddleEarthCombat.get_damage(a1, a2) #tie return 0 if (a1 == a2) max_damage = a1.attack_value - a2.attack_value if a1 > a2 max_damage = a2.attack_value - a1.attack_value if a2 > a1 return rand(max_damage) + 1 end |
Instance Method Details
#do_round(a1 = Attack.new(@character1.challenge_rank), a2 = Attack.new(@character2.challenge_rank), d = MiddleEarthCombat.get_damage(a1, a2)) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/middle_earth_challenge_simulator.rb', line 100 def do_round(a1 = Attack.new(@character1.challenge_rank), a2 = Attack.new(@character2.challenge_rank), d = MiddleEarthCombat.get_damage(a1, a2)) raise RuntimeError if @character1.health <= 0 || @character2.health <= 0 if a1 > a2 @character2.health = @character2.health - d elsif a2 > a1 @character1.health = @character1.health - d else #tie end @results << CombatRound.new(a1, a2, d, @character1, @character2) end |
#fight ⇒ Object
116 117 118 119 120 121 |
# File 'lib/middle_earth_challenge_simulator.rb', line 116 def fight until self.character1.health <= 0 || self.character2.health <= 0 do_round end @victor = self.character1.health > 0 ? self.character1 : self.character2 end |
#to_s ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/middle_earth_challenge_simulator.rb', line 123 def to_s result = "============================================================\n" \ "MiddleEarthCombat\n" \ "Victor: #{@victor}\n" \ "after #{@results.size} rounds\n" \ "============================================================\n" \ "Combat Details\n" @results.each do |r| result << "------------------------------------------\n" result << "Damage #{r.damage}\n" result << "#{r.character1} attack was #{r.attack1}\n" result << "#{r.character2} attack was #{r.attack2}\n" end return result end |