-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessing_game_inv.rb
executable file
·36 lines (29 loc) · 1.15 KB
/
guessing_game_inv.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
# Inverted guessing game - the computer generates a number, and determines
# whether the user needs to chose bigger or smaller
#!env ruby
computers_choice = rand(100) + 1
remaining_guesses = 6
puts "I chose a random integer between 1 and 100. Take a guess as to what it is."
while remaining_guesses >= 1
remaining_guesses -= 1
# The user inputs a number
user_response = gets.chomp.to_i
# Checks to see if the input is an appropriate integer -
# If not, requests an appropriate integer
if user_response <= 0 or user_response > 100
puts "Please enter an integer - as a digit - between 1 and 100."
remaining_guesses += 1
else
# The computer tells the user whether the number is lower, higher, or spot-on
if user_response > computers_choice
puts "The number you are looking for is smaller than #{user_response}..."
elsif user_response < computers_choice
puts "The number you are looking for is bigger than #{user_response}..."
else user_response == computers_choice
puts "You got it! The number is #{computers_choice}!"
exit 0
end
end
end
# The default is the computer wins
puts "The number was #{computers_choice} - better luck next time!"