def generate_word(len)
  [*..].shuffle.first(len)        
end

def get_guess(len)
  loop do
    print 
    guess = gets.strip
    err = case
          when guess.match(/[^1-9]/)            ; 
          when guess.length!= len              ; 
          when guess.split().uniq.length!= len; 
          else return guess.split()
          end
    puts 
  end
end

def score(word, guess)
  bulls = cows = 0
  guess.each_with_index do |num, idx|
    if word[idx] == num
      bulls += 1
    elsif word.include? num
      cows += 1
    end
  end 
  [bulls, cows]
end

word_length = 4
puts 
word = generate_word(word_length)
count = 0
loop do
  guess = get_guess(word_length)
  count += 1
  break if word == guess
  puts  % score(word, guess)
end
puts