Class: SeaBattle::Cell

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

Overview

It’s Cell of Board

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status = 1) ⇒ Cell

status value: 1 -> empty field 2 -> field is part of ship 4 -> attacked field 8 -> is only selected by user 16 -> is sunk 6 -> attacked field and exsist ship



16
17
18
# File 'lib/sea_battle/cell.rb', line 16

def initialize(status = 1)
  @status = status
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#add_shipObject



20
21
22
# File 'lib/sea_battle/cell.rb', line 20

def add_ship
  @status += 2 unless is_in_ship?
end

#attackObject



24
25
26
# File 'lib/sea_battle/cell.rb', line 24

def attack
  @status += 4 unless is_attacked?
end

#is_attacked?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/sea_battle/cell.rb', line 36

def is_attacked?
  @status & 4 == 4
end

#is_empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/sea_battle/cell.rb', line 40

def is_empty?
  @status & 1 == 1
end

#is_in_ship?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/sea_battle/cell.rb', line 44

def is_in_ship?
  @status & 2 == 2
end

#is_selected?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sea_battle/cell.rb', line 48

def is_selected?
  @status & 8 == 8
end

#is_sunk?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sea_battle/cell.rb', line 52

def is_sunk?
  @status & 16 == 16
end

#reset_cellObject



56
57
58
# File 'lib/sea_battle/cell.rb', line 56

def reset_cell
  @status = 1
end

#sunkObject



28
29
30
# File 'lib/sea_battle/cell.rb', line 28

def sunk
  @status += 16 unless is_sunk?
end

#switch_selectObject



32
33
34
# File 'lib/sea_battle/cell.rb', line 32

def switch_select
  is_selected? ? @status -= 8 : @status += 8
end