Declare That the Player Has Lost
Learn to code the game rules and decide that the player has lost.
We'll cover the following...
Define the rules of the game
Let’s now include a condition for the player to lose the game if they haven’t guessed the word in seven tries. Let's have a look at the updated implementation for this requirement.
Press + to interact
require_relative "word_info"hidden_word = "fuchsia"found_letters = []wrong_letters = []errors_counter = 0loop doif errors_counter > 6puts "You lost the game, BANANE!!! The word was #{hidden_word}"breakendinfo = word_info(hidden_word, found_letters)puts infoputsunless info.include?('_')puts "You win, PATATE!"breakendprint "Wrong letters: "puts wrong_letters.sort.uniq.join(" ")putsprint "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"wrong_letters << answer.upcaseerrors_counter = errors_counter + 1endend
Count errors
We must count the number of errors. Therefore, we assign the name errors_counter
to zero (the value) at line 8.
When we conclude that the proposal is wrong, we recall the value associated with the errors_counter
...