Rook Captures on Chessboard

Easy
18.2% Acceptance

In this lab, you have to create a function that will find the number of available captures for a rook on an 8x8 chessboard. The board will have a white rook 'R', a certain number of white bishops 'B', black pawns 'p', and empty squares '.'. You have to count the number of pawns that the rook can capture in its current position.

To understand how a rook can capture pawns in this lab, let's go through some rules of how the rook will move:

  • The rook can choose one of the four cardinal directions: north, east, south, or west.
  • It moves in that direction until it chooses to stop, reaches the edge of the board, captures a black pawn, or is blocked by a white bishop.
  • A rook is considered attacking a pawn if it can capture the pawn on the rook's turn.

The function you have to build must return the number of available captures for the white rook.

For a better understanding, let's go through some examples:

Example 1:

Board:

. . . . . . . . . . . p . . . . . . . R . . . p . . . . . . . . . . . . . . . . . . . p . . . . . . . . . . . . . . . . . . . .

In this example, the rook is attacking all three pawns. So, the answer should be 3.

Example 2:

Board:

. . . . . . . . . p p p p p . . . p p B p p . . . p B R B p . . . p p B p p . . . p p p p p . . . . . . . . . . . . . . . . . .

In this example, the bishops are blocking the rook from attacking any of the pawns. So, the answer should be 0.

Example 3:

Board:

. . . . . . . . . . . p . . . . . . . p . . . . p p . R . p B . . . . . . . . . . . . B . . . . . . . p . . . . . . . . . . . .

In this example, the rook is attacking the pawns at positions b5, d6, and f5. So, the answer should be 3.

To solve this lab, you must follow these constraints:

  • board.length == 8
  • board[i].length == 8
  • board[i][j] is either 'R', '.', 'B', or 'p'
  • There is exactly one cell with board[i][j] == 'R'

You must export the function numRookCaptures as an ES module to be evaluated.

Challenges:

  1. Export a function named numRookCaptures.
  2. The function should take an 8x8 chessboard array as input and return the number of available captures for the rook.
  3. Ensure the function follows the constraints and rules mentioned above.