-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-kyu-tic-tac-toe-checker.js
90 lines (70 loc) · 1.93 KB
/
5-kyu-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
81
82
83
84
85
86
87
88
89
90
// 5 kyu | Tic-Tac-Toe Checker
// https://www.codewars.com/kata/525caa5c1bf619d28c000335
/*
If we were to set up a Tic-Tac-Toe game, we would want to know whether the
board's current state is solved, wouldn't we? Our goal is to create a function
that will check that for us!
Assume that the board comes in the form of a 3x3 array, where the value is 0 if
a spot is empty, 1 if it is an "X", or 2 if it is an "O", like so:
```
[[0, 0, 1],
[0, 1, 2],
[2, 1, 0]]
```
We want our function to return:
- -1 if the board is not yet finished (there are empty spots),
- 1 if "X" won,
- 2 if "O" won,
- 0 if it's a cat's game (i.e. a draw).
You may assume that the board passed in is valid in the context of a game of
Tic-Tac-Toe.
*/
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
// You have passed all of the tests! :)
const isSolved = board => {
const lines = new Array(8).fill('');
let isFinished = true;
for (let i = 0; i < 3; i++)
for (let j = 0; j < 3; j++) {
if (0 === board[i][j]) isFinished = false;
lines[i] += board[i][j];
lines[3 + j] += board[i][j];
if (i === j) lines[6] += board[j][i];
if (2 === i + j) lines[7] += board[j][i];
}
for (const line of lines)
if ('111' === line) return 1;
else if ('222' === line) return 2;
return isFinished ? 0 : -1;
};
// 〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰〰
import { strictEqual } from 'assert';
strictEqual(
isSolved([
[0, 0, 1],
[0, 1, 2],
[2, 1, 0],
]),
-1,
);
/*
0 1 2
+---+---+---+
0 | 0 | 0 | 1 |
1 | 0 | 1 | 2 |
2 | 2 | 1 | 0 |
+---+---+---+
[0, 0], [0, 1], [0, 2]
[1, 0], [1, 1], [1, 2]
[2, 0], [2, 1], [2, 2]
[0, 0], [1, 1], [2, 2]
[0, 2], [1, 1], [2, 0]
*/
strictEqual(
isSolved([
[1, 1, 1],
[0, 2, 2],
[0, 0, 0],
]),
1,
);