Skip to content

Instantly share code, notes, and snippets.

@americos
Created December 16, 2010 05:46
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 americos/743084 to your computer and use it in GitHub Desktop.
Save americos/743084 to your computer and use it in GitHub Desktop.
Main class for Codebreaker
module Codebreaker
class Game
def initialize(output)
@output = output
end
def start(secret)
@secret = secret
@output.puts 'Welcome to Codebreaker!'
@output.puts 'Enter guess:'
end
def guess(guess)
mark = ''
(0..3).each do |index|
if exact_match?(guess, index)
mark << '+'
end
end
(0..3).each do |index|
if number_match?(guess, index)
mark << '-'
end
end
@output.puts mark
end #guess
def exact_match?(guess, index)
guess[index] == @secret[index]
end #exact_match?
def number_match?(guess, index)
@secret.include?(guess[index]) && !exact_match?(guess, index)
end #number_match
end #end Game
end #end Codebreaker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment