Class: SeaBattle::Board
- Inherits:
-
Object
- Object
- SeaBattle::Board
- Includes:
- Support
- Defined in:
- lib/sea_battle/board.rb
Overview
It’s Board of game Sea Battle
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#horizontal ⇒ Object
readonly
Returns the value of attribute horizontal.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#vertical ⇒ Object
readonly
Returns the value of attribute vertical.
Instance Method Summary collapse
-
#activate_board ⇒ Object
When all ships are on board then board can be activated Return true when board was activated.
- #attack(row, column) ⇒ Object
-
#initialize(board = "1" * 100, status = :initialized) ⇒ Board
constructor
A new instance of Board.
- #is_attacked?(row, column) ⇒ Boolean
- #is_in_ship?(row, column) ⇒ Boolean
-
#is_sunken_ship?(row, column) ⇒ Boolean
Return true if on position (row, column) is the ship which is sunked Return false if on position (row, column) is not the ship which is sunked or don’t exist the ship.
-
#random_position ⇒ Object
Return position to attack (have not attacked).
-
#random_ships ⇒ Object
Set ships on the board (random positions).
-
#ship_positions(row, column) ⇒ Object
Return array of position ship if it will be on position [row, column].
Methods included from Support
Constructor Details
#initialize(board = "1" * 100, status = :initialized) ⇒ Board
Returns a new instance of Board.
14 15 16 17 18 19 20 21 |
# File 'lib/sea_battle/board.rb', line 14 def initialize(board = "1" * 100, status = :initialized) @horizontal, @vertical = 10, 10 load_board(board) @status = status check_board end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
12 13 14 |
# File 'lib/sea_battle/board.rb', line 12 def board @board end |
#horizontal ⇒ Object (readonly)
Returns the value of attribute horizontal.
12 13 14 |
# File 'lib/sea_battle/board.rb', line 12 def horizontal @horizontal end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
12 13 14 |
# File 'lib/sea_battle/board.rb', line 12 def status @status end |
#vertical ⇒ Object (readonly)
Returns the value of attribute vertical.
12 13 14 |
# File 'lib/sea_battle/board.rb', line 12 def vertical @vertical end |
Instance Method Details
#activate_board ⇒ Object
When all ships are on board then board can be activated Return true when board was activated
25 26 27 28 29 30 31 32 33 |
# File 'lib/sea_battle/board.rb', line 25 def activate_board result = 0 @board.flatten.each do |cell| result += 1 if cell.is_in_ship? end return false unless result == 20 @status = :activated true end |
#attack(row, column) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/sea_battle/board.rb', line 35 def attack(row, column) if @status == :activated board[row][column].attack is_sunken_ship?(row, column) is_finished? end end |
#is_attacked?(row, column) ⇒ Boolean
43 44 45 |
# File 'lib/sea_battle/board.rb', line 43 def is_attacked?(row, column) board[row][column].is_attacked? end |
#is_in_ship?(row, column) ⇒ Boolean
47 48 49 |
# File 'lib/sea_battle/board.rb', line 47 def is_in_ship?(row, column) board[row][column].is_in_ship? end |
#is_sunken_ship?(row, column) ⇒ Boolean
Return true if on position (row, column) is the ship which is sunked Return false if on position (row, column) is not the ship which is sunked or don’t exist the ship
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sea_battle/board.rb', line 54 def is_sunken_ship?(row, column) positions = ship_positions(row, column) return false if positions.empty? is_sunk = true positions.each do |position_row, position_column| is_sunk = false unless board[position_row][position_column].is_attacked? end if is_sunk positions.each do |position_row, position_column| board[position_row][position_column].sunk end end is_sunk end |
#random_position ⇒ Object
Return position to attack (have not attacked)
70 71 72 73 74 |
# File 'lib/sea_battle/board.rb', line 70 def random_position mixed_board_positions.each do |row, column| return [row, column] unless is_attacked?(row, column) end end |
#random_ships ⇒ Object
Set ships on the board (random positions)
77 78 79 80 81 82 83 |
# File 'lib/sea_battle/board.rb', line 77 def random_ships return unless @status == :initialized reset_board [1, 1, 1, 1, 2, 2, 2, 3, 3, 4].each do |length| RandomShip.new(self, length).add_ship end end |
#ship_positions(row, column) ⇒ Object
Return array of position ship if it will be on position [row, column]
86 87 88 89 90 91 |
# File 'lib/sea_battle/board.rb', line 86 def ship_positions(row, column) horizontal = horizontal_ship_position(row, column) vertical = vertical_ship_position(row, column) return horizontal if horizontal.size > vertical.size vertical end |