added hangman graphics. game is finished

This commit is contained in:
user 2022-01-24 03:18:27 -05:00
parent 53b032310e
commit 3b71f69f19

View File

@ -2,15 +2,15 @@
#01/21/2022
def pick_word(a)
rand_int = rand(0..a.size)
return a[rand_int].chop!
rand_int = rand(0..a.size) #pick a num within the range of the file length
return a[rand_int].chop! #and format the word
end
def generate_hidden_letters(word)
hidden_letters = Array.new
word.length.times do |i|
if rand(0..1) == 0 #do this 50% of the time
hidden_letters.push(word[i]) #create an array of letters to be hidden
if rand(0..1) == 0 #do this 50% of the time
hidden_letters.push(word[i]) #create an array of letters to be hidden
end
end
return hidden_letters
@ -28,9 +28,9 @@ def scramble_word(word, hidden_letters, scrambled_word)
end
def unscramble_word(word, guesses, scrambled_word)
guesses.each do |letter| #for each hidden letter
guesses.each do |letter| #for each guess
word.length.times do |i|
if word[i] == letter #if you find one, replace it with a "_" in the string
if word[i] == letter #if you find one, replace it with a the letter in the string
scrambled_word[i] = letter
end
end
@ -38,18 +38,26 @@ def unscramble_word(word, guesses, scrambled_word)
return scrambled_word
end
def print_hangman(wrong_guesses)
man = [" o\n", " \\", "@", "/\n", " /", " \\"] #ascii art of a hangman rope
puts "_______"
puts " | "
wrong_guesses.times do |i|
print "#{man[i]}"
end
end
a = Array.new
dict = File.open('5desk.txt')
dict.each do |row|
dict.each do |row| #loading the dict file into an array
a.push(row)
end
#picking a word between 5 and 12 characters long
word = pick_word(a)
until word.length.between?(5, 12)
until word.length.between?(5, 12) #picking a word between 5 and 12 characters long
word = pick_word(a)
word.downcase!
end
word.downcase!
#initialize some variables
guess = ""
@ -61,11 +69,12 @@ scrambled_word << word
hidden_letters = generate_hidden_letters(word)
scrambled_word = scramble_word(word, hidden_letters, scrambled_word)
puts "Word is #{word}"
print "\n\nWord is #{word}\n" #cheating XD
while true
#puts the array as a readable string
print"\n"
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
#check game end conditions
@ -79,8 +88,7 @@ while true
exit
end
#get player input
print "Guess a letter: "
print "Guess a letter: " #get player input
guess = gets.chop
unless guesses.include?(guess) #keep track of previous guesses
@ -92,18 +100,10 @@ while true
wrong_guesses = wrong_guesses + 1
end
print "Guesses: #{guesses.inspect[1...-1].gsub('"',"").gsub(',','')}\n\n"
print "\n\nGuesses: #{guesses.inspect[1...-1].gsub('"',"").gsub(',','')}\n"
scrambled_word = unscramble_word(word, guesses, scrambled_word)
hidden_letters.delete(guess)
print_hangman(wrong_guesses) #^add correct letters to the board
hidden_letters.delete(guess) #and remove guessed letters from the hidden_letters array
end
#if input is included in word but not hidden word then
#if word[x] == input then hidden[x] == input
#do this over the array
#if guess == a hidden letter
# fill in that letter
#else
# increase hangman counter