Question: Ruby - Writing a program that guesses letters in a word


Ruby - Writing a program that guesses letters in a word

Answers 1
Added at 2017-01-03 08:01

Background: Using a class, I'm building a hangman-like game where player 1 enters a word to be guessed, and player 2 attempts to guess it. The number of guesses allotted to player 2 is relative to the word in question, but repeated guesses do not count against the player.

Each guess should provide continual feedback to player 2 by showing their progress towards guessing the word, which should be printed at the end of each guessing phase. ex) The word 'code' would be displayed as "_ _ _ _ ", if the letter 'o' were to be guessed, the feedback would look like " _ o _ _", etc. Once the word is guessed or a player has 0 attempts left, a winner is announced.

Issue 1: I can't get the program to close when the game_won? method evaluates to true. It continues to run until attempts == game_word.length + 2. Any ideas on how to end the program with the winning statement?

Issue 2: I tried adding a game_lost method, but couldn't get it to work once player 2 runs out of attempts (tried creating an instance variable to be tied to attempts in the interface, but whenever it was called outside of the class, an error popped up stating that it was a nil class, rather than an integer. How can I make a functional method that states when the game is lost?

Issue 3: Whenever an incorrect letter is entered, the "Nope, try again..." response is printed out as many times as the length of the word is. It seems to be printing out whatever is evaluated last, x amount of times, in the guess_the_letter method.

Issue 4: If the word entered contains more than one of the same letter, the word progress update will appear as many times as that letter exists within the game word. (Seems to be a similar issue to issue 3) Any ideas as to what I'm doing wrong here.

    class GuessingGame

    def initialize(word)
        @word = word.downcase
        @display_word = "_" * word.length

    # Take a guess in the form of a letter and check to see if it is in the
    # target word, update the word pattern to include the missing  letter
    def guess_the_letter(g_letter)
        @word.split("").each_with_index do |w_letter, index|
            if g_letter == w_letter
                @display_word[index] = g_letter
                puts "Here is your progress towards guessing the word:"
        p @display_word
          if !@word.include? (g_letter)
            puts "Nope, try again..."

    # Determine winning conditions
    def game_won?
      if    @word == @display_word
            puts "Congratulations Player 2, you won!"

    def game_lost?
      #method body

puts "Welcome to the Word Guessing Game!"
puts "This game is for 2 players."
puts "Player 1, please enter a word for player 2 to guess..."
game_word = gets.chomp

game =

attempts = 0
guessed_letters = []

#Create an interface for the users to play the game

until attempts == game_word.length + 2
  puts "Player 2, please guess a letter..."
    letter_guess = gets.chomp
    if guessed_letters.include? letter_guess
        puts "You already tried that letter, enter a new one."
        letter_guess = gets.chomp
    guessed_letters << letter_guess
  attempts += 1 
Answers to

Ruby - Writing a program that guesses letters in a word

nr: #1 dodano: 2017-01-03 08:01

When asking this kind of question, you should indicate, where line 46 is.

In your case, I guess it's the expression attempts == @word.length. You are not in class context, so @word does not refer to the instance variable in GuessingGame.

You can not directly access an instance variable of another object (i.e. an object different to self), so you need to provide an accessor method. Ruby makes this easy using attr_reader:

attr_reader :word

creates a read-accessor method to @word which is called word.

UPDATE: I just see that you have modified your original code. With the new code, you will have the same problem with @bad_guesses.

BTW, if you edit your posting, please always explain what you changed.

Source Show
◀ Wstecz