class BattleBoats::BoardFormatter
Public Instance Methods
column_label_to_column_number(column_label)
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 51 def column_label_to_column_number(column_label) column_label.to_i end
format_board(board, hide_ships: true)
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 6 def format_board(board, hide_ships: true) board_string = horizontal_line board_string << newline board_string << column_header board_string << horizontal_line board_string << newline board.play_area.each_with_index do |row, row_number| board_string << pipe board_string << " #{row_labels[row_number]} " board_string << pipe row.each do |cell| board_string << if cell.occupied? && cell.hit? " #{cell.occupant.symbol.red} " elsif cell.occupied? && !hide_ships " #{cell.occupant.symbol.green} " elsif cell.hit? " #{'X'.yellow} " else " #{'~'.blue} " end board_string << pipe end board_string << newline board_string << horizontal_line board_string << newline end board_string end
input_to_coordinate(input)
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 39 def input_to_coordinate(input) input_row = input[0] input_column = input[1] row = row_label_to_row_number(input_row) column = column_label_to_column_number(input_column) BattleBoats::Coordinate.new(row: row, column: column) end
row_label_to_row_number(row_label)
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 47 def row_label_to_row_number(row_label) row_labels.index(row_label.upcase) end
valid_coordinate_input?(input)
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 35 def valid_coordinate_input?(input) input =~ /^[A-J][0-9]$/i end
Private Instance Methods
column_header()
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 57 def column_header "| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n" end
horizontal_line()
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 69 def horizontal_line "-" * 67 end
newline()
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 65 def newline "\n" end
pipe()
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 73 def pipe "|" end
row_labels()
click to toggle source
# File lib/battle_boats/board_formatter.rb, line 61 def row_labels ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] end