-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_guessing_game.rb
61 lines (45 loc) · 1.37 KB
/
number_guessing_game.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
# Assignment 1:
# Create a number guessing game.
# Game should pick a random number between 1 and 100 and ask you for a guess.
# If the guess is less than the number, inform the user and let them guess again.
# If the guess is greater than the number, inform the user and let them guess again.
# If the guess is correct, inform the user and quit.
# Bonus: write the opposite program: you, the user, pick a number between 1 and 100.
# The computer has to guess the number.
# You tell it whether it's too high, too low, or right on after each guess.
# The computer gets five guesses.
def number_guess
a = rand(1..100).to_i
p a
print "Take a guess my friend: "
response = gets.chomp
until response.to_i == a
if response.to_i > a
puts "Your number is greater than number. Guess again"
response = gets.chomp
elsif response.to_i < a
puts "Your number is smaller than number. Guess again"
response = gets.chomp
end
end
p "You are a great Gusser!"
end
def i_pick
p "Let me pick a number from 1 to 100:"
user = gets.chomp
a = rand(1..100)
count = 0
until a == user.to_i || count > 5
if a > user.to_i
p "#{a} is too high"
a = rand(1..100)
elsif a < user.to_i
p "#{a} is too low"
a = rand(1..100)
elsif a == user.to_i
p "I got u right"
end
count += 1
end
end
i_pick()