Class: BattleBoats::BoardFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/battle_boats/board_formatter.rb

Instance Method Summary collapse

Instance Method Details

#column_label_to_column_number(column_label) ⇒ Object


51
52
53
# 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) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object


39
40
41
42
43
44
45
# 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) ⇒ Object


47
48
49
# 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) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/battle_boats/board_formatter.rb', line 35

def valid_coordinate_input?(input)
  input =~ /^[A-J][0-9]$/i
end