Class: BattleBoats::Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

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 = ""
  @error_messages = []
  @play_area = []
  10.times do
    row = []
    10.times do
      row << BattleBoats::Cell.new
    end
    @play_area << row
  end
end

Instance Attribute Details

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



5
6
7
# File 'lib/battle_boats/board.rb', line 5

def error_messages
  @error_messages
end

#play_areaObject (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_reportObject (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

Returns:

  • (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 @error_messages.empty?
    cell = cell_at(coordinate: coordinate)
    cell.strike
    @status_report = cell.status_report
    true
  else
    false
  end
end