Class: SeaBattle::GUI

Inherits:
Object
  • Object
show all
Defined in:
lib/sea_battle/gui.rb

Overview

User can play with computer on console

Instance Method Summary collapse

Constructor Details

#initializeGUI

Returns a new instance of GUI.



11
12
13
14
15
16
17
18
19
# File 'lib/sea_battle/gui.rb', line 11

def initialize
  @user_board = ::SeaBattle::Board.new
  @computer_board = ::SeaBattle::Board.new
  @computer_board.random_ships
  @computer_board.activate_board
  @sea_battle = ::SeaBattle.new(@user_board, @computer_board)
  set_default_keyboard
  push_message("Initialized SeaBattle")
end

Instance Method Details

#playObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sea_battle/gui.rb', line 21

def play
  while true and not @keyboard[:exit]
    update_screen
    user_command
    next if @keyboard[:error]
    if @keyboard[:position]
      attack_on(@row, @column, :first_player)
      break unless @sea_battle.winner_is.nil?
      while @sea_battle.active_user == :second_player
        sleep 1 + rand
        row, column = @computer_board.random_position
        attack_on(row, column, :second_player)
      end
      break unless @sea_battle.winner_is.nil?
      @keyboard[:position] = false
    end
  end
end