r/learnruby • • Sep 22 '14
Mastermind Game Question
I have two strings guess = 'rgby' code = 'gbyg'
How do I count the correct number of colors/characters guessed in the code? In this case the answer would be 3. The guess has 3 correct guesses contained in the code, irrespective of position.
1
Upvotes
View all comments
Show parent comment
1
u/[deleted] Sep 22 '14
Yes that works, thank you very much. now for the correct position, I have two convoluted ways shown below.
Is there an easier way to do this with enumerable? Thanks for your help.
if (@secret_code[0] <=> @code_guess[0]) == 0 @correct_position +=1 end
if (@secret_code[1] <=> @code_guess[1]) == 0 @correct_position +=1 end
if (@secret_code[2] <=> @code_guess[2]) == 0 @correct_position +=1 end
if (@secret_code[3] <=> @code_guess[3]) == 0 @correct_position +=1 end