-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhangman.rb
More file actions
226 lines (165 loc) · 4.1 KB
/
hangman.rb
File metadata and controls
226 lines (165 loc) · 4.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class Hangman
$LOAD_PATH << '.'
require 'module_screen.rb'
def self.draw(x)
hangman = [" O","/","|","\\","/ ","\\"]
if x == 0
puts hangman[0]
elsif x == 1
puts hangman[0]
print hangman[1]
elsif x==2
puts hangman[0]
print hangman[1]
print hangman[2]
elsif x==3
puts hangman[0]
print hangman[1]
print hangman[2]
print hangman[3]
elsif x==4
puts hangman[0]
print hangman[1]
print hangman[2]
puts hangman[3]
print hangman[4]
elsif x==5
puts hangman[0]
print hangman[1]
print hangman[2]
puts hangman[3]
print hangman[4]
print hangman[5]
print hangman[6]
end
end
def self.invalid(a,g)
until a.include?(g) == true
puts "INVALID ENTRY"
puts "Please guess from the available letters"
print "> "
g = gets.chomp.upcase.strip
end
end
def self.guessmethod()
puts "Guess a letter: "
print "> "
g = gets.chomp.upcase.strip
return g
end
def self.showLivesandAlphabet(alp,lives)
puts "\n\n"
puts "Lives Remaining: #{lives}"
puts "Letters Remaining: "
alp.each do |x|
print "#{x} "
end
puts "\n\n"
end
wrong = -1
puts "Choose one language to start "
puts "1-) English"
puts "2-) Turkish"
choice = gets.chomp
if(choice == "1")
dictionary = File.open('dictionary.txt')
dictionary_array = dictionary.readlines
dictionary_array.shuffle!
elsif (choice =="2")
dictionary = File.open('dictionary_tr.txt')
dictionary_array = dictionary.readlines
dictionary_array.shuffle!
end
puts "Type \"start\" to begin a new game\n"
turn = 0
rematch = nil
print "> "
user_word = gets.chomp.downcase.strip
until user_word == "start"
print "> "
user_word = gets.chomp.downcase.strip
end
until rematch == "quit"
# yeni bir kelimeyi shuffle ile rastgele alıyoruz sonrasında bu kelimenin harflerinden kelimeye uygun bir array cıkartıyoruz
dictionary_array.shuffle!
word = dictionary_array[0]
word_array = word.chars.to_a
letters_remaining = dictionary_array[0].chars.to_a
# \n i silmek icin
word_array.delete_at(word_array.length)
letters_remaining.delete_at(letters_remaining.length-1)
# alfabeyi tanımladık
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"]
lives = 6
# yeniden denemk isterse veya ilk oyunsa
if rematch == "new" || turn == 0
word_array.each do |x|
print "_ "
end
showLivesandAlphabet(alphabet,lives)
guess=guessmethod()
# harf secmek ıcın hakkı yyoksa veya harf kalmadıysa bu donguyu kıracagız
until lives == 0 || letters_remaining == []
Screen.clear
if word_array.include?(guess.downcase) == true
if wrong >= 0
draw(wrong)
end
puts "\n\n"
alphabet.delete(guess)
letters_remaining.delete(guess.downcase)
if letters_remaining == []
break
end
word_array.each do |x|
if alphabet.include?(x.upcase) == true
print "_ "
else
print "#{x.upcase} "
end
end
showLivesandAlphabet(alphabet,lives)
puts "\n\n"
guess = guessmethod
invalid(alphabet,guess)
elsif lives > 1
wrong += 1
if wrong >= 0
draw(wrong)
end
lives -= 1
puts "\nWRONG!"
alphabet.delete(guess)
word_array.each do |x|
if alphabet.include?(x.upcase) == true
print "_ "
else
print "#{x.upcase} "
end
end
showLivesandAlphabet(alphabet,lives)
puts "\n\n"
guess = guessmethod
invalid(alphabet,guess)
else
lives -= 1
wrong +=1
draw(wrong)
puts "YOU LOSE!\n"
puts "The word was #{word_array.to_s.upcase}"
end
end
if letters_remaining == []
puts "CONGRATULATIONS! You got the word #{word_array.to_s.upcase}"
end
turn += 1
puts "Type \"new\" to play again, type \"quit\" to exit"
print "> "
rematch = gets.chomp.downcase.strip
else
puts "Please type either \"new\" OR \"quit\""
end
end
end
app = Hangman.new