Check if Move is Legal LeetCode solution in JavaScript

1
var checkMove = function (board, rMove, cMove, color) {
2
const moves = [-1, 0, 1];
3

4
let count = 0;
5

6
for (let i = 0; i < 3; ++i) {
7
for (let j = 0; j < 3; ++j) {
8
if (i === 1 && j === 1) continue;
9

10
const rowDir = moves[i];
11
const colDir = moves[j];
12

13
if (isLegal(rMove, cMove, rowDir, colDir, color, 1)) return true;
14
}
15
}
16

17
return false;
18

19
function withinBound(row, col) {
20
return row >= 0 && col >= 0 && row < 8 && col < 8;
21
}
22

23
function isLegal(currRow, currCol, rowDir, colDir, startColor, length) {
24
if (!withinBound(currRow, currCol)) return false; // we went passed the boundaries
25
if (board[currRow][currCol] === startColor) return length >= 3; // we seen another start color
26
if (board[currRow][currCol] === "." && length > 1) return false;
27

28
return isLegal(
29
currRow + rowDir,
30
currCol + colDir,
31
rowDir,
32
colDir,
33
startColor,
34
length + 1
35
);
36
}
37
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0