# frozen_string_literal: true module Codebreaker class Console include Storage attr_reader :game, :player, :guess def welcome_message output(:welcome_message) run end private def run loop do @game = Game.new output(:game_navigation) case user_input when I18n.t(:start) then start_game_preparations when I18n.t(:rule) then output(:rules) when I18n.t(:stats) then statistic(sort_player) else output(:wrong_command) end end end def start_game_preparations return unless registration && difficulty_setup game_cycle end def registration @player = Player.new loop do output(:output_name) player.name = user_input return true if player.valid? puts player.errors.first end end def game_cycle output(:mode_info, game.difficulty) game_cycle_logic until game.lost? lost end def game_cycle_logic round_menu @guess = user_input hint_menu if guess == I18n.t(:hint) mark if game.guess_valid?(guess) won if game.won?(guess) end def difficulty_setup loop do output(:output_difficulty) return true if game.set(user_input) end end def round_menu output(:remained_attempts, attempts: game.attempts) output(:hints_left, hints: game.hint_keeper.size) output(:input_guess) end def hint_menu output(:no_hints_left) if game.hint_keeper.empty? output(:take_hint, take_hint: game.take_hint!) end def mark game.use_attempt! && (puts play(guess)) end def won output(:game_won, secret_code: game.secret_code.join('')) save_result play_again end def lost output(:game_lost, secret_code: game.secret_code.join('')) end def save_result output(:save) game.save(game.to_yaml(player.name)) if user_input == 'yes' end def play_again output(:play_again) user_input == 'yes' ? run : exit end def play(guess) guess = guess.chars.map(&:to_i) game.guess(guess) end def statistic(array) puts I18n.t(:statistic) rating = 1 array.each do |e| header_part2 = "|#{e[:try]}\t\t|#{e[:hints_total]}\t\t|#{e[:hints_used]}\t\t|" header_part1 = "|#{rating}\t|#{e[:name]}\t|#{e[:difficulty]}\t\t|#{e[:attempts]}\t\t" puts header_part1 + header_part2 rating += 1 end end def user_input input = gets.chomp input == I18n.t(:exit) ? exit : input end def output(*message) puts I18n.t(*message) end end end