forked from AdaGold/word-guess-csv
-
Notifications
You must be signed in to change notification settings - Fork 41
/
word_guess.rb
134 lines (109 loc) · 3.25 KB
/
word_guess.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
class WordGuess
def initialize(debug = false)
# are we in debug mode?
@debug = debug
# possible words, selected at random
@words = {
"e" => %w(dog cat bug hat cap lit kin fan fin fun tan ten tin ton),
"m" => %w(plain claim brine crime alive bride skine drive slime stein jumpy),
"h" => %w(
machiavellian prestidigitation plenipotentiary quattuordecillion
magnanimous unencumbered bioluminescent circumlocution
)
}
# players attempts allowed by difficulty
@tries = {
"e" => 10,
"m" => 6,
"h" => 4
}
# ask the user to set the game mode
mode = set_mode
@word = @words[mode].sample # chosen word; players try to guess this
@guesses = @tries[mode] # how many tries the player gets
@user_word = "•" * @word.length # a "blank word" for user output
@guessed = [] # keep track of letters that have been guessed
# debugging?
if @debug
puts "Your word is #{ @word }."
end
# user messages
puts "You have #{ @guesses } guesses."
puts "Guess the word: #{ @user_word }"
# start the first turn
play_turn
end
def play_turn
# a turn begins by asking a player for their guess
letter = ask_for_letter
# update the word with the letter, maybe
update_user_word!(letter)
# decrement the available guesses
# unless the letter matches and it's not already in the @guessed array
lose_a_turn?(letter)
# push the letter into the guessed array, if we need to
add_to_guessed(letter)
# debugging
puts "Previous guesses: #{ @guessed.join(" ") }"
puts "You guessed #{ letter }. The word is now #{ @user_word }."
puts "You have #{ @guesses } guesses left."
# determine if the player has won or lost
if won?
end_game(true)
elsif lost?
end_game(false)
else # play another turn if we haven't won or lost
play_turn
end
end
private
def add_to_guessed(letter)
# push the letter to the array, then get rid of duplicates.
@guessed.push(letter.upcase).uniq!
end
def end_game(won)
if won
puts "You wins the game! Yay! ^␣^"
else
puts "You did not wins the game. :( Next time you will, I bet. <3"
end
exit # game over!
end
def won?
# we win when the user has guessed all the letters to the word
@word == @user_word
end
def lost?
# we lose when the user is out of guesses and has not guessed the word
!won? && @guesses <= 0
end
def lose_a_turn?(letter)
# if the guessed letter isn't part of the word and
# the guessed letter isn't already in the list of guesses
if [email protected]?(letter) && [email protected]?(letter.upcase)
@guesses -= 1
end
end
def set_mode
mode = ""
until %w(e m h).include? mode
print "\nThis can be (e)asy, (m)edium or really (h)ard. The choice is yours: "
mode = gets.chomp
end
mode
end
def update_user_word!(letter)
@word.chars.each_index do |index|
@user_word[index] = letter if @word[index] == letter
end
end
def ask_for_letter
letter = ""
until ('a'..'z').include? letter
print "\nPlease guess a letter! (a..z): "
letter = gets.chomp.downcase
end
letter
end
end
WordGuess.new