lib/codeguessing/game.rb in codeguessing-0.3.2 vs lib/codeguessing/game.rb in codeguessing-0.3.3
- old
+ new
@@ -1,18 +1,19 @@
module Codeguessing
class Game
- attr_reader :attempts, :hint_count, :state
+ attr_reader :attempts, :hint_count, :state, :answer
attr_accessor :secret_code
MAX_HINT = 2
MAX_ATTEMPTS = 5
def initialize(opt = {})
@secret_code = opt[:secret_code] || random
@attempts = opt[:attempts] || MAX_ATTEMPTS
@hint_count = opt[:hint_count] || MAX_HINT
@state = opt[:state] || ''
+ @answer = opt[:answer] || ''
end
def guess(code)
loose unless check?(use_attempt)
res = ''
@@ -22,11 +23,11 @@
else
res += '-'
end
end
win if res == '++++'
- res
+ @answer = res
end
def hint
res = ''
need_index = rand(0...4)
@@ -40,16 +41,23 @@
return '' unless check?(hint_count)
use_hint
res
end
- def cur_score(name = 'Anonim')
+ def cur_game
hash = {}
- hash[:name] = name
self.instance_variables.each do |k, v|
new_k = k.to_s.gsub('@','').to_sym
hash[new_k] = self.instance_variable_get(k)
end
+ hash
+ end
+
+ def cur_score(name = 'Anonim')
+ hash = cur_game
+ hash[:name] = name
+ hash.delete(:answer)
+ hash.delete(:state)
hash
end
def valid?(code)
return true if code =~ /^[1-6]{4}$/s