Class: MiddleEarthChallengeSimulator

Inherits:
Object
  • Object
show all
Defined in:
lib/middle_earth_challenge_simulator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c1, c2, rounds) ⇒ MiddleEarthChallengeSimulator

Returns a new instance of MiddleEarthChallengeSimulator.

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
# File 'lib/middle_earth_challenge_simulator.rb', line 145

def initialize (c1, c2, rounds)
  raise ArgumentError if c1.health <= 0 || c2.health <= 0
  
  @character1 = c1.dup
  @character2 = c2.dup
  @num_rounds = rounds    
  @character1_wins = 0
  @character2_wins = 0
end

Instance Attribute Details

#character1Object (readonly)

Returns the value of attribute character1.



142
143
144
# File 'lib/middle_earth_challenge_simulator.rb', line 142

def character1
  @character1
end

#character1_winsObject (readonly)

Returns the value of attribute character1_wins.



142
143
144
# File 'lib/middle_earth_challenge_simulator.rb', line 142

def character1_wins
  @character1_wins
end

#character2Object (readonly)

Returns the value of attribute character2.



142
143
144
# File 'lib/middle_earth_challenge_simulator.rb', line 142

def character2
  @character2
end

#character2_winsObject (readonly)

Returns the value of attribute character2_wins.



142
143
144
# File 'lib/middle_earth_challenge_simulator.rb', line 142

def character2_wins
  @character2_wins
end

#num_roundsObject (readonly)

Returns the value of attribute num_rounds.



142
143
144
# File 'lib/middle_earth_challenge_simulator.rb', line 142

def num_rounds
  @num_rounds
end

Class Method Details

.run(args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/middle_earth_challenge_simulator.rb', line 165

def MiddleEarthChallengeSimulator.run(args)
  unless args.size == 5
    puts "\nUSAGE:\n\tmiddle_earth_challenge_simulator.rb rank1 health1 rank2 health2 rounds\n"
    puts "EXAMPLE:\n\tmiddle_earth_challenge_simulator.rb 50 100 30 100 10000"
    return -1
  end
  
  c1 = Character.new("Character #1", args.shift.to_i, args.shift.to_i)
  c2 = Character.new("Character #2", args.shift.to_i, args.shift.to_i)
  rounds = args.shift.to_i
  
  puts "Simulating #{rounds} rounds of combat between #{c1} and #{c2}\n"
  
  mec = MiddleEarthChallengeSimulator.new(c1, c2, rounds)
  mec.simulate
  
  puts "#{mec.character1} won #{mec.character1_wins}"
  puts "#{mec.character2} won #{mec.character2_wins}"        
  
end

Instance Method Details

#simulateObject



155
156
157
158
159
160
161
162
163
# File 'lib/middle_earth_challenge_simulator.rb', line 155

def simulate
  (1..@num_rounds).each do
    me_combat = MiddleEarthCombat.new(@character1.dup, @character2.dup)
    me_combat.fight
    @character1_wins += 1 if (@character1.name == me_combat.victor.name)
    @character2_wins += 1 if (@character2.name == me_combat.victor.name)             
    
  end
end