Skip to content

Commit 74846a4

Browse files
authored
Create 999-available-captures-for-rook.js
1 parent 98898d9 commit 74846a4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

999-available-captures-for-rook.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {number}
4+
*/
5+
const numRookCaptures = function(board) {
6+
for (let i = 0; i < board.length; ++i)
7+
for (let j = 0; j < board[i].length; ++j)
8+
if (board[i][j] == 'R') return cap(board,i,j,0,1)+cap(board,i,j,0,-1)+cap(board,i,j,1,0)+cap(board,i,j,-1,0);
9+
return 0;
10+
};
11+
12+
function cap(b, x, y, dx, dy) {
13+
while (x >= 0 && x < b.length && y >= 0 && y < b[x].length && b[x][y] != 'B') {
14+
if (b[x][y] == 'p') return 1;
15+
x += dx; y += dy;
16+
}
17+
return 0;
18+
}

0 commit comments

Comments
 (0)