Class: SvatokCodebreaker::ConsoleForGame

Inherits:
Object
  • Object
show all
Defined in:
lib/svatok_codebreaker/console_game.rb

Instance Method Summary collapse

Constructor Details

#initializeConsoleForGame

Returns a new instance of ConsoleForGame.



3
4
5
6
7
# File 'lib/svatok_codebreaker/console_game.rb', line 3

def initialize
  @exit_game = false
  @menu_commands = %w(exit restart hint save)
  @console_message = GameMessage.new
end

Instance Method Details

#answer_valid?(answer) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/svatok_codebreaker/console_game.rb', line 37

def answer_valid?(answer)
  @game.valid_guess?(answer) || @menu_commands.include?(answer)
end

#command_exitObject



41
42
43
44
# File 'lib/svatok_codebreaker/console_game.rb', line 41

def command_exit
  @exit_game = true
  @console_message.show(:exit_game)
end

#command_hintObject



51
52
53
54
# File 'lib/svatok_codebreaker/console_game.rb', line 51

def command_hint
  return @console_message.show(:no_hint) unless @game.hint
  @game.show_hint
end

#command_restartObject



46
47
48
49
# File 'lib/svatok_codebreaker/console_game.rb', line 46

def command_restart
  @game = Game.new(@player_name)
  @console_message.show(:restart_game)
end

#command_saveObject



56
57
58
59
60
# File 'lib/svatok_codebreaker/console_game.rb', line 56

def command_save
  return @console_message.show(:not_available) unless @game.end_of_game?
  @game.save_game
  @console_message.show(:saved)
end

#game_completitionObject



32
33
34
35
# File 'lib/svatok_codebreaker/console_game.rb', line 32

def game_completition
  return @console_message.show(:win) if @game.marking_guess == '++++'
  @console_message.show(:lose, @game.get_game_data)
end

#prepare_gameObject



9
10
11
12
13
14
15
# File 'lib/svatok_codebreaker/console_game.rb', line 9

def prepare_game
  puts @console_message.show(:about)
  puts @console_message.show(:login)
  @player_name = gets.chomp
  puts @console_message.show(:start)
  run_game
end

#run_gameObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/svatok_codebreaker/console_game.rb', line 17

def run_game
  @game = Game.new(@player_name)
  until @exit_game
    answer = gets.chomp
    begin
      raise puts @console_message.show(:not_valid_answer) unless answer_valid?(answer)
      raise puts send(('command_' + answer.downcase).to_sym) if @menu_commands.include?(answer)
      @game.submit_guess(answer)
      puts @game.end_of_game? ? game_completition : @console_message.show(:next_step, @game.get_game_data)
    rescue
      next
    end
  end
end