lib/alex_codebreaker/game.rb in alex_codebreaker-0.1.6 vs lib/alex_codebreaker/game.rb in alex_codebreaker-0.1.7

- old
+ new

@@ -1,87 +1,72 @@ -require_relative 'modules/validators/arguments_validation' +module AlexCodebreaker + class Game + include AlexCodebreaker::Modules::ArgumentsValidation -class Game - include ArgumentsValidation + attr_reader :secret_code, :session, :win_status - attr_reader :session, :win_status + def initialize + @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code + @secret_code_for_hint = @secret_code.clone.uniq + @session = Session.new + end - def initialize - @secret_code = Array.new(Settings::CODE_LENGTH) { rand(Settings::CODE_MIN_DIGIT..Settings::CODE_MAX_DIGIT) } - @session = Session.new - @win_status = false - end + def hint + return unless @session.check_hints - def add_player_name(given_name) - name_validation(given_name) ? @session.player_name = given_name : false - end + hint_processing + end - def choose_difficulty(chosen_difficulty) - @session.difficulty_manager(chosen_difficulty.to_sym) - end + def guess(user_input) + return unless guess_validation(user_input) && @session.check_attempts - def hint - return false if @session.hints_used >= @session.hints_total + comparator(user_input) + end - @session.hints_used += 1 - @secret_code_for_hint ||= @secret_code.clone.uniq - @secret_code_for_hint.delete(@secret_code_for_hint.sample) - end + private - def guess(user_input) - return false if !guess_validation(user_input) || check_attempts + def hint_processing + return @secret_code_for_hint.first if @secret_code_for_hint.one? - @session.attempts_used += 1 - @win_status = true if user_input.to_i == @secret_code.join.to_i - comparator(user_input) - end + @secret_code_for_hint.delete(@secret_code_for_hint.sample) + end - def check_attempts - @session.attempts_used >= @session.attempts_total - end + 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, secret_code_for_comparison).compact + user_input = matching_only_numbers(user_input, secret_code_for_comparison).compact + matching_absent(user_input) + end - def save_game - @session.save_session_statistic if @win_status - end - - def show_secret_code - @secret_code.join - 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(Settings::MATCHING[:absent])).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 - Settings::MATCHING[:place] - elsif !@secret_code_for_comparison.include?(value) - Settings::MATCHING[:absent] - else - value + def matching_numbers_and_indexes(user_input, secret_code_for_comparison) + user_input.map.with_index do |value, index| + if value == secret_code_for_comparison[index] + secret_code_for_comparison[index] = nil + AlexCodebreaker::Modules::Settings::MATCHING[:place] + else + value + end 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_at(@secret_code_for_comparison.index(value)) - Settings::MATCHING[:presence] - elsif [Settings::MATCHING[:place], Settings::MATCHING[:absent]].include?(value) - value - else - Settings::MATCHING[:absent] + def matching_only_numbers(user_input, secret_code_for_comparison) + user_input.map do |value| + if secret_code_for_comparison.include?(value) + secret_code_for_comparison.delete_at(secret_code_for_comparison.index(value)) + AlexCodebreaker::Modules::Settings::MATCHING[:presence] + elsif value == AlexCodebreaker::Modules::Settings::MATCHING[:place] + value + end end + end + + def matching_absent(user_input) + AlexCodebreaker::Modules::Settings::MATCHING[:place] * + user_input.count(AlexCodebreaker::Modules::Settings::MATCHING[:place]) + + AlexCodebreaker::Modules::Settings::MATCHING[:presence] * + user_input.count(AlexCodebreaker::Modules::Settings::MATCHING[:presence]) + + AlexCodebreaker::Modules::Settings::MATCHING[:absent] * + (AlexCodebreaker::Modules::Settings::CODE_LENGTH - user_input.length) end end end