Class: RandomGunner

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

Instance Method Summary collapse

Constructor Details

#initialize(targets_remaining, shots_fired) ⇒ RandomGunner

Returns a new instance of RandomGunner.



5
6
7
8
9
# File 'lib/uss_monte_carlo/random_gunner.rb', line 5

def initialize(targets_remaining, shots_fired)
  @targets_remaining = targets_remaining
  @shots_fired = shots_fired
  @shot = @shots_fired.last || ROWS[rand(ROWS.length)] + COLUMNS[rand(COLUMNS.length)]
end

Instance Method Details

#next_shotObject

Raises:



11
12
13
14
15
16
17
18
# File 'lib/uss_monte_carlo/random_gunner.rb', line 11

def next_shot
  raise GunnerError.new("All shots have been taken: #{@shots_fired}") if @shots_fired.length >= (ROWS.length * COLUMNS.length)
  @shot = random_coord
  while @shots_fired.include? @shot
    @shot = random_coord
  end
  @shot
end

#shot_result(coordinates, was_hit, ship_sunk) ⇒ Object



20
21
22
# File 'lib/uss_monte_carlo/random_gunner.rb', line 20

def shot_result(coordinates, was_hit, ship_sunk)
  @shots_fired << coordinates unless @shots_fired.include? coordinates
end