From 22be2e6bbe0278f4a493d1535a0c4d25d54be9dd Mon Sep 17 00:00:00 2001 From: Stewan Lheureux <stewan.l-heureux@etu.univ-nantes.fr> Date: Fri, 10 Apr 2020 19:48:27 +0200 Subject: [PATCH] =?UTF-8?q?Finalisation=20des=20fonctions=20de=20validatio?= =?UTF-8?q?n=20des=20pi=C3=A8ces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/ts/move-validation.ts | 209 ++++++++++++++++++++++++++++++++- 1 file changed, 205 insertions(+), 4 deletions(-) diff --git a/src/main/ts/move-validation.ts b/src/main/ts/move-validation.ts index de99f9c..31e37bf 100644 --- a/src/main/ts/move-validation.ts +++ b/src/main/ts/move-validation.ts @@ -102,7 +102,187 @@ export function kingMove(board: Chessboard, move: Move): boolean { */ export function queenMove(board: Chessboard, move: Move): boolean { +// Mouvements de la Tour haut et bas + +if (move.from!.file == move.to!.file) { // Le déplacement se fait-il sur la même colonne ? + + let moveRange = Math.abs(move.from!.rank-move.to!.rank!); // combien de cases on veut traverser en haut ou en bas ? + let moveFromTop = move.from!; + let moveFromBottom = move.from!; + + for (let i=1; i<=moveRange; i++) { + + // Si on va en haut + if (move.from!.rank!-move.to!.rank! < 0) { + + if (equals(move.to!, top(moveFromTop))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, top(moveFromTop)); + if (!destination.isEmpty) { + return false; + } + } + } + + // Si on va en bas + if (move.from!.rank!-move.to!.rank! > 0) { + + if (equals(move.to!, bottom(moveFromBottom))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, bottom(moveFromBottom)); + if (!destination.isEmpty) { + return false; + } + } + } + moveFromTop = top(moveFromTop); + moveFromBottom = bottom(moveFromBottom); + } +} + +// Mouvements de la Tour gauche et droite + + if (move.from!.rank == move.to!.rank) { // Le déplacement se fait-il sur la même ligne ? + + let moveRange = Math.abs(move.from!.file-move.to!.file!); // combien de cases on veut traverser à gauche ou à droite ? + let moveFromRight = move.from!; + let moveFromLeft = move.from!; + + for (let i=1; i<=moveRange; i++) { + + + // Si on va à droite + if (move.from!.file!-move.to!.file! < 0) { + + if (equals(move.to!, right(moveFromRight))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, right(moveFromRight)); + if (!destination.isEmpty) { + return false; + } + } + } + + // Si on va à gauche + if (move.from!.file!-move.to!.file! > 0) { + + if (equals(move.to!, left(moveFromLeft))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, left(moveFromLeft)); + if (!destination.isEmpty) { + return false; + } + } + } + + moveFromRight = right(moveFromRight); + moveFromLeft = left(moveFromLeft); + } + } + + // Mouvements du Fou + +if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.from!.file)) { + + let moveRange = Math.abs(move.from!.rank-move.to!.rank); + let moveFrom11pm = move.from!; + let moveFrom8pm = move.from!; + let moveFrom5pm = move.from!; + let moveFrom2pm = move.from!; + + for (let i=1; i<=moveRange; i++) { + + // Si on va en haut à gauche (11pm) + + if (move.to!.file < move.from!.file && move.to!.rank > move.from!.rank) { + + if (equals(move.to!, left(top(moveFrom11pm)))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, left(top(moveFrom11pm))); + if (!destination.isEmpty) { + return false; + } + } + } + + // Si on va en haut à droite (2pm) + + if (move.to!.file > move.from!.file && move.to!.rank > move.from!.rank) { + + if (equals(move.to!, right(top(moveFrom2pm)))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, right(top(moveFrom2pm))); + if (!destination.isEmpty) { + return false; + } + } + } + + // Si on va en bas à gauche (8pm) + + if (move.to!.file < move.from!.file && move.to!.rank < move.from!.rank) { + + if (equals(move.to!, left(bottom(moveFrom8pm)))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, left(bottom(moveFrom8pm))); + if (!destination.isEmpty) { + return false; + } + } + } + + // Si on va en bas à droite (5pm) + if (move.to!.file > move.from!.file && move.to!.rank < move.from!.rank) { + + if (equals(move.to!, right(bottom(moveFrom5pm)))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)); + } + + else { + let destination: Square = squareAtPosition(board, right(bottom(moveFrom5pm))); + if (!destination.isEmpty) { + return false; + } + } + } + + moveFrom11pm = left(top(moveFrom11pm)); + moveFrom8pm = left(bottom(moveFrom8pm)); + moveFrom5pm = right(bottom(moveFrom5pm)); + moveFrom2pm = right(top(moveFrom2pm)); + + } +} return false; } @@ -254,6 +434,29 @@ export function empressMove(board: Chessboard, move: Move): boolean { */ export function princessMove(board: Chessboard, move: Move): boolean { +// Mouvements du Chevalier + + // Mouvement vers le bas + if (equals(move.to!, left(bottom(bottom(move.from!)))) || equals(move.to!, right(bottom(bottom(move.from!))))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)) + } + // Mouvement vers le haut + if (equals(move.to!, left(top(top(move.from!)))) || equals(move.to!, right(top(top(move.from!))))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)) + } + // Mouvement vers la gauche + if (equals(move.to!, bottom(left(left(move.from!)))) || equals(move.to!, top(left(left(move.from!))))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)) + } + // Mouvement vers la droite + if (equals(move.to!, bottom(right(right(move.from!)))) || equals(move.to!, top(right(right(move.from!))))) { + let destination: Square = squareAtPosition(board, move.to!); + return (destination.isEmpty || isValidEat(board,move)) + } + // Mouvements du Fou if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.from!.file)) { @@ -264,12 +467,11 @@ if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.f let moveFrom5pm = move.from!; let moveFrom2pm = move.from!; - for (let i=1; i<=moveRange; i++) { // Si on va en haut à gauche (11pm) - if (move.to!.file > move.from!.file && move.to!.rank < move.from!.rank) { + if (move.to!.file < move.from!.file && move.to!.rank > move.from!.rank) { if (equals(move.to!, left(top(moveFrom11pm)))) { let destination: Square = squareAtPosition(board, move.to!); @@ -301,7 +503,6 @@ if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.f } } - // Si on va en bas à gauche (8pm) if (move.to!.file < move.from!.file && move.to!.rank < move.from!.rank) { @@ -321,7 +522,7 @@ if (Math.abs(move.to!.rank - move.from!.rank) == Math.abs(move.to!.file - move.f // Si on va en bas à droite (5pm) - if (move.to!.file < move.from!.file && move.to!.rank > move.from!.rank) { + if (move.to!.file > move.from!.file && move.to!.rank < move.from!.rank) { if (equals(move.to!, right(bottom(moveFrom5pm)))) { let destination: Square = squareAtPosition(board, move.to!); -- GitLab