-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiagonalCheck.js
78 lines (67 loc) · 2.23 KB
/
diagonalCheck.js
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
// Pseudo
// Checks for diagonal moving from bottom left to top right
// 4 item where n..n+3, y.. y-3
// edge case if n < 0 || n > 6
// y < 0 || y > 5
// * Start check with col[0] for either red or black and check neighbor index[col] diagonal top right
// * If neighbor is incorrect - move to next item in column
// * If neighbor is correct - check next neighbor
// * If reaches boundary
// * board[column][row]
function diagonalCheck(board, color) {
return checkBottomLeftUp(board, color) ||
checkTopLeftDown(board, color);
}
function checkBottomLeftUp(board, color) {
for(col=0; col < 4; col++){
for(row=3; row < 6; row++){
if (board[col][row] === color) {
if (board[col+1][row-1] === color) {
if (board[col+2][row-2] === color){
if (board[col+3][row-3] === color){
return true;
}
}
}
}
}
}
return false //finishes checking all staring nodes and doesn't find 4 in a row will return false
};
function checkTopLeftDown(board, color) {
for(col=0; col < 4; col++){
for(row=0; row < 3; row++){
if (board[col][row] === color) {
if (board[col+1][row+1] === color) {
if (board[col+2][row+2] === color){
if (board[col+3][row +3] === color){
return true;
}
}
}
}
}
}
return false //finishes checking all staring nodes and doesn't find 4 in a row will return false
};
// // Test -----------
// var board2 = [
// [ 'red','','','','','','' ],
// [ '','red','','','','','' ],
// [ '','','red','','','','' ],
// [ '','','','red','','','' ],
// [ '','','','','','','' ],
// [ '','','','','','','' ],
// [ '','','','','','','' ] ]
// var board3 = [
// [ 'red','','','blue','','','' ],
// [ '','red','','','','','red' ],
// [ 'red','','blue','','blue','','red' ],
// [ '','blue','','red','blue','blue','' ],
// [ '','red','','red','blue','','' ],
// [ 'blue','','blue','blue','','','' ],
// [ '','','blue','','','','' ] ]
// console.log(checkWin(board, 'red') === true);
// console.log(checkBottomLeftUp(board,'red') === true);
// console.log(checkTopLeftDown(board2,'red') === true);
// console.log(diagonalCheck(board3, 'blue')) === true;