-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.rb
73 lines (65 loc) · 2.32 KB
/
hangman.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
rightLetters = []
wrongLetters = []
allWords = IO.readlines('words.txt')
for i in 0...allWords.length
allWords[i] = allWords[i].downcase.chomp
end
puts "Welcome to Hangman!"
puts "Enter 1 to play with a word of your choosing -- It has to be real!"
puts "Enter 2 to play with a randomly selected word from our dictionary"
choice = gets.to_i
if (choice == 1)
while(1)
puts "Enter your word!"
word = gets.downcase.chomp
if(allWords.include?(word))
break
else
puts "Not a valid word, try again!"
end
end
else
word = allWords.sample.downcase
end
wordArr = word.split(//)
puts "Your word is #{word.length} letters long, how many lives to do want?"
lives = gets.to_i
puts "Let us begin with the hanging of the man\n\n\n\n"
until lives == 0 || rightLetters.length == wordArr.uniq.length
puts "Lives Remaining: #{lives}"
puts "Wrong Letters Bank: "
wrongLetters.each{|let| print "#{let} "}
print "\n"
puts "Correct Guesses: "
for i in 0...wordArr.length
if rightLetters.include?(wordArr[i])
print "#{wordArr[i]} "
else
print "_ "
end
end
print "\n"
puts "Enter your next guess"
guess = gets.downcase.chomp
if (guess.length != 1 || Alphabet.include?(guess) == false)
puts "Not a valid guess, try again"
next
elsif wrongLetters.include?(guess) || rightLetters.include?(guess)
puts "Not valid . . . You already guessed that letter! \n////////////////////\n\n\n"
else
if (wordArr.include?(guess))
puts "What a good guess! \n////////////////////\n\n\n"
rightLetters.push(guess)
else
puts "What a bad guess \n////////////////////\n\n\n"
wrongLetters.push(guess)
lives = lives - 1
end
end
end
if lives != 0
puts "Congratulations . . . YOU WIN!!"
else
puts "You lose, the correct word was #{word}"
end