Skip to content

Commit 268bf27

Browse files
Kyle WeissKyle Weiss
Kyle Weiss
authored and
Kyle Weiss
committed
Let's actually randomize the first guess...
1 parent 88a12da commit 268bf27

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

guessing_game.rb

+41-16
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,51 @@
66
max = 100
77
min = 1
88
remaining_guesses = 6
9+
first_guess = rand(100) + 1
910

1011
while remaining_guesses > 1
1112
remaining_guesses -= 1
12-
guess = (min + max) / 2
13-
puts "I will guess the number #{guess}."
14-
puts "Did I get the right number? Or should my next guess bigger or smaller?"
15-
16-
# If the guess is too big, the user should type smaller - or bigger, if it is too small
17-
user_response = gets.chomp
18-
19-
# The computer uses the response to guess its next number
20-
if user_response == "smaller"
21-
puts "The number you are looking for is smaller than your guess."
22-
max = guess
23-
elsif user_response == "bigger"
24-
puts "The number you are looking for is bigger than your guess."
25-
min = guess
13+
guess = first_guess
14+
15+
if remaining_guesses == 5
16+
puts "I will guess the number #{first_guess}."
17+
18+
puts "Did I get the right number? Or should my next guess bigger or smaller?"
19+
20+
# If the guess is too big, the user should type smaller - or bigger, if it is too small
21+
user_response = gets.chomp
22+
23+
# The computer uses the response to guess its next number
24+
if user_response == "smaller"
25+
puts "The number you are looking for is smaller than your guess."
26+
max = guess
27+
elsif user_response == "bigger"
28+
puts "The number you are looking for is bigger than your guess."
29+
min = guess
30+
else
31+
puts "Whew, I got it on the first try!"
32+
exit 0
33+
end
34+
2635
else
27-
puts "Bwahahahahaa, I win!"
28-
exit 0
36+
guess = (min + max) / 2
37+
puts "I will guess the number #{guess}."
38+
puts "Did I get the right number? Or should my next guess bigger or smaller?"
39+
40+
# If the guess is too big, the user should type smaller - or bigger, if it is too small
41+
user_response = gets.chomp
42+
43+
# The computer uses the response to guess its next number
44+
if user_response == "smaller"
45+
puts "The number you are looking for is smaller than your guess."
46+
max = guess
47+
elsif user_response == "bigger"
48+
puts "The number you are looking for is bigger than your guess."
49+
min = guess
50+
else
51+
puts "Bwahahahahaa, I win!"
52+
exit 0
53+
end
2954
end
3055
end
3156

0 commit comments

Comments
 (0)