Class: EatTheOcean::Evaluator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opponent_ship_1 = nil, opponent_ship_2 = nil, opponent_ship_3 = nil, opponent_ship_4 = nil, map = nil) ⇒ Evaluator

Returns a new instance of Evaluator.



6
7
8
9
10
11
12
# File 'lib/eat_the_ocean/evaluator.rb', line 6

def initialize(opponent_ship_1 = nil, opponent_ship_2 = nil, opponent_ship_3 = nil, opponent_ship_4 = nil, map = nil)
  @ship_array = [opponent_ship_1, opponent_ship_2, opponent_ship_3, opponent_ship_4]
  @hits_record = []
  @misses_record = []
  @guess_record = []
  @map = map
end

Instance Attribute Details

#guess_recordObject

Returns the value of attribute guess_record.



3
4
5
# File 'lib/eat_the_ocean/evaluator.rb', line 3

def guess_record
  @guess_record
end

#hits_recordObject (readonly)

Returns the value of attribute hits_record.



4
5
6
# File 'lib/eat_the_ocean/evaluator.rb', line 4

def hits_record
  @hits_record
end

#mapObject

Returns the value of attribute map.



3
4
5
# File 'lib/eat_the_ocean/evaluator.rb', line 3

def map
  @map
end

#misses_recordObject (readonly)

Returns the value of attribute misses_record.



4
5
6
# File 'lib/eat_the_ocean/evaluator.rb', line 4

def misses_record
  @misses_record
end

#ship_arrayObject

Returns the value of attribute ship_array.



3
4
5
# File 'lib/eat_the_ocean/evaluator.rb', line 3

def ship_array
  @ship_array
end

Instance Method Details

#hit(user_coordinate) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eat_the_ocean/evaluator.rb', line 14

def hit(user_coordinate)
  hit_it = false
  @ship_array.each do |ship|
    if ship
      @guess_record << user_coordinate
      if ship.coordinates.include?(user_coordinate)
        @map.grid_mark(user_coordinate,"🍣")
        @hits_record << user_coordinate
        ship.hits += 1
        @guess_record.uniq!
        hit_it = true
        if ship.coordinates.length == ship.hits
          ship.sunk = 1
        end
        break
      else
        @misses_record << user_coordinate
        @map.grid_mark(user_coordinate,"💦")
        @misses_record.uniq!
        hit_it = false
      end
    end
  end
  @guess_record.uniq!
  hit_it
end