-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.rb
161 lines (146 loc) · 3.22 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
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
require 'colorize'
class Hangman
def initialize(hash = {})
@secret_word = hash[:word] || File.readlines("possible_words.txt").sample.strip
@so_far = "-" * @secret_word.length
@num_wrong = 0
@guessed_list = []
@secret_word.upcase!
status
guess_a_letter
end
def current_info
puts @so_far
puts "Letters guessed: " + @guessed_list.sort.join(", ")
end
def guess_a_letter
until winning || losing
puts "What letter would you like to guess?"
guess = gets.chomp.upcase
if guessed_multiple_letters(guess)
puts "Please guess #{'one letter'.red} at a time."
elsif same_guess(guess)
puts "You aleady guessed that!"
elsif is_it_a_number(guess)
puts "That's a number. This is a game with letters."
else
evaluate_guess(guess)
end
end
end_game_message
end
def evaluate_guess(guess)
@guessed_list << guess
if @secret_word.include?(guess)
guess_is_right(guess)
puts "#{'Congratulations'.green} on your correct guess!"
puts "-----------------"
else
guess_is_wrong(guess)
puts "No, that's #{'wrong'.red}, sorry!"
puts "-----------------"
end
end
def end_game_message
if @so_far == @secret_word
puts "Congratulations! #{'YOU WON!'.green.underline}"
else
puts "Oh no! #{'You lost!'.upcase.red.underline}"
puts "GAME OVER!".red.underline
puts "The word was #{@secret_word.red}!"
end
end
def guess_is_right(guess)
@so_far = @secret_word.tr('^' + @guessed_list.join, '-')
puts status
end
def guess_is_wrong(guess)
@num_wrong += 1
puts status
end
def guessed_multiple_letters(guess)
guess.length != 1
end
def same_guess(guess)
@guessed_list.include?(guess)
end
def winning #Charlie Sheen
@so_far == @secret_word
end
def losing
@num_wrong == 6
end
def is_it_a_number(guess)
guess.to_i.to_s == guess
end
def status
case @num_wrong
when 0
puts "|--------|"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|============"
when 1
puts "|--------|"
puts "| O"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|============"
when 2
puts "|--------|"
puts "| O"
puts "| |"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|============"
when 3
puts "|--------|"
puts "| O"
puts "| /|"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|============"
when 4
puts "|--------|"
puts "| O"
puts "| /|\\"
puts "|"
puts "|"
puts "|"
puts "|"
puts "|============"
when 5
puts "|--------|"
puts "| O"
puts "| /|\\"
puts "| /"
puts "| |"
puts "|"
puts "|"
puts "|============"
when 6
puts "|--------|"
puts "| O"
puts "| /|\\"
puts "| /\\"
puts "| | |"
puts "|"
puts "|"
puts "|============"
end
puts ""
current_info
end
end
my_game = Hangman.new