lib/codeguessing/game.rb in codeguessing-0.2.0 vs lib/codeguessing/game.rb in codeguessing-0.3.0
- old
+ new
@@ -1,20 +1,18 @@
module Codeguessing
class Game
- attr_reader :result, :attempts, :hint_count, :state, :scores
+ attr_reader :result, :attempts, :hint_count, :state
attr_accessor :secret_code
MAX_HINT = 2
MAX_ATTEMPTS = 5
- def initialize(data = [])
- @secret_code = ''
- 4.times { @secret_code += rand(1..6).to_s }
- @attempts = MAX_ATTEMPTS
- @hint_count = MAX_HINT
+ def initialize(opt = {})
+ @secret_code = opt[:secret_code] || random
+ @attempts = opt[:attempts] || MAX_ATTEMPTS
+ @hint_count = opt[:hint_count] || MAX_HINT
@state = ''
- @scores = data || []
end
def guess(code)
loose unless check?(use_attempt)
res = ''
@@ -42,24 +40,13 @@
return '' unless check?(hint_count)
use_hint
res
end
- def save(path, name = 'Anonim')
- return false if state != true
- score = cur_score
- score[:name] = name
- @scores << score
- File.new(path, 'w') unless File.exist?(path)
- File.open(path, "r+") do |f|
- f.write(@scores.to_yaml)
- end
- @scores
- end
-
- def cur_score
+ def cur_score(name = 'Anonim')
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.delete(:scores)
@@ -91,8 +78,14 @@
end
def loose
@state = false
end
+
+ def random
+ code = ''
+ 4.times { code += rand(1..6).to_s }
+ code
+ end
end
end