-
Notifications
You must be signed in to change notification settings - Fork 1
/
guessing_game3.rb
40 lines (30 loc) · 1.06 KB
/
guessing_game3.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
# cs50.tv pset1 skittles.c
class Game
attr_accessor :name, :target_number, :player_guess, :got_it_right, :counter
def intro
puts "We're going to play a guessing game. What's your name?"
@name = gets.chomp
puts "Hi #{name}! I'm so glad to meet you. I'm thinking of a number between 0 and 100! Go ahead and guess what it is!"
end
def set_number
@target_number = rand(100)
end
def check_for_win
@counter = 0
@got_it_right = false
while @got_it_right == false do
@counter += 1
@player_guess = gets.to_i
if @player_guess < 1 then puts "Now, don't be difficult. Try again!"
elsif @player_guess > 99 then puts "Now, don't be difficult. Try again!"
elsif @player_guess < @target_number then puts "Sorry, try a higher number!"
elsif @player_guess > @target_number then puts "Sorry, try a lower number."
else @got_it_right = true end
end
if @got_it_right == true then puts "Congratulations #{@name}, you won the game!" + "It only took #{@counter} tries! You're so very clever." end
end
end
test = Game.new
test.intro
test.set_number
test.check_for_win