module Codebreaker include Validation class Game PLUS = '+'.freeze MINUS = '-'.freeze attr_reader :name, :difficult, :secret, :attempts, :hints, :hints_used, :attempts_used def initialize(name, difficult) @name = name @difficult = difficult @attempts = get_attempts(difficult) @hints = get_hints(difficult) @attempts_used = 0 @hints_used = 0 generate_secret @secret_chars = @secret.chars.shuffle end def decrement_attempts return false if @attempts.zero? @attempts_used += 1 @attempts -= 1 end def use_hint return false if @hints.zero? @hints_used += 1 @hints -= 1 @secret_chars.pop end def win?(matches) matches == '++++' end def check_number(guess, secret_code = @secret) secret = secret_code.chars input = guess.chars grouped = secret.zip(input).group_by { |values| values.first == values.last } grouped.default = [] plus = grouped[true].size return PLUS * plus if plus == 4 secret, input = grouped[false].transpose minus = (secret & input).inject(0) { |count, number| count + [secret.count(number), input.count(number)].min } PLUS * plus << MINUS * minus end private def generate_secret @secret = (1..4).map { rand(1..6) }.join end def get_attempts(difficult) case difficult when 'easy' then 15 when 'medium' then 10 when 'hell' then 5 end end def get_hints(difficult) case difficult when 'easy' then 2 else 1 end end end end