# frozen_string_literal: true module Codebreaker class Game include Validation include FileStore attr_reader :hints, :user, :state, :secret_code def initialize(user) @user = user @state = Constants::START_GAME @secret_code = SecretCodeGenerator.call end def start validate_state(@state, Constants::START_GAME) @copied_code = @secret_code.dup @state = Constants::IN_GAME end def generate_matrix(input) validate_state(@state, Constants::IN_GAME) validate_guess(input) user.decrement_attemts MatrixGenerator.new(input, @secret_code).call end def self.statistics load_file.each.sort_by { |game| [game[:attempts], game[:used_hints], game[:used_attempts]] } end def hints? (user.hints <= Constants::DIFFICULTIES[@user.difficulty][:hints]) && user.hints.positive? end def attempts? (user.attempts <= Constants::DIFFICULTIES[@user.difficulty][:attempts]) && user.attempts.positive? end def win?(result) return false unless result == @secret_code @state = Constants::WIN true end def lose? return unless user.attempts.zero? @state = Constants::LOSE true end def self.show_difficulties Constants::DIFFICULTIES end def use_hint return if @copied_code.empty? hint = @copied_code.sample @copied_code.join.sub!(hint.to_s, "") user.decrement_hints hint end end end