diff --git a/src/main/ts/move-validation.ts b/src/main/ts/move-validation.ts index a5b15d0d27d03ffd661f8e1b10e98e1e6cb75ab3..967fac0aa11dbd9d3bef3206e4917a8b6ba1bead 100644 --- a/src/main/ts/move-validation.ts +++ b/src/main/ts/move-validation.ts @@ -64,6 +64,58 @@ function isEmptyRank(chessboard : Chessboard, move: Move): boolean { return i === end; } +/** + *Check if the diagonals are empty + *@param chessboard + *@param move + */ +function isEmptyDiagonale(chessboard: Chessboard, move: Move): boolean{ + + let r: number = move.from!.rank; + let f: number = move.from!.file; + + let endR: number = move.to!.rank; + let endF: number = move.to!.file; + + //"↖" + if (f < endF && r > endR){ + f++ + r-- + while (f < endF && r > endR && chessboard.board[f][r].isEmpty){ + f++ + r-- + } + } + //"↘" + if (f > endF && r < endR){ + f-- + r++ + while (f > endF && r < endR && chessboard.board[f][r].isEmpty){ + f-- + r++ + } + } + //"↗" + if (f < endF && r < endR){ + f++ + r++ + while (f < endF && r < endR && chessboard.board[f][r].isEmpty){ + f++ + r++ + } + } + //"↙" + if (f > endF && r > endR){ + f-- + r-- + while (f > endF && r > endR && chessboard.board[f][r].isEmpty){ + f-- + r-- + } + } + return (f == endF && r == endR) +} + /** * Checks whether a Black Pawn can perform a given move. * A pawn can move forward to the unoccupied square immediately in front of @@ -163,25 +215,34 @@ export function queenMove(board: Chessboard, move: Move): boolean { let destination: Square = squareAtPosition(board, move.to!); let start: Square = squareAtPosition(board, move.from!); - for (let i = 0; i<Math.abs ((start.position.rank-destination.position.rank)+(start.position.file-destination.position.file));i++){ - if((equals(move.to!, top(move.from!))) || (equals(move.to!, right(move.from!))) || (equals(move.to!, left(move.from!))) || (equals(move.to!, bottom(move.from!)))) - { - if(start.piece!.isWhite){ - return !(destination.isEmpty || !destination.piece!.isWhite); - } - } - return true; + + // Validation to move in horizontal move + if (move.from!.rank === move.to!.rank && isEmptyRank(board,move)==true){ + + return (destination.isEmpty || destination.piece!.isWhite !== start.piece!.isWhite); } + // Validation of movement to move in vertical move + if (move.from!.file ===move.to!.file && isEmptyFile(board,move)==true) { + + return (destination.isEmpty || destination.piece!.isWhite !== start.piece!.isWhite); + } + // Validation of movement to move in diagonal - // Check that there is no pawn between origin square and the destination square - if (Math.abs(move.from!.rank - move.to!.rank) === Math.abs(move.from!.file - move.to!.file)) - { - return (destination.isEmpty||destination.piece!.isWhite !== start.piece!.isWhite); + // Check that there is no pawn between origin square and the destination square + const filedifference : number = start.position.file - destination.position.file; + const rankdifference : number = start.position.rank - destination.position.rank; + + // Le if vérifie que la valeur absolue des différences sont égales,ce qui premet de savoir si la trajectoire + // est diagonale, et si cette dernière est vide. + if ((Math.abs(filedifference) == Math.abs(rankdifference)) && (isEmptyDiagonale(board, move)==true)){ + return (destination.isEmpty || destination.piece!.isWhite !== start.piece!.isWhite); } + return false; } + /** diff --git a/src/test/ts/queen-move-validation.spec.ts b/src/test/ts/queen-move-validation.spec.ts index 763d66bf6e322f60e41ce22f352a6f5bfb339d1b..547d6b22e23dfa1116d90025cfba44eb47f1b957 100644 --- a/src/test/ts/queen-move-validation.spec.ts +++ b/src/test/ts/queen-move-validation.spec.ts @@ -151,7 +151,7 @@ export class TestQueenMoves { @Test("A Queen can capure pieces from a different color") testCanCaptureDifferentColor() { // Place a black Pawn on H4 - putPiece(chessboard, positionH4, pieces.whitePawn); + putPiece(chessboard, positionH4, pieces.blackPawn); let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true} // Check the move moveE4_H4 is possible Expect(isPossible.queenMove(chessboard, mvtCapture)).toBeTruthy();