Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2017
    Thanked 0 Times in 0 Posts

    Help with Hangman like code

    Hey all,

    I am learning Ruby and I am struggling to fix the issue with the program i am playing with. I cant get it to end after the correct number of misses and i also cant seem to get it to properly enter in the letter when i correctly guess. I spend hours tooling with it, any help or suggestions?

    # create a dictionary of words chosen at random for the user to guess

    word_bank = %w[venezuela peru colombia brazil argentina chile uruguay]

    puts "\nWelcome to Hangman Lite!"

    play_again = TRUE

    # this is the game loop, which continues as long as the user wants to play


    # create an array to hold bad guesses, i.e. letters not in target word
    misses = Array.new

    word_bank.shuffle! # randomly shuffle array of words and choose first one
    target_word = word_bank[0]

    # split word into an array of letters
    target_letters = Array.new(target_word.split(''))
    # create an array of hyphens the length of the target word
    progress_word = Array.new(target_letters.size, '-')
    # reset counter to ten bad guesses permitted
    attempt_count = 10

    # set counter for number of remaining guesses to length of target word
    remain_count = target_word.size
    # this loop continues as long as word hasn't been fully guessed and user # hasn't run out of attempts
    # display the word in progress
    print "\nWord: "
    for x in (0..target_word.size-1)
    print progress_word[x] + ' '
    # if user has misses, display them in a sequence
    if misses.size > 0
    print "\n\nMisses: "
    for x in 0.. misses.size
    print misses[x]
    # ask user to guess one letter
    print "\n\nGuess a letter >> "
    guess = gets.downcase.chomp
    if guess.size > 1
    puts 'Guess one letter, please.'
    end while guess.size > 1

    # if letter is in target word, replace underscore with actual letter
    # and decrease counter for remaining letters to guess

    if target_word.include?guess
    for x in (0..target_word.size - 1)
    # if letter was already guessed, do not count it again
    if target_letters[x] == guess and progress_word[x] == "_"
    progress_word[x] = guess
    remain_count - 1
    # if letter is not in target word and user hasn't already tried it, # decrease the number of attempts by 1. else
    if !misses.include?guess
    misses.push(guess) # add bad guess to misses array
    puts 'Try again!'
    attempt_count -= 1 # penalize user
    # repeat bad guess does not count against player
    puts 'You already guessed that letter. Try again!'

    # keep going until all letters guessed or all attempts exhausted
    end until remain_count == 0 or attempt_count == 0

    # tell user why the game ended
    if remain_count == 0
    puts "\n\nCongratulations, you guessed the word!"
    puts "\n\nSorry, you ran out of attempts."
    # show the word
    puts "\nThe word is... #{target_word} "
    # take the word from the last game out of play
    word_bank.delete_if { |word| word == target_word }

    # ask user to play again

    print "\nDo you want to play again (y/n)? "
    play_again = (gets.downcase.chomp[0] == 'y')

    end while play_again # keep going if user wants to

    puts "\nThanks for playing. Goodbye!"

  2. #2
    New Coder
    Join Date
    May 2015
    Thanked 0 Times in 0 Posts
    What errors do you get?


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts