Commit 2d306101 authored by Stewan Lheureux's avatar Stewan Lheureux
Browse files

Mise à jour de move validation

parent 74744c54
......@@ -2,6 +2,14 @@ import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, top, bottom } from "./position";
// Fonction de validation des moves
function isValidEat(board : Chessboard, move : Move): boolean {
let start : Square = squareAtPosition(board, move.from!);
let destination : Square = squareAtPosition(board, move.to!);
return (start.piece!.isWhite !== destination.piece!.isWhite || !start.piece!.isWhite !== !destination.piece!.isWhite)
}
/**
* Checks whether a Black Pawn can perform a given move.
* A pawn can move forward to the unoccupied square immediately in front of
......@@ -71,8 +79,17 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function kingMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
// Mouvement normal & mangeage de pièce
if (equals(move.to!, bottom(move.from!)) || equals(move.to!, right(move.from!)) || equals(move.to!, top(move.from!)) || equals(move.to!, left(move.from!))
|| equals(move.to!, left(top(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!)))) {
let destination: Square = squareAtPosition(board, move.to!);
return (destination.isEmpty || isValidEat(board,move))
}
return false;
}
/**
......@@ -84,8 +101,9 @@ export function kingMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function queenMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
return false;
}
/**
......@@ -101,10 +119,65 @@ export function queenMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function empressMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
/* Comme la Tour mais avec en plus les mouvements du chevalier */
/*
// 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 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!.file-move.to!.file!); // combien de cases on veut traverser ?
let moveFromTop = move.from!;
for (let i=1; i<=moveRange; i++) {
// Si on va en haut
if (move.from!.file!-move.to!.file! < 0) {
if (equals(move.to!, top(move.from!))) {
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;
}
}
moveFromTop = top(moveFromTop);
}
}
}
return false;
}
/**
* Checks whether a Princess can perform a given move.
* A princess can move any number of squares diagonally,
......@@ -134,6 +207,35 @@ export function princessMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function camelMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
}
\ No newline at end of file
/* Comme Chevalier mais avec une case de plus */
// Mouvement vers le bas
if (equals(move.to!, left(bottom(bottom(bottom(move.from!))))) || equals(move.to!, right(bottom(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(top(move.from!))))) || equals(move.to!, right(top(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(left(move.from!))))) || equals(move.to!, top(left(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(right(move.from!))))) || equals(move.to!, top(right(right(right(move.from!)))))) {
let destination: Square = squareAtPosition(board, move.to!);
return (destination.isEmpty || isValidEat(board,move))
}
return false;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment