Commit 1347e84a authored by Romain DUPONT's avatar Romain DUPONT
Browse files

Replace move-validation.ts

parent 4328fb56
import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, bottom, top } from "./position";
import { equals, left, right, bottom, top, position } from "./position";
/**
* Checks whether a Black Pawn can perform a given move.
......@@ -89,7 +89,7 @@ export function kingMove(board: Chessboard, move: Move): boolean {
*/
export function queenMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
return rankFileMove(board, move);
}
/**
......@@ -102,8 +102,10 @@ export function queenMove(board: Chessboard, move: Move): boolean {
*/
export function rookMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
}
return rankFileMove(board, move);
}
/**
* Checks whether a Bishop can perform a given move.
......@@ -133,4 +135,51 @@ export function bishopMove(board: Chessboard, move: Move): boolean {
export function knightMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
}
\ No newline at end of file
}
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++) {
if (!isEmpty(board, position(i, move.to!.rank))) {
return false;
}
}
return true;
}
} 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++) {
if (!isEmpty(board, position(move.to!.file, i))) {
return false;
}
}
return true;
}
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--) {
if (!isEmpty(board, position(i, move.to!.rank))) {
return false;
}
}
return true;
}
} 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--) {
if (!isEmpty(board, position(move.to!.file, i))) {
return false;
}
}
return true;
}
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