Class: HomingGunner
- Inherits:
-
Object
- Object
- HomingGunner
- Defined in:
- lib/uss_monte_carlo/homing_gunner.rb
Instance Method Summary collapse
-
#initialize(targets_remaining, hit_location, shots_fired) ⇒ HomingGunner
constructor
A new instance of HomingGunner.
- #next_shot ⇒ Object
- #shot_result(coordinates, was_hit, ship_sunk) ⇒ Object
- #unaccounted_hits ⇒ Object
Constructor Details
#initialize(targets_remaining, hit_location, shots_fired) ⇒ HomingGunner
Returns a new instance of HomingGunner.
5 6 7 8 9 10 11 |
# File 'lib/uss_monte_carlo/homing_gunner.rb', line 5 def initialize(targets_remaining, hit_location, shots_fired) @smallest_ship = targets_remaining.inject(5) { |min, s| SHIP_SIZES[s] < min ? SHIP_SIZES[s] : min } @original_hit = @shot = hit_location @shots_fired = shots_fired @hits = [hit_location] pick_direction_and_orientation end |
Instance Method Details
#next_shot ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/uss_monte_carlo/homing_gunner.rb', line 13 def next_shot raise GunnerError.new("All shots have been taken: #{@shots_fired}") if @shots_fired.length >= (ROWS.length * COLUMNS.length) candidate_shot = move_from(@shot) if candidate_shot == @shot self.send next_strategy candidate_shot = move_from(@shot) end @shot = candidate_shot while @shots_fired.include? @shot @shot = @original_hit self.send next_strategy @shot = move_from(@shot) end @shot end |
#shot_result(coordinates, was_hit, ship_sunk) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/uss_monte_carlo/homing_gunner.rb', line 30 def shot_result(coordinates, was_hit, ship_sunk) @shots_fired << coordinates unless @shots_fired.include? coordinates if ship_sunk remove_hits(ship_sunk) elsif was_hit @hits << coordinates else @shot = @original_hit self.send next_strategy end end |
#unaccounted_hits ⇒ Object
42 43 44 |
# File 'lib/uss_monte_carlo/homing_gunner.rb', line 42 def unaccounted_hits @hits end |