Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 22be2e6b rédigé par Stewan Lheureux's avatar Stewan Lheureux
Parcourir les fichiers

Finalisation des fonctions de validation des pièces

parent adea0437
Branches master
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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!);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter