class BattleBoats::Board

Attributes

board_size[R]
fleet[R]
play_area[R]
status_report[R]

Public Class Methods

new(fleet: BattleBoats::Fleet.new) click to toggle source
# 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

Public Instance Methods

attempt_to_deploy_ship(ship:, coordinate:, orientation:) click to toggle source
# 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:) click to toggle source
# 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?() click to toggle source
# File lib/battle_boats/board.rb, line 60
def game_over?
  fleet.ships.all?(&:sunk?)
end
get_random_coordinate() click to toggle source
# 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_randomly() click to toggle source
# 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:) click to toggle source
# File lib/battle_boats/board.rb, line 25
def ship_deployed?(ship:)
  play_area.flatten.map(&:occupant).include?(ship)
end
strike_position(coordinate:) click to toggle source
# 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

Private Instance Methods

cells_occupiable?(cells:) click to toggle source
# File lib/battle_boats/board.rb, line 86
def cells_occupiable?(cells:)
  cells.none?(&:nil?) && cells.none?(&:occupied?)
end
create_play_area() click to toggle source
# File lib/battle_boats/board.rb, line 76
def create_play_area
  Array.new(board_size) do
    row = []
    board_size.times do
      row << BattleBoats::Cell.new
    end
    row
  end
end
within_range?(coordinate:) click to toggle source
# File lib/battle_boats/board.rb, line 90
def within_range?(coordinate:)
  (0...board_size).cover?(coordinate.row) &&
    (0...board_size).cover?(coordinate.column)
end