Search⌘ K

Declare That the Player Has Lost

Learn to code the game rules and decide that the player has lost.

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.

Ruby
require_relative "word_info"
hidden_word = "fuchsia"
found_letters = []
wrong_letters = []
errors_counter = 0
loop do
if errors_counter > 6
puts "You lost the game, BANANE!!! The word was #{hidden_word}"
break
end
info = word_info(hidden_word, found_letters)
puts info
puts
unless info.include?('_')
puts "You win, PATATE!"
break
end
print "Wrong letters: "
puts wrong_letters.sort.uniq.join(" ")
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"
wrong_letters << answer.upcase
errors_counter = errors_counter + 1
end
end

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 ...