lib/ep-codebreaker/game.rb in ep-codebreaker-0.3.1 vs lib/ep-codebreaker/game.rb in ep-codebreaker-0.3.2

- old
+ new

@@ -5,59 +5,47 @@ module Codebreaker class Game include GameWriter - MIN = 1 - MAX = 6 + MIN = 1 + MAX = 6 LENGTH = 4 - REGEXP = Regexp.new("^[#{MIN}-#{MAX}]{#{LENGTH}}$") - TRIES = 10 HINTS = 3 - private_constant :MIN, :MAX, :LENGTH, :REGEXP, :TRIES, :HINTS + private_constant :MIN, :MAX, :LENGTH, :TRIES, :HINTS attr_reader :tries_left, :hints_left def start - @code = Array.new(LENGTH) { rand(MIN..MAX) }.join + @code = Array.new(LENGTH) { rand(MIN..MAX) }.join + @result = '' + @tries_left = TRIES + @hints_left = HINTS @indexes_for_hint = (0...LENGTH).to_a - - @tries_left = TRIES - @hints_left = HINTS - - @finished = false - @won = false end def check_guess(input) verify input - @tries_left -= 1 - - result = check_input(@code.chars, input.chars) - define_stage result - result + @result = check_input(@code.chars, input.chars) end def hint return false if @hints_left.zero? - @tries_left -= 1 @hints_left -= 1 - define_stage - generate_hint end def finished? - @finished + @tries_left.zero? || won? end def won? - @won + @result == ('+' * LENGTH) end def answer @code if finished? end @@ -106,20 +94,12 @@ index = @indexes_for_hint.delete(@indexes_for_hint.sample) hint[index] = @code[index] hint end - def define_stage(result = '') - if result == ('+' * LENGTH) - @finished = true - @won = true - elsif @tries_left.zero? - @finished = true - end - end - def verify(input) + regexp = Regexp.new("^[#{MIN}-#{MAX}]{#{LENGTH}}$") msg = "Guesses must consist of #{LENGTH} digits from #{MIN} to #{MAX}" - raise(ArgumentError, msg) if !input.is_a?(String) || !input.match?(REGEXP) + raise(ArgumentError, msg) if !input.is_a?(String) || !input.match?(regexp) end end end