Created
December 16, 2010 05:46
-
-
Save americos/743084 to your computer and use it in GitHub Desktop.
Main class for Codebreaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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