-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.js
45 lines (36 loc) · 1.09 KB
/
tic-tac-toe.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
// You are given the state of a tic-tac-toe board as a list of 3 strings, like this:
// ```
// ['XOX',
// ' O ',
// ' ']
// ```
// You goal is to identify whether the game is over or not, and if it is, whether it's a draw or a win for X or O.
// Bonus points for returning the winning move for X or O if there is one.
const exampleBoard =
['XOX',
' O ',
' ']
// Assuming X has the first turn.
const checkHorizontal = (board) => {
let winFound = false
for (let index = 0; index < 3; index++) {
const currentRow = board[index]
const foundMatch = currentRow.split('').every((value) => currentRow[0] === value)
if (foundMatch) {
winFound = true
break;
};
}
return winFound;
}
const checkVertical = (board) => {
for (let index = 0; index < 3; index++) {
currentColumn = [board[0][index], board[1][index], board[2][index]]
const [first, ...rest] = currentColumn;
for (let j = 0; j < rest.length; j++) {
if (rest[j] !== first) return false;
}
}
}
const checkDiagonal = (board) => {
}