Class: BattleBoats::Board
- Inherits:
-
Object
- Object
- BattleBoats::Board
- Defined in:
- lib/battle_boats/board.rb
Instance Attribute Summary collapse
-
#error_messages ⇒ Object
readonly
Returns the value of attribute error_messages.
-
#play_area ⇒ Object
readonly
Returns the value of attribute play_area.
-
#status_report ⇒ Object
readonly
Returns the value of attribute status_report.
Instance Method Summary collapse
- #cell_at(coordinate:) ⇒ Object
- #game_over? ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #place_ship_horizontally(coordinate:, ship:) ⇒ Object
- #place_ship_vertically(coordinate:, ship:) ⇒ Object
- #strike_position(coordinate:) ⇒ Object
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/battle_boats/board.rb', line 7 def initialize @status_report = "" = [] @play_area = [] 10.times do row = [] 10.times do row << BattleBoats::Cell.new end @play_area << row end end |
Instance Attribute Details
#error_messages ⇒ Object (readonly)
Returns the value of attribute error_messages.
5 6 7 |
# File 'lib/battle_boats/board.rb', line 5 def end |
#play_area ⇒ Object (readonly)
Returns the value of attribute play_area.
5 6 7 |
# File 'lib/battle_boats/board.rb', line 5 def play_area @play_area end |
#status_report ⇒ Object (readonly)
Returns the value of attribute status_report.
5 6 7 |
# File 'lib/battle_boats/board.rb', line 5 def status_report @status_report end |
Instance Method Details
#cell_at(coordinate:) ⇒ Object
36 37 38 39 40 |
# File 'lib/battle_boats/board.rb', line 36 def cell_at(coordinate:) if within_range?(coordinate: coordinate) @play_area[coordinate.row.to_i][coordinate.column.to_i] end end |
#game_over? ⇒ Boolean
32 33 34 |
# File 'lib/battle_boats/board.rb', line 32 def game_over? false end |
#place_ship_horizontally(coordinate:, ship:) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/battle_boats/board.rb', line 42 def place_ship_horizontally(coordinate:, ship:) cells_to_occupy = Array.new(ship.length) do |offset| cell_at(coordinate: coordinate.right(offset: offset)) end occupy_cells(cells: cells_to_occupy, ship: ship) end |
#place_ship_vertically(coordinate:, ship:) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/battle_boats/board.rb', line 50 def place_ship_vertically(coordinate:, ship:) cells_to_occupy = Array.new(ship.length) do |offset| cell_at(coordinate: coordinate.up(offset: offset)) end occupy_cells(cells: cells_to_occupy, ship: ship) end |
#strike_position(coordinate:) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/battle_boats/board.rb', line 20 def strike_position(coordinate:) validate_position(coordinate: coordinate) if .empty? cell = cell_at(coordinate: coordinate) cell.strike @status_report = cell.status_report true else false end end |