-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckWin_EC.m
77 lines (76 loc) · 3.04 KB
/
CheckWin_EC.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
function [int,object] = CheckWin_EC(matrix,player)
% J.Cohen
% Lab 3 Homework Part 1 - CheckWin
% 2019-10-01
% CheckWin
% This function checks to see if either player has won a game by using
% logic to find columns with matching markers in them. When a match is
% found the function draws a line through those matches
cross_one = [1,0,0;0,1,0;0,0,1];% create a win scinerio for specialty situations
cross_two = [0,0,1;0,1,0;1,0,0];% create a win scinerio for specialty situations
object = [];
if player == 1% determine which player we are checking
matrix(isnan(matrix))=0;% replace NaN values with falses
if all(matrix.*cross_one == cross_one)% check to see if the board matches special case 1
int = 1;% if so output is true
hold on
object = line([0,3],[0,3]);
object.Color = [0,0,0];
object.LineWidth = 3;
elseif all(matrix.*cross_two == cross_two)% check to see if the board matches special case 2
int = 1;% if so output is true
hold on
object = line([0,3],[3,0]);
object.Color = [0,0,0];
object.LineWidth = 3;
elseif any(all(matrix)) == 1% check to see if there is a vertical line match
int = 1;% if so output is true
hold on
x = find(double(all(matrix)))-.5;
object = line([x,x],[0,3]);
object.Color = [0,0,0];
object.LineWidth = 3;
elseif any(all(matrix')) == 1% check to see if there is a horizontal line match
int = 1;% if so output is true
hold on
y = find(double(all(matrix')))-.5;
object = line([0,3],[y,y]);
object.Color = [0,0,0];
object.LineWidth = 3;
else
int = 0;% otherwise output is false
end
elseif player == 2% determine which player we are checking
matrix(isnan(matrix))=1;% replace all NaN values with trues
matrix = ~matrix;% inverse the logical of the matrix
if all(matrix.*cross_one == cross_one)% check to see if the board matches special case 1
int = 2;% if so output is true
hold on
object = line([0,3],[0,3]);
object.Color = [1,0,0];
object.LineWidth = 3;
elseif all(matrix.*cross_two == cross_two)% check to see if the board matches special case 2
int = 2;% if so output is true
hold on
object = line([0,3],[3,0]);
object.Color = [1,0,0];
object.LineWidth = 3;
elseif any(all(matrix)) == 1% check to see if there is a vertical line match
int = 2;% if so output is true
hold on
x = find(double(all(matrix)))-.5;
object = line([x,x],[0,3]);
object.Color = [1,0,0];
object.LineWidth = 3;
elseif any(all(matrix')) == 1% check to see if there is a horizontal line match
int = 2;% if so output is true
hold on
y = find(double(all(matrix')))-.5;
object = line([0,3],[y,y]);
object.Color = [1,0,0];
object.LineWidth = 3;
else
int = 0;% otherwise output is false
end
end
end