-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtic-tac-toe-checker.js
80 lines (73 loc) · 2.09 KB
/
tic-tac-toe-checker.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
79
80
function isSolved(board) {
const isWinner = (player, set) => set.every(cell => cell === player);
// check if either diagonal is solved by x or o
const backDiagonal = [board[0][0], board[1][1], board[2][2]];
if (isWinner(1, backDiagonal)) {
return 1;
} else if (isWinner(2, backDiagonal)) {
return 2;
}
const forwardDiagonal = [board[2][0], board[1][1], board[0][2]];
if (isWinner(1, forwardDiagonal)) {
return 1;
} else if (isWinner(2, forwardDiagonal)) {
return 2;
}
// check if each row is solved by x or o
for (let i = 0; i < 3; i++) {
const row = board[i];
if (isWinner(1, row)) {
return 1;
} else if (isWinner(2, row)) {
return 2;
}
}
// check if each column is solved by x or o
for (let i = 0; i < 3; i++) {
const col = [board[0][i], board[1][i], board[2][i]];
if (isWinner(1, col)) {
return 1;
} else if (isWinner(2, col)) {
return 2;
}
}
// if none of the above true and there are 0s anywhere, not solved
if (board.some(row => row.some(cell => cell === 0))) {
return -1;
}
// else draw
return 0;
}
function isSolved(board) {
const isWinner = (player, set) => set.every(cell => cell === player);
// check if each row is solved by x or o
const sets = [];
for (let i = 0; i < 3; i++) {
const row = board[i];
sets.push(row);
const col = [board[0][i], board[1][i], board[2][i]];
sets.push(col);
}
// check if either diagonal is solved by x or o
const backDiagonal = [board[0][0], board[1][1], board[2][2]];
sets.push(backDiagonal);
const forwardDiagonal = [board[2][0], board[1][1], board[0][2]];
sets.push(forwardDiagonal);
for (let i = 0; i < sets.length; i++) {
const set = sets[i];
if (isWinner(1, set)) return 1;
if (isWinner(2, set)) return 2;
}
// if none of the above true and there are 0s anywhere, not solved
if (board.some(row => row.some(cell => cell === 0))) {
return -1;
}
// else draw
return 0;
}
// You can use Test.expect(boolean, [optional] string) to test your code
console.log(isSolved([
[0,0,1],
[1,2,2],
[2,1,1]
]));