Open faizalzakaria opened 7 years ago
Hi Faizal, Here is my attempt. Would this be valid for it to prevent the pawn from moving forward (if its a white piece on the top)
if(piece.isWhite){
if(board[x][y + 1]){
console.log("can't move there")
return
}
}
for black pawn if it is on the bottom
if(!piece.isWhite){
if(board[x][y - 1]){
console.log("can't move there")
return
}
}
@kbanico , you can implement it here, https://github.com/faizalzakaria/react-chess-app/blob/master/src/components/Pawn.js#L21
The validMove
missed the condition.
return !this.hasFriendlyPiece(piece, opponentPiece) &&
(dx === 0 &&
(dy === validY[0] ||
(piece.firstMove && dy === validY[1]))) ||
(this.hasOpponentPiece(piece, opponentPiece) && Math.abs(dx) === 1 && dy === validY[0]);
!this.hasFriendlyPiece(piece, opponentPiece)
, only checks if there is a friendly piece, it should also block the move if there is an opponent piece.
Does this make sense ?
Pawn can only move forward if there isn't any piece in front of him.