Skip to content

Instantly share code, notes, and snippets.

@mariozig
Last active December 10, 2015 23:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mariozig/4512157 to your computer and use it in GitHub Desktop.
Source for the Mastermind game. Blog post @ http://ruby.zigzo.com/2013/01/10/building-mastermind/
class Game
COLORS = ["R", "G", "B", "Y", "O", "P"]
def initialize
@comp = Computer.new
@player = Player.new
end
def play
10.times do |i|
puts "This is chance #{i+1} of 10"
current_guess = @player.guess_code
standing = evaluate_guess(current_guess)
if standing[:exact].length == 4
puts "You won!"
return
else
puts "#{standing[:exact].length} Exact Matches"
puts "#{standing[:near].length} Near Matches"
end
end
# If we make it this far, we have used up
# all of our turns and lost.
puts "You lost!"
return
end
def evaluate_guess(current_guess)
results = { :exact => [], :near => [] }
current_guess.each_with_index do |choice, position|
if exact_match?(choice, position)
results[:exact] << true
elsif near_match?(choice)
results[:near] << true
end
end
results
end
def near_match?(choice)
@comp.code_combination.include?(choice)
end
def exact_match?(choice, position)
choice == @comp.code_combination[position]
end
end
class Computer
attr_reader :code_combination
def initialize
@code_combination = code
end
def code
colors = Game::COLORS.shuffle
generated_code = []
4.times{ generated_code << colors.pop }
generated_code
end
end
class Player
def guess_code
puts 'What is your guess? Choose one of the following: "R", "G", "B", "Y", "O", "P"'
converted_guess(gets.chomp)
end
def converted_guess(guess)
guess.split('')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment