lib/codeguessing/game.rb in codeguessing-0.3.3 vs lib/codeguessing/game.rb in codeguessing-0.4
- old
+ new
@@ -1,9 +1,8 @@
module Codeguessing
class Game
- attr_reader :attempts, :hint_count, :state, :answer
- attr_accessor :secret_code
+ attr_accessor :attempts, :hint_count, :state, :answer, :secret_code
MAX_HINT = 2
MAX_ATTEMPTS = 5
def initialize(opt = {})
@@ -14,17 +13,20 @@
@answer = opt[:answer] || ''
end
def guess(code)
loose unless check?(use_attempt)
+ hash = {}
res = ''
code.each_char.with_index do |char, i|
- if char == secret_code[i]
- res += '+'
- else
- res += '-'
- end
+ case
+ when char == secret_code[i]
+ res += '+'
+ when secret_code.count(char) == 1
+ hash[char] = '-'
+ end
end
+ res += hash.values.join('')
win if res == '++++'
@answer = res
end
def hint