lib/codeguessing/game.rb in codeguessing-0.4.7 vs lib/codeguessing/game.rb in codeguessing-0.4.8
- old
+ new
@@ -4,98 +4,101 @@
MAX_HINT = 2
MAX_ATTEMPTS = 5
MAX_SIZE = 4
- 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] || ''
+ def initialize
+ @secret_code = random
+ @attempts = MAX_ATTEMPTS
+ @hint_count = MAX_HINT
+ @state = ''
+ @answer = ''
end
def guess(code)
- @state = 'loose' unless check?(use_attempt)
- @state = 'win' if code == secret_code
+ @state = 'loose' unless natural?(use_attempt)
+ if code == secret_code
+ @state = 'win'
+ return @answer = '+' * MAX_SIZE
+ end
@answer = get_mark(code)
- end
+ end
def get_mark(code)
- return false unless valid?(code)
- hash = {}
- res = ''
- secret_code.each_char.with_index do |char, i|
- if code[i] == char
- res += '+'
- code[i] = '_'
- hash.delete(char)
- elsif code.include?(char)
- hash[char] = '-'
- end
- end
- res += hash.values.join('')
- end
+ raise 'Invalid data' unless valid?(code)
+ mark = ''
+ secret_codes = secret_code.chars
+ codes = code.chars
+
+ secret_codes.each_with_index do |char, index|
+ next unless char == codes[index]
+ secret_codes[index] = nil
+ codes[index] = nil
+ mark += '+'
+ end
+
+ secret_codes.compact.each_with_index do |char, index|
+ next unless code_index = codes.index(char)
+ codes[code_index] = nil
+ mark += '-'
+ end
+
+ mark
+ end
def hint
- return '' unless check?(hint_count)
+ return '' unless natural?(hint_count)
use_hint
hint = '*' * MAX_SIZE
index = rand(0...MAX_SIZE)
- code_char = secret_code[index]
- hint[index] = code_char
+ hint[index] = secret_code[index]
hint
end
+ def win?
+ case @state
+ when 'win' then true
+ when 'loose' then false
+ end
+ end
+
def cur_score(name = 'Anonim')
- hash = cur_game
- hash[:name] = name
- hash[:attempts] = MAX_SIZE - attempts
- hash[:hint_count] = MAX_HINT - hint_count
- hash[:date] = Time.now.to_i
- hash
+ scores = cur_game
+ scores[:name] = name
+ scores[:attempts] = MAX_SIZE - attempts
+ scores[:hint_count] = MAX_HINT - hint_count
+ scores[:date] = Time.now.to_i
+ scores
end
+ def cur_game
+ attributes = instance_variables.map do |var|
+ [var[1..-1].to_sym, instance_variable_get(var)]
+ end
+ attributes.to_h
+ end
+
def valid?(code)
return true if code =~ /^[1-6]{#{MAX_SIZE}}$/s
false
- end
+ end
- def use_attempt
- @attempts -= 1
- end
+ private
- def use_hint
- @hint_count -= 1
+ def natural?(number)
+ number >= 0
end
- def win?
- case @state
- when 'win' then true
- when 'loose' then false
- end
+ def random
+ Array.new(MAX_SIZE) { rand(1..6) }.join
end
- def cur_game
- hash = {}
- 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
+ def use_attempt
+ @attempts -= 1
end
- private
-
- def check?(varible)
- return false if varible == 0
- true
- end
-
- def random
- code = ''
- MAX_SIZE.times { code += rand(1..6).to_s }
- code
+ def use_hint
+ @hint_count -= 1
end
end
end