-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckForWin.m
More file actions
82 lines (72 loc) · 2.69 KB
/
checkForWin.m
File metadata and controls
82 lines (72 loc) · 2.69 KB
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
function [gameOver] = checkForWin(currentBoard, chip1, chip2)
%checkForWin checks the board to see if the game has been won
% currentBoard: The current board variable (with sprite numbers)
% chip1: Sprite number for player 1's chip
% chip2: Sprite number for player 2's chip
% gameOver: True if the game is over, False if no connect-4s are detected
gameOver = false;
% Horizontal Check -
for Row = 2:7
for Column = 1:4
if chip1 == currentBoard(Row, Column) && ...
chip1 == currentBoard(Row, Column+1) && ...
chip1 == currentBoard(Row, Column+2) && ...
chip1 == currentBoard(Row, Column+3) || ...
chip2 == currentBoard(Row, Column) && ...
chip2 == currentBoard(Row, Column+1) && ...
chip2 == currentBoard(Row, Column+2) && ...
chip2 == currentBoard(Row, Column+3)
gameOver = true;
return
end
end
end
% Vertical Check |
for Row = 2:4
for Column = 1:7
if chip1 == currentBoard(Row, Column) && ...
chip1 == currentBoard(Row+1, Column) && ...
chip1 == currentBoard(Row+2, Column) && ...
chip1 == currentBoard(Row+3, Column) || ...
chip2 == currentBoard(Row, Column) && ...
chip2 == currentBoard(Row+1, Column) && ...
chip2 == currentBoard(Row+2, Column) && ...
chip2 == currentBoard(Row+3, Column) ...
gameOver = true;
return
end
end
end
% Diagonal Check /
for Row = 7:-1:4
for Column = 1:4
if chip1 == currentBoard(Row, Column) && ...
chip1 == currentBoard(Row-1, Column+1) && ...
chip1 == currentBoard(Row-2, Column+2) && ...
chip1 == currentBoard(Row-3, Column+3) || ...
chip2 == currentBoard(Row, Column) && ...
chip2 == currentBoard(Row-1, Column+1) && ...
chip2 == currentBoard(Row-2, Column+2) && ...
chip2 == currentBoard(Row-3, Column+3) ...
gameOver = true;
return
end
end
end
% Diagonal Check \
for Row = 2:4
for Column = 1:4
if chip1 == currentBoard(Row, Column) && ...
chip1 == currentBoard(Row+1, Column+1) && ...
chip1 == currentBoard(Row+2, Column+2) && ...
chip1 == currentBoard(Row+3, Column+3) || ...
chip2 == currentBoard(Row, Column) && ...
chip2 == currentBoard(Row+1, Column+1) && ...
chip2 == currentBoard(Row+2, Column+2) && ...
chip2 == currentBoard(Row+3, Column+3) ...
gameOver = true;
return
end
end
end
end