Show All the Letters That the Player Has Already Found
Learn to make the game more intuitive.
We'll cover the following...
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 doinfo = word_info(hidden_word, found_letters)puts infoputsprint "Give me a letter: "answer = getsanswer = answer.chomp.downcaseif answer == 'stop'exitendif answer.length > 1puts "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)elsefound_letters << answerendelseputs "Invalid letter"endend
Let’s have a look at the updated code:
- In line 5, we create an empty collection
[]