-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_for_win.m
59 lines (49 loc) · 1.72 KB
/
check_for_win.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
function win = check_for_win(grid, column_just_played, how_many_to_connect)
row_just_played = find(grid(:,column_just_played)>0,1);
if isempty(row_just_played)
error('column_just_played is empty');
end
player_just_played = grid(row_just_played, column_just_played);
win = false;
% Check vertical
row_start = row_just_played;
row_stop = row_start+how_many_to_connect-1;
if row_start >=1 && row_stop <= size(grid,1)
attempt = grid(row_start:row_stop,column_just_played);
if length(unique(attempt)) == 1
win = true;
return
end
end
for offset = 0:how_many_to_connect-1
% Check horizontal
column_stop = column_just_played+offset;
column_start = column_stop-how_many_to_connect+1;
if column_start >=1 && column_stop <= size(grid,2)
attempt = grid(row_just_played,column_start:column_stop);
if length(unique(attempt)) == 1
win = true;
return
end
end
% Check backslash diagonal
row_stop = row_just_played+offset;
row_start = row_stop-how_many_to_connect+1;
if row_start >=1 && row_stop <= size(grid,1) && column_start >=1 && column_stop <= size(grid,2)
attempt = diag(grid(row_start:row_stop,column_start:column_stop));
if length(unique(attempt)) == 1
win = true;
return
end
end
% Check forwardslash diagonal
row_start = row_just_played-offset;
row_stop = row_start+how_many_to_connect-1;
if row_start >=1 && row_stop <= size(grid,1) && column_start >=1 && column_stop <= size(grid,2)
attempt = diag(fliplr(grid(row_start:row_stop,column_start:column_stop)));
if length(unique(attempt)) == 1
win = true;
return
end
end
end