-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMove.m
95 lines (85 loc) · 2.66 KB
/
getMove.m
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
function board = getMove(board, first)
if(nargin == 1)
first = false;
end
[m, n] = size(board);
bestGuess = [0, 0];
guessConfidence = 0;
%Iterate over the board
for i = 1:m
for j = 1:n
%If the cell has a known number of neighbouring mines
if(board(i, j) > 0 && board(i, j) <= 8)
%Count its neighbouthood
[nBomb, nVeiled] = adjacency(board, i, j);
%For guessing
confidence = nVeiled/(board(i, j) - nBomb);
%If there are as many adjacent water spaces as remaining mines, mark all as mined
if(nVeiled == board(i, j) - nBomb)
for k = i-1:i+1
if(k > 0 && k <= m)
for l = j-1:j+1
if(l > 0 && l <= n) % && ~all([k, l] == [i, j]))
if(board(k, l) == -1)
board(k, l) = 10;
end
end
end
end
end
board(i, j) = 12;
%If all neighbouring mines have been found, mark remaining water spaces as safe
elseif(0 == board(i, j) - nBomb)
for k = i-1:i+1
if(k > 0 && k <= m)
for l = j-1:j+1
if(l > 0 && l <= n && ~all([k, l] == [i, j]))
if(board(k, l) == -1)
board(k, l) = 11;
end
end
end
end
end
board(i, j) = 12;
%Update best guess
elseif(confidence > guessConfidence)
bestGuess = [i, j];
guessConfidence = confidence;
end
end
end
end
if(~any(board == 10 | board == 11))
% fprintf('Making random choice...\n');
if(all(all(board == -1))) %The game has just started, we have to guess
fprintf('Making first move...\n');
candidates = find((board == -1));
pick = randi(length(candidates));
[i, j] = ind2sub([m, n], candidates(pick));
board(i, j) = 11;
else %Note: While neat, guessing near a number is actually a worse strategy than shooting into the blue
candidates = [];
for r = bestGuess(1)-1:bestGuess(1)+1
if(r > 0 && r <= m)
for c = bestGuess(2)-1:bestGuess(2)+1
if(c > 0 && c <= n)
if(board(r, c) == -1 && ~(r == bestGuess(1) && c == bestGuess(2)))
candidates = [candidates; r, c];
end
end
end
end
end
if(isempty(candidates))
[rows, cols] = ind2sub([m, n], find(board == -1));
candidates = [rows cols];
end
if(~isempty(candidates))
pick = randi(size(candidates, 1));
board(candidates(pick, 1), candidates(pick, 2)) = 11;
beep;
fprintf('Guessing at (%d, %d) with confidence %d...\n', candidates(pick, 1), candidates(pick, 2), guessConfidence);
end
end
end