-
Notifications
You must be signed in to change notification settings - Fork 1
/
computer.rb
50 lines (41 loc) · 1.27 KB
/
computer.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
class Computer
attr_accessor :color_spectrum, :guess_set, :correct_colors
def initialize
@color_spectrum = ["red", "green", "yellow", "blue", "black", "magenta", "cyan", "white"]
@guess_set = 0
@correct_colors = []
end
def guess(hints, turn)
computer_guess = get_guess(hints, turn)
print "#{computer_guess[0]}, #{computer_guess[1]}, #{computer_guess[2]}, #{computer_guess[3]}\n"
computer_guess
end
def get_guess(hints, turn)
computer_guess = []
if @correct_colors.uniq.length != 4
case turn
when 1
4.times { computer_guess << @color_spectrum[@guess_set] }
@guess_set += 1
when (2..8)
4.times { computer_guess << @color_spectrum[@guess_set] }
@correct_colors << @color_spectrum[@guess_set - 1] if color_is_correct(hints, turn)
@guess_set += 1 if @guess_set < 8
when 9
@correct_colors << @color_spectrum[@guess_set - 1] if color_is_correct(hints, turn)
computer_guess = @correct_colors
@correct_colors = @correct_colors.shuffle
else
computer_guess = @correct_colors
@correct_colors = @correct_colors.shuffle
end
else
computer_guess = @correct_colors
@correct_colors = @correct_colors.shuffle
end
computer_guess
end
def color_is_correct(hints, turn)
hints[12 - turn + 1].colors[0] == "green"
end
end