-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectFour.js
67 lines (61 loc) · 1.69 KB
/
connectFour.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
function whoIsWinner(piecesPositionList) {
let board = [];
for (let i = 0; i < 6; i++) {
board.push(["_", "_", "_", "_", "_", "_", "_"]);
}
let winner = "Draw";
for (let i = 0; i < piecesPositionList.length; i++) {
let move = piecesPositionList[i].split("_");
let col = move[0].charCodeAt(0) - 65;
let row = 5;
while (row >= 0 && board[row][col] !== "_") {
row--;
}
board[row][col] = move[1];
for (let j = 0; j < 6; j++) {
for (let k = 0; k < 7; k++) {
if (board[j][k] !== "_") {
if (
k + 3 < 7 &&
board[j][k + 1] === board[j][k] &&
board[j][k + 2] === board[j][k] &&
board[j][k + 3] === board[j][k]
) {
winner = board[j][k];
return winner;
}
if (
j + 3 < 6 &&
board[j + 1][k] === board[j][k] &&
board[j + 2][k] === board[j][k] &&
board[j + 3][k] === board[j][k]
) {
winner = board[j][k];
return winner;
}
if (
k + 3 < 7 &&
j + 3 < 6 &&
board[j + 1][k + 1] === board[j][k] &&
board[j + 2][k + 2] === board[j][k] &&
board[j + 3][k + 3] === board[j][k]
) {
winner = board[j][k];
return winner;
}
if (
k - 3 >= 0 &&
j + 3 < 6 &&
board[j + 1][k - 1] === board[j][k] &&
board[j + 2][k - 2] === board[j][k] &&
board[j + 3][k - 3] === board[j][k]
) {
winner = board[j][k];
return winner;
}
}
}
}
}
return winner;
}