Class: BattleBoats::ConsoleUI

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

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout, input: $stdin, board_formatter: BattleBoats::BoardFormatter.new) ⇒ ConsoleUI

Returns a new instance of ConsoleUI.


7
8
9
10
11
12
13
# File 'lib/battle_boats/console_ui.rb', line 7

def initialize(output: $stdout,
               input: $stdin,
               board_formatter: BattleBoats::BoardFormatter.new)
  @output = output
  @input = input
  @board_formatter = board_formatter
end

Instance Method Details

#display_ally_board(board) ⇒ Object


24
25
26
# File 'lib/battle_boats/console_ui.rb', line 24

def display_ally_board(board)
  output.puts board_formatter.format_board(board, hide_ships: false)
end

#display_board(board) ⇒ Object


19
20
21
22
# File 'lib/battle_boats/console_ui.rb', line 19

def display_board(board)
  hide_ships = !BattleBoats::DEVELOPMENT
  output.puts board_formatter.format_board(board, hide_ships: hide_ships)
end

#display_ship_data(ship:) ⇒ Object


28
29
30
31
# File 'lib/battle_boats/console_ui.rb', line 28

def display_ship_data(ship:)
  output.puts "SHIP: #{ship.name} ALIAS: #{ship.symbol}"
  output.puts "LENGTH: #{ship.length}"
end

#display_status_report(status_report) ⇒ Object


53
54
55
# File 'lib/battle_boats/console_ui.rb', line 53

def display_status_report(status_report)
  output.puts status_report
end

#get_coordinateObject


33
34
35
36
37
38
39
40
41
# File 'lib/battle_boats/console_ui.rb', line 33

def get_coordinate
  output.puts "Target coordinate: "
  user_input = input.gets.chomp
  until board_formatter.valid_coordinate_input?(user_input)
    output.puts "Coordinate invalid."
    user_input = input.gets.chomp
  end
  board_formatter.input_to_coordinate(user_input)
end

#get_orientationObject


43
44
45
46
47
48
49
50
51
# File 'lib/battle_boats/console_ui.rb', line 43

def get_orientation
  output.puts "Orientation [hV]:"
  user_input = input.gets.chomp
  until valid_orientation_input?(user_input)
    output.puts "Orientation invalid."
    user_input = input.gets.chomp
  end
  input_to_orientation(user_input)
end

#greetObject


15
16
17
# File 'lib/battle_boats/console_ui.rb', line 15

def greet
  output.puts "Welcome to Battle Boats!"
end

#loseObject


61
62
63
# File 'lib/battle_boats/console_ui.rb', line 61

def lose
  output.puts "You've lost the game!"
end

#winObject


57
58
59
# File 'lib/battle_boats/console_ui.rb', line 57

def win
  output.puts "You've won the game!"
end