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

#initialize(fleet: BattleBoats::Fleet.new) ⇒ Board

Returns a new instance of Board.


8
9
10
11
12
13
# File 'lib/battle_boats/board.rb', line 8

def initialize(fleet: BattleBoats::Fleet.new)
  @fleet = fleet
  @status_report = ""
  @board_size = 10
  @play_area = create_play_area
end

Instance Attribute Details

#board_sizeObject (readonly)

Returns the value of attribute board_size


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

def board_size
  @board_size
end

#fleetObject (readonly)

Returns the value of attribute fleet


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

def fleet
  @fleet
end

#play_areaObject (readonly)

Returns the value of attribute play_area


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

def play_area
  @play_area
end

#status_reportObject (readonly)

Returns the value of attribute status_report


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

def status_report
  @status_report
end

Instance Method Details

#attempt_to_deploy_ship(ship:, coordinate:, orientation:) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/battle_boats/board.rb', line 29

def attempt_to_deploy_ship(ship:, coordinate:, orientation:)
  cells = Array.new(ship.length) do |offset|
    if orientation == :horizontal
      next_coordinate = coordinate.right(offset: offset)
    elsif orientation == :vertical
      next_coordinate = coordinate.up(offset: offset)
    else
      return nil
    end
    cell_at(coordinate: next_coordinate)
  end

  if cells_occupiable?(cells: cells)
    cells.each do |cell|
      cell.occupant = ship
    end
  end
end

#cell_at(coordinate:) ⇒ Object


64
65
66
67
68
# File 'lib/battle_boats/board.rb', line 64

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)

60
61
62
# File 'lib/battle_boats/board.rb', line 60

def game_over?
  fleet.ships.all?(&:sunk?)
end

#get_random_coordinateObject


70
71
72
# File 'lib/battle_boats/board.rb', line 70

def get_random_coordinate
  BattleBoats::Coordinate.random(row: 0...board_size, column: 0...board_size)
end

#place_ships_randomlyObject


15
16
17
18
19
20
21
22
23
# File 'lib/battle_boats/board.rb', line 15

def place_ships_randomly
  @fleet.ships.each do |ship|
    until ship_deployed?(ship: ship)
      coordinate = get_random_coordinate
      orientation = %i[horizontal vertical].sample
      attempt_to_deploy_ship(ship: ship, coordinate: coordinate, orientation: orientation)
    end
  end
end

#ship_deployed?(ship:) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/battle_boats/board.rb', line 25

def ship_deployed?(ship:)
  play_area.flatten.map(&:occupant).include?(ship)
end

#strike_position(coordinate:) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/battle_boats/board.rb', line 48

def strike_position(coordinate:)
  cell = cell_at(coordinate: coordinate)
  if cell.hit?
    @status_report = "That position has already been hit"
    false
  else
    cell.strike
    @status_report = cell.status_report
    true
  end
end