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

begin

# 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
begin
# display the word in progress
print "\nWord: "
for x in (0..target_word.size-1)
print progress_word[x] + ' '
end
# 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]
end
end
# ask user to guess one letter
begin
print "\n\nGuess a letter >> "
guess = gets.downcase.chomp
if guess.size > 1
puts 'Guess one letter, please.'
end
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
end
end
# 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
else
# repeat bad guess does not count against player
puts 'You already guessed that letter. Try again!'
end
end

# 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!"
else
puts "\n\nSorry, you ran out of attempts."
end
# 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!"