Class: BattleBoats::Ship

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, length:, symbol: "O") ⇒ Ship

Returns a new instance of Ship.



8
9
10
11
12
13
# File 'lib/battle_boats/ship.rb', line 8

def initialize(name:, length:, symbol: "O")
  @name = name
  @length = length
  @symbol = symbol.red
  @hits = 0
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/battle_boats/ship.rb', line 6

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/battle_boats/ship.rb', line 6

def name
  @name
end

#symbolObject (readonly)

Returns the value of attribute symbol.



6
7
8
# File 'lib/battle_boats/ship.rb', line 6

def symbol
  @symbol
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/battle_boats/ship.rb', line 15

def empty?
  false
end

#hitObject



23
24
25
# File 'lib/battle_boats/ship.rb', line 23

def hit
  @hits += 1
end

#hit_countObject



19
20
21
# File 'lib/battle_boats/ship.rb', line 19

def hit_count
  @hits
end

#sunk?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/battle_boats/ship.rb', line 27

def sunk?
  hit_count == length
end