-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_maker.rb
69 lines (63 loc) · 1.43 KB
/
code_maker.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
class Mastermind
def play_maker
@@player_name = "computer"
if @@guesses_remaining == 12
puts "Enter your secret code. Choose 4 digits from 1 to 6."
@@player_code = gets.chomp
while (@@player_code.length != 4) || !(@@player_code =~ /[1-6]{4}/)
puts "Code must be 4 digits from 1-6"
@@player_code = gets.chomp
end
@@player_code = @@player_code.split("")
computer_guess_two
else
computer_guess_two
end
if Game.check(@guess, @@player_code)
play_maker
end
end
def computer_guess
if @@guesses_remaining == 12
@guess = first_guess
else
@guess = @numbers.sample(4)
end
end
def computer_guess_two
if @@guesses_remaining == 12
@guess = first_guess
else
filter_guesses
@guess = @possible_guesses.sample
end
return @guess
end
def first_guess
@guess = ["1", "1", "2", "2"]
end
def filter_guesses
@possible_guesses.reject! do |code|
guess_check(@guess, code)
@current_count != @@prev_count
end
end
def guess_check(guess, code)
@@temp = code.dup
@correct = 0
@position = 0
for i in (0..3)
if (guess[i] == @@temp[i])
@correct += 1
@@temp[i] = "x"
end
end
for j in (0..3)
if guess.include?(@@temp[j])
@position += 1
end
end
@current_count = [@correct, @position]
return @current_count
end
end