added hangman graphics. game is finished
This commit is contained in:
parent
53b032310e
commit
3b71f69f19
44
hangman.rb
44
hangman.rb
@ -2,8 +2,8 @@
|
|||||||
#01/21/2022
|
#01/21/2022
|
||||||
|
|
||||||
def pick_word(a)
|
def pick_word(a)
|
||||||
rand_int = rand(0..a.size)
|
rand_int = rand(0..a.size) #pick a num within the range of the file length
|
||||||
return a[rand_int].chop!
|
return a[rand_int].chop! #and format the word
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_hidden_letters(word)
|
def generate_hidden_letters(word)
|
||||||
@ -28,9 +28,9 @@ def scramble_word(word, hidden_letters, scrambled_word)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def unscramble_word(word, guesses, scrambled_word)
|
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|
|
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
|
scrambled_word[i] = letter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -38,18 +38,26 @@ def unscramble_word(word, guesses, scrambled_word)
|
|||||||
return scrambled_word
|
return scrambled_word
|
||||||
end
|
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
|
a = Array.new
|
||||||
dict = File.open('5desk.txt')
|
dict = File.open('5desk.txt')
|
||||||
dict.each do |row|
|
dict.each do |row| #loading the dict file into an array
|
||||||
a.push(row)
|
a.push(row)
|
||||||
end
|
end
|
||||||
|
|
||||||
#picking a word between 5 and 12 characters long
|
|
||||||
word = pick_word(a)
|
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 = pick_word(a)
|
||||||
word.downcase!
|
|
||||||
end
|
end
|
||||||
|
word.downcase!
|
||||||
|
|
||||||
#initialize some variables
|
#initialize some variables
|
||||||
guess = ""
|
guess = ""
|
||||||
@ -61,11 +69,12 @@ scrambled_word << word
|
|||||||
hidden_letters = generate_hidden_letters(word)
|
hidden_letters = generate_hidden_letters(word)
|
||||||
scrambled_word = scramble_word(word, hidden_letters, scrambled_word)
|
scrambled_word = scramble_word(word, hidden_letters, scrambled_word)
|
||||||
|
|
||||||
puts "Word is #{word}"
|
print "\n\nWord is #{word}\n" #cheating XD
|
||||||
|
|
||||||
while true
|
while true
|
||||||
|
|
||||||
#puts the array as a readable string
|
#puts the array as a readable string
|
||||||
|
print"\n"
|
||||||
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
||||||
|
|
||||||
#check game end conditions
|
#check game end conditions
|
||||||
@ -79,8 +88,7 @@ while true
|
|||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
||||||
#get player input
|
print "Guess a letter: " #get player input
|
||||||
print "Guess a letter: "
|
|
||||||
guess = gets.chop
|
guess = gets.chop
|
||||||
|
|
||||||
unless guesses.include?(guess) #keep track of previous guesses
|
unless guesses.include?(guess) #keep track of previous guesses
|
||||||
@ -92,18 +100,10 @@ while true
|
|||||||
wrong_guesses = wrong_guesses + 1
|
wrong_guesses = wrong_guesses + 1
|
||||||
end
|
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)
|
scrambled_word = unscramble_word(word, guesses, scrambled_word)
|
||||||
|
print_hangman(wrong_guesses) #^add correct letters to the board
|
||||||
hidden_letters.delete(guess)
|
hidden_letters.delete(guess) #and remove guessed letters from the hidden_letters array
|
||||||
|
|
||||||
end
|
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
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user