-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessing_Game.rb
More file actions
95 lines (78 loc) · 1.71 KB
/
Guessing_Game.rb
File metadata and controls
95 lines (78 loc) · 1.71 KB
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
class GuessingGame
GAME_LENGTH = 5
def initialize
@trys = 1
@wins = 0
greeting
play_round
display_score
end
def play_round
try = 0
GAME_LENGTH.times do
@round = GameRound.new
@round.play
if @round.user_guessed_correctly?
@wins += 1
end
try = @round.trys
end
end
def greeting
msg = "Welcome to the Game :)"
puts msg
puts "-" * msg.length
end
def display_score
puts "you won #{@wins} out of #{GAME_LENGTH}"
end
end
class GameRound
MAX_NUMBER = 5
def play
pick_number
user_guess
print_results
end
def pick_number
@number = rand(1..MAX_NUMBER)
end
def user_guess
print "pick a number between 1 and #{MAX_NUMBER}: "
@guess = gets.chomp.to_i
too_high
end
def too_high
if @guess > MAX_NUMBER
puts "number too high"
user_guess
end
end
def user_guessed_correctly?
@guess == @number
end
def print_results
if user_guessed_correctly?
puts "Correct!"
@trys = 1
else
user_guess
if user_guessed_correctly?
puts "Correct!"
@trys = 2
else
user_guess
if user_guessed_correctly?
puts "Correct!"
@trys = 3
else
puts "Sorry, the number was #{@number}"
end
end
end
end
def trys
@trys
end
end
GuessingGame.new