lib/codeguessing/game.rb in codeguessing-0.4.1 vs lib/codeguessing/game.rb in codeguessing-0.4.2

- old
+ new

@@ -14,18 +14,22 @@ end def guess(code) loose unless check?(use_attempt) hash = {} - res = '' - code.each_char.with_index do |char, i| + res = '' + remaine_chars = code + right_chars = [] + secret_code.each_char.with_index do |char, i| case - when char == secret_code[i] - res += '+' - when secret_code.count(char) == 1 && - code.count(char) == 1 - hash[char] = '-' - end + when code[i] == char + res += '+' + right_chars << char + remaine_chars[i] = '*' + hash.delete(char) if right_chars.include?(char) + when remaine_chars.include?(char) + hash[char] = '-' + end end res += hash.values.join('') win if res == '++++' @answer = res end