Skip to content

Instantly share code, notes, and snippets.

@nevernormal1
Created March 3, 2009 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevernormal1/73169 to your computer and use it in GitHub Desktop.
Save nevernormal1/73169 to your computer and use it in GitHub Desktop.
# Use a covering technique to cover first blacks, then whites
# At each step, anything covered is acccounted for.
# Anything uncovered at the end is just plain wrong
def guess(guess)
covered_result = @secret_code.dup
guess.each_with_index do |color, i|
covered_result[i] = 'b' if @secret_code[i] == color
end
guess.each_with_index do |color, i|
match = covered_result.index(color)
covered_result[match] = 'w' if match
end
print_result(covered_result)
end
private
def print_result(result)
@messenger.puts result.select {|c| c =~ /[bw]/}.sort.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment