title: Ruby - Writing a program that guesses letters in a word
body: <p>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.</p>
  
  <p>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 "_ _ _ _ <em>", if the letter 'o' were to be guessed, the feedback would look like "</em> _ o _ _", etc. Once the word is guessed or a player has 0 attempts left, a winner is announced.</p>
  
  <p>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?</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>    class GuessingGame
  
      def initialize(word)
          @word = word.downcase
          @display_word = "_" * word.length
      end
  
      # 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)
          g_letter.downcase
          @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
              end
            if !@word.include? (g_letter)
              puts "Nope, try again..."
            end
          end
      end
  
      # Determine winning conditions
      def game_won?
        if    @word == @display_word
              puts "Congratulations Player 2, you won!"
              true
          else
              false
        end
      end
  
      def game_lost?
        #method body
      end
  end
  
  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 = GuessingGame.new(game_word)
  
  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
      end
      guessed_letters &lt;&lt; letter_guess
    game.guess_the_letter(letter_guess)
    game.game_won?
    attempts += 1 
  end
  </code></pre>