-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer_player.rb
65 lines (52 loc) · 1.15 KB
/
computer_player.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
class ComputerPlayer
attr_accessor :previous_guess, :board_size
def initialize(size)
@board_size = size
@matched_cards = {}
@known_cards = {}
@previous_guess = nil
end
def receive_revealed_card(pos, value)
@known_cards[pos] = value
end
def receive_match(pos1, pos2)
@matched_cards[pos1] = true
@matched_cards[pos2] = true
end
def get_input
if previous_guess
second_guess
else
first_guess
end
end
def unmatched_pos
(pos, _) = @known_cards.find do |pos, val|
@known_cards.any? do |pos2, val2|
(pos != pos2 && val == val2) &&
!(@matched_cards[pos] || @matched_cards[pos2])
end
end
pos
end
def match_previous
(pos, _) = @known_cards.find do |pos, val|
pos != previous_guess && val == @known_cards[previous_guess] &&
!@matched_cards[pos]
end
pos
end
def first_guess
unmatched_pos || random_guess
end
def second_guess
match_previous || random_guess
end
def random_guess
guess = nil
until guess && !@known_cards[guess]
guess = [rand(board_size), rand(board_size)]
end
guess
end
end