class Game include ArgumentsValidation attr_reader :session def start @secret_code = Array.new(4) { rand(1..6) } @session = Session.new @win_status = false end def add_player_name(given_name) @session.player_name = name_validation(given_name) end def choose_difficulty(chosen_difficulty) @session.difficulty_manager(chosen_difficulty.to_sym) end def hint raise NoHintsLeft if @session.hints_used >= @session.hints_total @session.hints_used += 1 @secret_code_for_hint ||= @secret_code.clone.uniq @secret_code_for_hint.delete(@secret_code_for_hint.sample) end def guess(user_input) guess_validation(user_input) raise NoAttemptsLeft if @session.attempts_used >= @session.attempts_total @session.attempts_used += 1 @win_status = true if comparator(user_input.to_s) == GameMessages::WIN_CONDITION comparator(user_input.to_s) end def save_game @session.save_session_statistic if @win_status end def show_secret_code @secret_code.join if @win_status end private def comparator(user_input) user_input = user_input.chars.map!(&:to_i) @secret_code_for_comparison = @secret_code.clone user_input = matching_numbers_and_indexes(user_input) user_input = matching_only_numbers(user_input).sort user_input.rotate(user_input.count(' ')).join end def matching_numbers_and_indexes(user_input) user_input.map.with_index do |value, index| if value == @secret_code_for_comparison[index] @secret_code_for_comparison[index] = nil GameMessages::MATCHING[:number_and_index] elsif !@secret_code_for_comparison.include?(value) GameMessages::MATCHING[:no_matching] else value end end end def matching_only_numbers(user_input) user_input.map do |value| if @secret_code_for_comparison.include?(value) @secret_code_for_comparison.delete(value) GameMessages::MATCHING[:only_number] elsif [GameMessages::MATCHING[:number_and_index], GameMessages::MATCHING[:no_matching]].include?(value) value else GameMessages::MATCHING[:no_matching] end end end end