I’m now into the third week of my apprenticeship. Last week was challenging – I completed the Coin Changer Kata and moved onto command-line Mastermind. This was a difficult adjustment and I had trouble making progress for most of the week.
I completed reading Apprenticeship Patterns whichwas an insightful, humbling and motivating book. It offered a roadmap for my apprenticeship as well as my career in software development beyond it. At the stage I am currently at, learning Ruby and building basic programs, I found the following patterns relevant:
- Concrete Skills
- Confront Your Ignorance
- Dig Deeper
- Expand Your Bandwidth
- Retreat Into Competence
- Your First Language
Of these, I found that I am firmly into Learning My First Language. After being assigned to create a Mastermind application, I realized my Ruby skills were lacking and studied from RubyKoans and RubyMonk.com [Retreat into Competence].
I was having some trouble with OOP combined with Ruby syntax as I started on my version of Mastermind. I came across Angeleah’s blog post on classes vs. instances of classes which was helpful and moved me along. Although I had a good grasp of the general structure and was generating a code and accepting the player’s guess, I was stuck at comparing the player’s guess with the code.
The general idea is to inform the player if their guess is the correct letter at that position, if it is in the wrong position or if it’s not in the code at all. I was using number codes, so I was manually evaluating the generated code and player’s guess by splitting the numbers into the thousands, hundreds, tens and single digits. Then I was using a function like:
def correct_first_guess
if @first_guess == @first_code
@number_correct << @first_guess
puts “You guessed the first digit correctly!”
elsif @first_guess == @second_code || @first_guess == @third_code || @first_guess == @fourth_code
@number_correct << -@first_guess
puts “You guessed the first digit correctly but in the incorrect position.”
end
@number_correct
end
for every digit in the code. This was tiresome and tedious. Luckily I paired with Myles on Friday afternoon to work on the code. The code to evaluate the player’s guess against the code was simple and efficient, giving a response made of +/-/_
def compare(user_guess, code)
i = 0
str = ”
while i < 4
if user_guess[i] == code[i]
str += ‘+’
elsif code.include?(user_guess[i])
str += ‘-‘
else
str += ‘_’
end
i += 1
end
str
end
This is my challenge. I need to change my thought process from doing things manually to automating them. One difference between Myles’ and my approach was that I focused on accepting the player’s guess and worked from there to evaluate it against the code. Myles used TDD to plug in sample codes and guesses, creating responses between them. This was where most of the work was to be done – at the end, we needed to create two very basic methods for accepting the player’s guess and presenting an appropriate response against the generated code. This is not the first time that I have run into this, but now that I have recognized it I hope that I can shift gears and bypass this sort of problem.