Class: CodebreakerVk::GameConsole

Inherits:
Object
  • Object
show all
Includes:
Database, Output, Validation
Defined in:
lib/codebreaker_vk/game_console.rb

Constant Summary collapse

HINT =
'hint'
SAVE =
'save'
INPUT_DATA =
/^[1-6]{4}$/.freeze

Constants included from Database

Database::SEED

Constants included from Validation

Validation::MAX_LETTERS, Validation::MIN_LETTERS

Instance Method Summary collapse

Methods included from Output

#rules, #start_info, #summary_info

Methods included from Database

#load, #save, #save_results

Methods included from Validation

#valid_string_length?

Constructor Details

#initialize(name, difficulty) ⇒ GameConsole

Returns a new instance of GameConsole.



13
14
15
# File 'lib/codebreaker_vk/game_console.rb', line 13

def initialize(name, difficulty)
  @game = Game.new(name: name, difficulty: difficulty)
end

Instance Method Details

#closeObject



49
50
51
52
# File 'lib/codebreaker_vk/game_console.rb', line 49

def close
  puts I18n.t(:goodbye)
  exit
end

#end_gameObject



39
40
41
42
43
44
45
46
47
# File 'lib/codebreaker_vk/game_console.rb', line 39

def end_game
  if @game.win?
    puts I18n.t(:win)
    puts I18n.t(:save)
    save_results if gets.chomp == SAVE
  else
    puts I18n.t(:lose)
  end
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/codebreaker_vk/game_console.rb', line 17

def start
  loop do
    break if @game.attempts.zero? || @game.win?

    start_info(@game.attempts, @game.hints)
    input = gets.chomp
    case input
    when GameMenu::EXIT then break close
    when HINT then next puts @game.use_hint
    when INPUT_DATA then puts @game.check(input)
    else puts I18n.t(:wrong_process)
    end
  end
  puts I18n.t(:game_over)
  statistics
end

#statisticsObject



34
35
36
37
# File 'lib/codebreaker_vk/game_console.rb', line 34

def statistics
  summary_info(@game.secret)
  end_game
end