Class: SeaBattle::RandomShip
- Inherits:
-
Object
- Object
- SeaBattle::RandomShip
- Includes:
- Support
- Defined in:
- lib/sea_battle/random_ship.rb
Overview
It’s random positions for new ship
Instance Method Summary collapse
-
#add_ship ⇒ Object
Add new Ship on board.
- #add_ship_of(direct) ⇒ Object
- #area_ship_positions(row, column, direct) ⇒ Object
-
#initialize(board, length = 1) ⇒ RandomShip
constructor
A new instance of RandomShip.
- #is_area_empty?(row, column, direct) ⇒ Boolean
- #ship_positions(row, column, direct) ⇒ Object
Methods included from Support
Constructor Details
#initialize(board, length = 1) ⇒ RandomShip
Returns a new instance of RandomShip.
10 11 12 13 14 15 |
# File 'lib/sea_battle/random_ship.rb', line 10 def initialize(board, length = 1) @board = board.board @length = length @horizontal = board.horizontal @vertical = board.vertical end |
Instance Method Details
#add_ship ⇒ Object
Add new Ship on board
18 19 20 21 22 23 24 25 26 |
# File 'lib/sea_battle/random_ship.rb', line 18 def add_ship if Random.rand(2) == 0 return if add_ship_of(:vertical) return if add_ship_of(:horizontal) else return if add_ship_of(:horizontal) return if add_ship_of(:vertical) end end |
#add_ship_of(direct) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sea_battle/random_ship.rb', line 55 def add_ship_of(direct) mixed_board_positions.each do |row, column| next if direct.eql?(:vertical) and row + @length - 1 >= @vertical next if direct.eql?(:horizontal) and column + @length - 1 >= @horizontal if is_area_empty?(row, column, direct) positions = ship_positions(row, column, direct) positions.map do |row, column| @board[row][column].add_ship end return positions end end nil end |
#area_ship_positions(row, column, direct) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sea_battle/random_ship.rb', line 41 def area_ship_positions(row, column, direct) result = [] first_row = row - 1 first_column = column - 1 last_row, last_column = row + 1, column + 1 last_row += @length - 1 if direct.eql?(:vertical) last_column += @length - 1 if direct.eql?(:horizontal) vertical_range = (first_row..last_row).to_a & (0...@vertical).to_a horizontal_range = (first_column..last_column).to_a & (0...@horizontal).to_a vertical_range.product(horizontal_range) end |
#is_area_empty?(row, column, direct) ⇒ Boolean
71 72 73 74 75 76 |
# File 'lib/sea_battle/random_ship.rb', line 71 def is_area_empty?(row, column, direct) area_ship_positions(row, column, direct).each do |row_index, column_index| return false if @board[row_index][column_index].is_in_ship? end true end |
#ship_positions(row, column, direct) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sea_battle/random_ship.rb', line 28 def ship_positions(row, column, direct) result = [] @length.times do |offset| if direct.eql?(:vertical) result << [row + offset, column] if row + offset < @vertical else result << [row, column + offset] if column + offset < @horizontal end end result end |