Skip to content

Commit f790597

Browse files
Create 1958-check-if-move-is-legal.js
1 parent 2d0e0cc commit f790597

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var checkMove = function(board, rMove, cMove, color) {
2+
const ROWS = board.length, COLS = board[0].length;
3+
let direction = [[1, 0], [-1, 0], [0, 1], [0, -1],
4+
[1, 1], [-1, -1], [1, -1], [-1, 1]];
5+
board[rMove][cMove] = color;
6+
7+
let legal = function(row, col, color, direc) {
8+
let dr = direc[0], dc = direc[1];
9+
row = row + dr;
10+
col = col + dc;
11+
let length = 1;
12+
13+
while(0 <= row && row < ROWS && 0 <= col && col < COLS) {
14+
length += 1;
15+
if(board[row][col] == '.') return false;
16+
if(board[row][col] == color)
17+
return length >= 3;
18+
row = row + dr;
19+
col = col + dc;
20+
}
21+
return false;
22+
}
23+
24+
for(const d of direction)
25+
if(legal(rMove, cMove, color, d)) return true;
26+
return false;
27+
};

0 commit comments

Comments
 (0)