-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.rb
53 lines (42 loc) · 1.11 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
# Hangman! Rails Girls Wellington March 2014.
guesses = []
guesses_left = 8
# select a random word
target_word = File.read("words.txt").split.sample
Kernel.loop do
# for each letter, display an underscore
target_word.chars.each do |letter|
if guesses.member?(letter)
Kernel.print(letter)
else
Kernel.print("_")
end
end
# guess a letter
Kernel.print("Guess a letter: ")
# get input from user
guess = Kernel.gets.strip
# TODO : validate user input
# check whether letter is in word
if target_word.index(guess) != nil
# if it is, display the letter the next time around
guesses.push(guess)
else
# if it's not, they get one less guess
guesses_left = guesses_left - 1
end
# if run out of guesses, say bad luck + exit
if guesses_left == 0
Kernel.puts("Sorry you ran out of guesses.")
break
end
# if they get all the letters, say you win + exit
letters_found = target_word.chars.select do |guess|
guesses.index(guess) != nil
end
if letters_found.length == target_word.length
Kernel.puts("Yay you won!")
break
end
# repeat
end