Commit 6d5957a9 authored by Saad MAHBOUBI's avatar Saad MAHBOUBI
Browse files

fin de la fonction rookMove, QueenMove et bishopMove qui marche tres

bien.
parent 7e2040cf
......@@ -46,7 +46,7 @@ export function isEmptyFile(chessboard : Chessboard, move: Move): boolean{
start = move.from!.rank;
}
for(let i : number = start + 1; i < end ; i = i + 1) {
for(let i : number = start + 1; i < end ; i++) {
let movePosition : Position = { rank : i , file : move.from!.file};
let squareMove : Square = squareAtPosition(chessboard, movePosition);
if(!squareMove.isEmpty){
......@@ -69,8 +69,8 @@ export function isEmptyRank(chessboard : Chessboard, move: Move): boolean{
}
for(let i : number = start + 1; i < end ; i = i + 1) {
let movePosition : Position = { rank : move.from!.rank , file : i};
for(let i : number = start + 1; i < end ; i++) {
let movePosition : Position = {rank : move.from!.rank , file : i};
let squareMove : Square = squareAtPosition(chessboard, movePosition);
if(!squareMove.isEmpty){
return false;
......@@ -79,6 +79,44 @@ export function isEmptyRank(chessboard : Chessboard, move: Move): boolean{
return true;
}
export function isEmptyDiagonal(chessboard : Chessboard, move: Move): boolean{
let startFile: number ;
let endFile : number ;
let startRank: number ;
let endRank : number ;
if (move.from!.file > move.to!.file) {
startFile = move.to!.file;
endFile = move.from!.file;
} else {
endFile = move.to!.file;
startFile = move.from!.file;
}
if (move.from!.rank > move.to!.rank) {
startRank = move.to!.rank;
endRank = move.from!.rank;
} else {
endRank = move.to!.rank;
startRank = move.from!.rank;
}
if(!((endFile - startFile) === (endRank - startRank))){
return false;
}
for(let i : number = startRank + 1; i < endRank ; i++) {
for(let j : number = startFile + 1; j < endFile ; j++) {
let movePosition : Position = { rank : i , file : j};
let squareMove : Square = squareAtPosition(chessboard, movePosition);
if(!squareMove.isEmpty){
return false;
}
}
}
return true;
}
export interface Square {
position : Position
......
import { Chessboard, isEmpty, Square, squareAtPosition, isEmptyFile, isEmptyRank } from "./chessboard";
import { Chessboard, isEmpty, Square, squareAtPosition, isEmptyFile, isEmptyRank, isEmptyDiagonal } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, bottom, top } from "./position";
import * as pieces from './piece';
......@@ -81,83 +81,36 @@ export function kingMove(board: Chessboard, move: Move): boolean {
let destination: Square = squareAtPosition(board, move.to!);
if (equals(move.to!, bottom(move.from!))) {
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
}
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if (equals(move.to!, top(move.from!))) {
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if (equals(move.to!, right(move.from!))) {
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if (equals(move.to!, left(move.from!))) {
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if(equals(move.to!, bottom(left(move.from!)))){
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if(equals(move.to!, bottom(right(move.from!)))){
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if(equals(move.to!, top(right(move.from!)))){
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
if(equals(move.to!, top(left(move.from!)))){
if(isEmpty(board, move.to!)){
return true;
}else if(depart.piece!.isWhite === !destination.piece!.isWhite){
return true;
}else{
return false;
};
return isEmpty(board, move.to!) || (depart.piece!.isWhite === !destination.piece!.isWhite);
}
return false;
......@@ -174,7 +127,19 @@ export function kingMove(board: Chessboard, move: Move): boolean {
*/
export function queenMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
let depart: Square = squareAtPosition(board, move.from!);
let destination: Square = squareAtPosition(board, move.to!);
if (move.from!.rank === move.to!.rank && isEmptyRank(board, move) || isEmptyDiagonal(board, move)){
return isEmpty(board, move.to!) || depart.piece!.isWhite === !destination.piece!.isWhite ;
}
if (move.from!.file === move.to!.file && isEmptyFile(board, move) || isEmptyDiagonal(board, move)){
return isEmpty(board, move.to!) || depart.piece!.isWhite === !destination.piece!.isWhite ;
}
return false;
}
/**
......@@ -212,7 +177,15 @@ export function rookMove(board: Chessboard, move: Move): boolean {
*/
export function bishopMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
let depart: Square = squareAtPosition(board, move.from!);
let destination: Square = squareAtPosition(board, move.to!);
if(isEmptyDiagonal(board, move)){
return isEmpty(board, move.to!) || depart.piece!.isWhite === !destination.piece!.isWhite ;
}
return false;
}
/**
......
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