Class: BattleBoats::Cell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCell

Returns a new instance of Cell.



9
10
11
12
# File 'lib/battle_boats/cell.rb', line 9

def initialize
  @hit = false
  @occupant = BattleBoats::NullShip.new
end

Instance Attribute Details

#occupantObject

Returns the value of attribute occupant.



7
8
9
# File 'lib/battle_boats/cell.rb', line 7

def occupant
  @occupant
end

Instance Method Details

#hit?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/battle_boats/cell.rb', line 14

def hit?
  @hit
end

#occupied?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/battle_boats/cell.rb', line 42

def occupied?
  !occupant.empty?
end

#status_reportObject



33
34
35
36
37
38
39
40
# File 'lib/battle_boats/cell.rb', line 33

def status_report
  occupant_name = occupant.name
  if occupant.sunk?
    "You sunk my #{occupant_name}!"
  elsif hit?
    "You hit my #{occupant_name}!"
  end
end

#strikeObject



18
19
20
21
22
23
# File 'lib/battle_boats/cell.rb', line 18

def strike
  if !hit?
    occupant.hit
    @hit = true
  end
end

#to_sObject



25
26
27
28
29
30
31
# File 'lib/battle_boats/cell.rb', line 25

def to_s
  if hit?
    occupant.symbol
  else
    ".".blue
  end
end