class Console include Codebreaker include Output include Database COMMANDS = { start: 'start', rules: 'rules', stats: 'stats', exit: 'exit', hints: 'hints' }.freeze YES = 'y'.freeze def run Messages.show_greeting loop do case gets.chomp.downcase when COMMANDS[:start] then return register when COMMANDS[:rules] then Messages.show_rules when COMMANDS[:stats] then Statistics.show when COMMANDS[:exit] then close else Messages.unknown_command end end end private def to_h(game) { name: game.name, difficult: @difficult, attempts: game.attempts, attempts_used: game.used_attempts, hints: game.hints, hints_used: game.used_hints } end def register name = set_name @difficult = set_difficult game = Game.new(name, @difficult.level) start_game(game) end def start_game(game) Messages.game_commands game_process(game) start_new_game end def game_process(game) puts game.secret loop do break Messages.lose if game.attempts.zero? Messages.show_game_info game guess = user_input next Messages.get_hint(game) if guess == COMMANDS[:hints] next Messages.unknown_command unless guess_valid? guess result = get_result(game, guess) break win_game(game) if result end end def start_new_game Messages.start_again start = gets.chomp.downcase return run if start == YES close end def win_game(game) Messages.win Messages.save_stats save = gets.chomp.downcase Statistics.save(to_h(game)) if save == YES end def get_result(game, guess) result = game.check_number(guess) puts "[#{result}]" game.decrement_attempts game.win? result end def set_name Messages.show_name loop do name = user_input return name if name_valid? name Messages.name_error end end def set_difficult Messages.show_difficult loop do difficult = user_input return Difficult.new(difficult) if difficult_valid? difficult Messages.difficult_error end end def user_input input = gets.chomp.downcase input == COMMANDS[:exit] ? close : input end def close Messages.show_bye exit end end