Class: SeaBattle::Cell
- Inherits:
-
Object
- Object
- SeaBattle::Cell
- Defined in:
- lib/sea_battle/cell.rb
Overview
It’s Cell of Board
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #add_ship ⇒ Object
- #attack ⇒ Object
-
#initialize(status = 1) ⇒ Cell
constructor
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.
- #is_attacked? ⇒ Boolean
- #is_empty? ⇒ Boolean
- #is_in_ship? ⇒ Boolean
- #is_selected? ⇒ Boolean
- #is_sunk? ⇒ Boolean
- #reset_cell ⇒ Object
- #sunk ⇒ Object
- #switch_select ⇒ Object
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
#status ⇒ Object (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_ship ⇒ Object
20 21 22 |
# File 'lib/sea_battle/cell.rb', line 20 def add_ship @status += 2 unless is_in_ship? end |
#attack ⇒ Object
24 25 26 |
# File 'lib/sea_battle/cell.rb', line 24 def attack @status += 4 unless is_attacked? end |
#is_attacked? ⇒ Boolean
36 37 38 |
# File 'lib/sea_battle/cell.rb', line 36 def is_attacked? @status & 4 == 4 end |
#is_empty? ⇒ Boolean
40 41 42 |
# File 'lib/sea_battle/cell.rb', line 40 def is_empty? @status & 1 == 1 end |
#is_in_ship? ⇒ Boolean
44 45 46 |
# File 'lib/sea_battle/cell.rb', line 44 def is_in_ship? @status & 2 == 2 end |
#is_selected? ⇒ Boolean
48 49 50 |
# File 'lib/sea_battle/cell.rb', line 48 def is_selected? @status & 8 == 8 end |
#is_sunk? ⇒ Boolean
52 53 54 |
# File 'lib/sea_battle/cell.rb', line 52 def is_sunk? @status & 16 == 16 end |
#reset_cell ⇒ Object
56 57 58 |
# File 'lib/sea_battle/cell.rb', line 56 def reset_cell @status = 1 end |
#sunk ⇒ Object
28 29 30 |
# File 'lib/sea_battle/cell.rb', line 28 def sunk @status += 16 unless is_sunk? end |
#switch_select ⇒ Object
32 33 34 |
# File 'lib/sea_battle/cell.rb', line 32 def switch_select is_selected? ? @status -= 8 : @status += 8 end |