-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.rb
93 lines (87 loc) · 2.47 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Game
attr_accessor :random_word, :word
@@tries = 1
def initialize
pick_random_line
length_selector(@random_word)
print_enigma
play_game
end
def pick_random_line
@random_word = File.readlines("google-10000-english-no-swears.txt").sample.to_s
# p random_word.is_a?(String)
end
def length_selector(word)
until word.length >= 6 && word.length <= 12
pick_random_line
end
@word = @random_word.chomp
# puts word.is_a?(String)
end
def print_enigma
enigma = @word.tr('a-zA-Z', '_')
@enigma = enigma.chomp.split(//)
puts enigma
puts @word
end
def play_game
while @@tries < 6
puts 'Type a letter to see if it is included in the word'
letter = gets.chomp.downcase
# def guessing
# if @@tries > 1
# puts "Do you want to guess already? Type yes or no"
# want_guess = gets.chomp.downcase
# if want_guess != "yes" || want_guess != "no"
# puts "Come again?"
# want_guess = gets.chomp.downcase
# elsif want_guess == "yes"
# puts "Alright, you may guess!"
# guess = gets.chomp.downcase
# if guess == @word
# puts "That's right! You win!"
# @@tries = 7
# else
# puts "That's not it! Next round!"
# end
# elsif want_guess == "no"
# puts "Alright, proceed!"
# @@tries -= 1
# end
# end
end
if @enigma.include?(letter)
puts 'You have already selected this character'
@@tries -= 1
elsif letter.ord < 97 || letter.ord > 122
puts 'Please type a valid character'
letter = gets.chomp.downcase
end
if @word.include?(letter)
a = (0 ... @word.length).find_all { |i| @word[i,1] == letter }
p a
def substitute_characters_in_enigma(original_string, indexes, character)
indexes.each do |index|
original_string[index] = character
end
original_string
end
substitute_characters_in_enigma(@enigma, a, letter)
p @enigma
end
@@tries += 1
p @@tries
guessing
if @@tries == 6
puts 'Final round! Time to guess the word!'
guess = gets.chomp.downcase
if guess == @word
puts "That's right! You win!"
elsif
puts "Too bad! Wrong guess! You loose!"
end
end
end
end
end
Game.new