Commit e10aca41 authored by Romain DUPONT's avatar Romain DUPONT
Browse files

importationn de la fonction isEatPossible qui valide si l'on peut manger ou non les pieces

parent 3314b2d9
import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Chessboard, isEmpty, Square, squareAtPosition, pieceAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, bottom, top, position } from "./position";
......@@ -76,7 +76,8 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
*/
export function kingMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return (Math.abs(move.to!.rank-move.from!.rank)==1 || Math.abs(move.to!.file-move.from!.file)==1);
return (Math.abs(move.to!.rank-move.from!.rank)==1 || Math.abs(move.to!.file-move.from!.file)==1) &&( isEmpty(board, move.to!) || isEatPossible(board, move));
}
/**
......@@ -89,7 +90,7 @@ export function kingMove(board: Chessboard, move: Move): boolean {
*/
export function queenMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return (rankFileMove(board, move) || DiagonalMove(board, move));
return (rankFileMove(board, move) || DiagonalMove(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
}
/**
......@@ -102,7 +103,7 @@ export function queenMove(board: Chessboard, move: Move): boolean {
*/
export function rookMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return rankFileMove(board, move);
return (rankFileMove(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
}
......@@ -117,7 +118,7 @@ export function rookMove(board: Chessboard, move: Move): boolean {
*/
export function bishopMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this
return DiagonalMove(board, move);
return (DiagonalMove(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
}
/**
......@@ -134,9 +135,10 @@ export function bishopMove(board: Chessboard, move: Move): boolean {
*/
export function knightMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return (((Math.abs(move.to!.rank-move.from!.rank)==1 && Math.abs(move.to!.file-move.from!.file)==2) ||
(Math.abs(move.to!.rank-move.from!.rank)==2 && Math.abs(move.to!.file-move.from!.file)==1) )&& isEmpty(board,move.to!));
return (
(((Math.abs(move.to!.rank - move.from!.rank) == 1 && Math.abs(move.to!.file - move.from!.file) == 2) ||
(Math.abs(move.to!.rank - move.from!.rank) == 2 && Math.abs(move.to!.file - move.from!.file) == 1)))&&( isEmpty(board, move.to!) || isEatPossible(board, move)));
}
......@@ -144,7 +146,7 @@ export function knightMove(board: Chessboard, move: Move): boolean {
function rankFileMove(board: Chessboard, move: Move): boolean {
if (move.from!.file < move.to!.file) {
if (move.from!.rank === move.to!.rank) {
for (let i: number = move.from!.file + 1; i <= move.to!.file; i++) {
for (let i: number = move.from!.file + 1; i < move.to!.file; i++) {
if (!isEmpty(board, position(i, move.to!.rank))) {
return false;
}
......@@ -153,7 +155,7 @@ function rankFileMove(board: Chessboard, move: Move): boolean {
}
} else if (move.from!.rank < move.to!.rank){
if (move.from!.file === move.to!.file) {
for (let i: number = move.from!.rank + 1; i <= move.to!.rank; i++) {
for (let i: number = move.from!.rank + 1; i < move.to!.rank; i++) {
if (!isEmpty(board, position(move.to!.file, i))) {
return false;
}
......@@ -164,7 +166,7 @@ function rankFileMove(board: Chessboard, move: Move): boolean {
return false;
} else if (move.from!.file > move.to!.file) {
if (move.from!.rank === move.to!.rank) {
for (let i: number = move.from!.file - 1; i >= move.to!.file; i--) {
for (let i: number = move.from!.file - 1; i > move.to!.file; i--) {
if (!isEmpty(board, position(i, move.to!.rank))) {
return false;
}
......@@ -173,7 +175,7 @@ function rankFileMove(board: Chessboard, move: Move): boolean {
}
} else if (move.from!.rank > move.to!.rank) {
if (move.from!.file === move.to!.file) {
for (let i: number = move.from!.rank - 1; i >= move.to!.rank; i--) {
for (let i: number = move.from!.rank - 1; i > move.to!.rank; i--) {
if (!isEmpty(board, position(move.to!.file, i))) {
return false;
}
......@@ -229,4 +231,9 @@ function DiagonalMove(board: Chessboard, move: Move): boolean {
}
} return false ;
}
function isEatPossible(chessboard: Chessboard, move : Move):boolean {
return( (pieceAtPosition(chessboard,position(move.from!.file, move.from!.rank)).isWhite) != (pieceAtPosition(chessboard,position(move.to!.file, move.to!.rank)).isWhite));
}
\ 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