faizalzakaria / react-chess-app

React Chess App For me to learn React.
0 stars 0 forks source link

Pawn shouldn't be able capture an opponent piece by moving forward. #1

Open faizalzakaria opened 7 years ago

faizalzakaria commented 7 years ago

Pawn can only move forward if there isn't any piece in front of him.

kbanico commented 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
        }
      }
faizalzakaria commented 7 years ago

@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 ?