-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
94 lines (81 loc) · 1.81 KB
/
test.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
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
class Code
attr_reader :colors
attr_accessor :user_guess, :sequence
def initialize
@sequence = []
@colors = ["R", "G", "B", "Y", "O", "P"]
@user_guess = []
end
def randomize
self.sequence = []
4.times do
self.sequence << colors.sample
end
end
def feedback
puts "You got #{exact_matches} guesses exactly right (black ball)."
puts "You got #{near_matches} almost right (white ball)."
end
def exact_matches
0.tap do |counter|
@user_guess.each_index do |el|
puts "Answer #{sequence[el]} vs User #{@user_guess[el]}" #Testing purposes only
counter += 1 if @user_guess[el] == sequence[el]
end
end
end
def near_matches
counter = 0
@user_guess.uniq.each do |el|
upper_bound = sequence.count(el)
color_count = @user_guess.count(el)
counter += [color_count, upper_bound].sort[0]
end
counter - exact_matches
end
def won?
exact_matches == 4
end
end
class Game
attr_accessor :turns
attr_reader :code
def initialize
@turns = 10
@code = Code.new
@code.randomize
end
def get_input
while true
puts "Enter your guess (4 letters):"
user_input = gets.chomp.upcase.split(//)
if user_input.length != 4 ||
user_input.any? { |color| [email protected]?(color)}
puts "Invalid guess."
else
@code.user_guess = user_input
break
end
end
end
def print_output
@code.feedback
end
def play
until @turns == 0 || @code.won?
puts "Turns remaining: #{turns}"
get_input
print_output
@turns -= 1
end
if @code.won?
puts "Congratulations, you win!"
else
puts "Sorry, maybe next time."
end
end
end
if __FILE__ == $PROGRAM_NAME
game = Game.new
game.play
end