Validate a Chess Bishop, Rook, or Queen Move
Updated
Problem statement
You are writing the backend for a two-player chess game, and the server must reject illegal moves. Write the move check for the pieces that slide across the board: the bishop, the rook, and the queen.
The board is given as board, 8 strings of 8 characters. A position is written [x, y], where x is the column and y is the row, so the square at [x, y] is character x of board[y]. Both start at 0, and row 0 is the first string.
A '.' is an empty square. A letter is a piece: K king, Q queen, R rook, B bishop, N knight, and P pawn. Uppercase letters are white pieces and lowercase letters are black pieces.
The square start holds a bishop, rook, or queen. Return true if that piece can move from start to end, and false otherwise. A move is valid when all of these hold:
- The piece moves:
endis a different square fromstart. - The move has the right shape. A bishop moves diagonally, so the column and the row change by the same amount. A rook moves in a straight line, so the column or the row stays the same. A queen can move either way.
- Every square strictly between
startandendis empty. A piece cannot jump over another piece. - The square
endis empty or holds a piece of the other color, which is captured. A piece cannot land on a piece of its own color.
Only the moving piece's own movement counts. Ignore whose turn it is, check, and every other rule of chess.
Function
isValidMove(board: String[], start: int[], end: int[]) → booleanExamples
Example 1
board = ["........","........","........","........","........","........","........","..B....."]start = [2,7]end = [5,4]return = trueThe white bishop is at [2, 7], column 2 of the bottom row. To reach [5, 4] it moves 3 columns right and 3 rows up, so the move is diagonal. The squares in between, [3, 6] and [4, 5], are empty, and the target square is empty. The move is valid.
Example 2
board = ["........","........","........","........","........","....p...","........","..B....."]start = [2,7]end = [5,4]return = falseThe same bishop move, but a black pawn stands on [4, 5], one of the squares in between. A bishop cannot jump over a piece, so the move is not valid.
Example 3
board = ["R.......","........","........","........","........","n.......","........","........"]start = [0,0]end = [0,5]return = trueThe white rook at [0, 0] moves straight down column 0 to [0, 5]. Rows 1 to 4 of that column are empty. The target holds a black knight, a piece of the other color, so the rook captures it. The move is valid.
Example 4
board = ["........","....n...","........","...Q....","........","........","........","........"]start = [3,3]end = [4,1]return = falseThe white queen at [3, 3] would move 1 column right and 2 rows up. That is neither a straight line nor a diagonal, so a queen cannot make this move, even though the target holds a black knight that could otherwise be captured.
Constraints
board.length == 8andboard[i].length == 8- Every character of
boardis one of.KQRBNPkqrbnp. start.length == 2andend.length == 20 <= start[i], end[i] <= 7- The square at
startholds a bishop, rook, or queen of either color.