...

/

Show the Letters That a Player Finds

Show the Letters That a Player Finds

Learn to improve the behavior of our program using methods or routines.

In the previous lesson, we introduced a routine named word_info. Although we didn’t add anything new to our game, we did improve its behavior and make it more efficient.

Improving the word_info routine

Now we need to show the correct letters in the proper position. We will improve the routine that we just wrote to show the letter. We also need the letter that the player proposed to be able to show it. We must join the letter with the hidden word when sending the word_info message.

Note: By using routines, notice how we can focus just on this need without thinking about the rest of the game. We can simply show the number of unknown letters and the letter that the player just found at the correct position.

Press + to interact
def word_info word, letter
n = word.length
underscores = "_ " * n
unless letter.nil?
i = word.index(letter)
underscores[2 * i] = letter.upcase
end
underscores
end

Display ghost letters

Lines 3–4 build the string that shows the ghost letters of the hidden word at their position, to which ...