Commit 5e1435d9 authored by Romain DUPONT's avatar Romain DUPONT
Browse files

instoration du tour par tour

parent df99fa37
import { Chessboard, isEmpty, Square, squareAtPosition, pieceAtPosition } from "./chessboard";
import { Move } from "./movements";
import { Move, whitePlay } from "./movements";
import { equals, left, right, bottom, top, position } from "./position";
/**
......@@ -17,20 +17,21 @@ import { equals, left, right, bottom, top, position } from "./position";
*/
export function blackPawnMove(board: Chessboard, move: Move): boolean {
// #TODO: Manage special 'En passant' move.
if(!whitePlay){
if (equals(move.to!, top(move.from!))) {
//console.log("Single forward");
return isEmpty(board, move.to!);
}
if (equals(move.to!, top(move.from!))) {
//console.log("Single forward");
return isEmpty(board, move.to!);
}
if (move.from!.rank === 6 && equals(move.to!, top(top(move.from!)))) {
//console.log("Double forward");
return isEmpty(board, top(move.from!)) && isEmpty(board, move.to!);
}
if (move.from!.rank === 6 && equals(move.to!, top(top(move.from!)))) {
//console.log("Double forward");
return isEmpty(board, top(move.from!)) && isEmpty(board, move.to!);
}
if (equals(move.to!, left(top(move.from!))) || equals(move.to!, right(top(move.from!)))) {
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || !destination.piece!.isWhite)
if (equals(move.to!, left(top(move.from!))) || equals(move.to!, right(top(move.from!)))) {
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || !destination.piece!.isWhite)
}
}
return false;
......@@ -50,18 +51,19 @@ export function blackPawnMove(board: Chessboard, move: Move): boolean {
*/
export function whitePawnMove(board: Chessboard, move: Move): boolean {
// #TODO: Manage special 'En passant' move.
if(whitePlay){
if (equals(move.to!, bottom(move.from!))) {
return isEmpty(board, move.to!);
}
if (equals(move.to!, bottom(move.from!))) {
return isEmpty(board, move.to!);
}
if (move.from!.rank === 1 && equals(move.to!, bottom(bottom(move.from!)))) {
return isEmpty(board, bottom(move.from!)) && isEmpty(board, move.to!);
}
if (move.from!.rank === 1 && equals(move.to!, bottom(bottom(move.from!)))) {
return isEmpty(board, bottom(move.from!)) && isEmpty(board, move.to!);
}
if (equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!)))) {
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || destination.piece!.isWhite)
if (equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!)))) {
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || destination.piece!.isWhite)
}
}
return false;
......@@ -78,7 +80,8 @@ 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)
&& (isEmpty(board, move.to!) || isEatPossible(board, move))
&& !isDangerousCase(board,move.to!.rank,move.to!.file,pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite) );
&& !isDangerousCase(board,move.to!.rank,move.to!.file,pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite)
&& whitePlay == pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite );
}
/**
......@@ -91,7 +94,8 @@ export function kingMove(board: Chessboard, move: Move): boolean {
*/
export function queenMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return (rankFileMove(board, move) || DiagonalEmpty(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
return (rankFileMove(board, move) || DiagonalEmpty(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move)
&& whitePlay == pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite );
}
/**
......@@ -104,7 +108,8 @@ export function queenMove(board: Chessboard, move: Move): boolean {
*/
export function rookMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return (rankFileMove(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
return ((rankFileMove(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move)))
&& whitePlay == pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite ;
}
......@@ -119,7 +124,8 @@ export function rookMove(board: Chessboard, move: Move): boolean {
*/
export function bishopMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this
return (DiagonalEmpty(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move));
return (DiagonalEmpty(board, move)) && (isEmpty(board, move.to!) || isEatPossible(board, move))
&& whitePlay == pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite ;
}
......@@ -140,11 +146,12 @@ export function knightMove(board: Chessboard, move: Move): boolean {
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)));
(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))
&& whitePlay == pieceAtPosition(board, position(move.from!.file, move.from!.rank)).isWhite );
}
/*let whitePlay : boolean = true;
function round(color:boolean)*/
......
......@@ -4,6 +4,7 @@ import * as pieces from './piece'
import {Piece} from './piece'
import * as isPossible from './move-validation'
export let whitePlay : boolean = true;
const VALID_MOVE_STRING: RegExp = new RegExp('([a-z]|[A-Z])([1-8])-([A-H]|[a-z])([1-8])')
export interface Move {
......@@ -37,7 +38,8 @@ export function processMove(chessboard:Chessboard, moveString: string): boolean
let move : Move = parseMoveString(moveString);
if (move.isValid && isMovePossible(chessboard, move)) {
performMove(chessboard, move);
performMove(chessboard, move);
whitePlay = !whitePlay;
} else {
console.log("Invalid movement !");
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