...

/

Show All the Letters That the Player Has Already Found

Show All the Letters That the Player Has Already Found

Learn to make the game more intuitive.

Accumulating found letters using collections

We only show one letter at a time, that is, the last one that the player has found. Understanding this behavior is rather simple. We simply assign a name to the proposed letter when it matches, and we use the same name each time, while simultaneously hiding the previous letter.

We need to accumulate all the found letters. We have what we call collections, which we can think of as bags in which we can add things.

Press + to interact
require_relative "word_info"
hidden_word = "fuchsia"
found_letters = []
loop do
info = word_info(hidden_word, found_letters)
puts info
puts
print "Give me a letter: "
answer = gets
answer = answer.chomp.downcase
if answer == 'stop'
exit
end
if answer.length > 1
puts "You must give only one letter!"
elsif answer.empty?
elsif hidden_word.include?(answer)
puts "The letter is part of the hidden word"
if found_letters.include?(answer)
else
found_letters << answer
end
else
puts "Invalid letter"
end
end

Let’s have a look at the updated code:

  • In line 5, we create an empty collection []
...