Class: BattleBoats::Engine

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

Instance Method Summary collapse

Constructor Details

#initialize(interface: BattleBoats::ConsoleUI.new, enemy_board: BattleBoats::Board.new, ally_board: BattleBoats::Board.new) ⇒ Engine

Returns a new instance of Engine.


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

def initialize(interface: BattleBoats::ConsoleUI.new,
               enemy_board: BattleBoats::Board.new,
               ally_board: BattleBoats::Board.new)
  @interface = interface
  @enemy_board = enemy_board
  @ally_board = ally_board
end

Instance Method Details

#place_ships_manuallyObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/battle_boats/engine.rb', line 14

def place_ships_manually
  ally_board.fleet.ships.each do |ship|
    until ally_board.ship_deployed?(ship: ship)
      interface.display_ally_board(ally_board)
      interface.display_ship_data(ship: ship)

      coordinate = interface.get_coordinate
      orientation = interface.get_orientation

      ally_board.attempt_to_deploy_ship(ship: ship,
                                        coordinate: coordinate,
                                        orientation: orientation)
    end
  end
end

#startObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/battle_boats/engine.rb', line 30

def start
  interface.greet
  until game_over?

    interface.display_status_report(ally_board.status_report)
    interface.display_ally_board(ally_board)

    interface.display_status_report(enemy_board.status_report)
    interface.display_board(enemy_board)

    coordinate = interface.get_coordinate
    until enemy_board.strike_position(coordinate: coordinate)
      interface.display_status_report(enemy_board.status_report)
      coordinate = interface.get_coordinate
    end

    enemy_coordinate = enemy_board.get_random_coordinate
    until ally_board.strike_position(coordinate: enemy_coordinate)
      enemy_coordinate = enemy_board.get_random_coordinate
    end

  end
  end_game
end