Commit e9c0b98b authored by E197700R's avatar E197700R
Browse files

ajout move en passant and test for pawn move validation

parent 4f2dd99a
......@@ -20,19 +20,66 @@ import { Piece } from './piece'
export function blackPawnMove(board: Chessboard, move: Move): boolean {
// #TODO: Manage special 'En passant' move.
let positionPiece: Position = { file: move.from!.file, rank: move.from!.rank };
if (board.historique[board.historique.length - 1] != undefined) {
let file: number = board.historique[board.historique.length - 1].to!.file;
let rank: number = board.historique[board.historique.length - 1].to!.rank;
if (bottom(board.historique[board.historique.length - 1].to!).file == move.to!.file &&
bottom(board.historique[board.historique.length - 1].to!).rank == move.to!.rank &&
pieceAtPosition(board, board.historique[board.historique.length - 1].to!).name == "White Pawn" &&
move.from!.rank - 1 == move.to!.rank &&
(
move.from!.file - 1 == move.to!.file ||
move.from!.file + 1 == move.to!.file
)
) {
board.nbCoups++;
board.historique.push(move);
let positionOfWhitePawn: Position = { file: file, rank: rank };
let squareOfWhitePawn: Square = { position: positionOfWhitePawn, isEmpty: true };
board.board[file][rank] = squareOfWhitePawn;
return true;
}
}
if (equals(move.to!, bottom(move.from!))) {
//console.log("Single forward");
return isEmpty(board, move.to!);
if (isEmpty(board, move.to!)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
}
}
if (move.from!.rank === 6 && equals(move.to!, bottom(bottom(move.from!)))) {
//console.log("Double forward");
return isEmpty(board, bottom(move.from!)) && isEmpty(board, move.to!);
if (isEmpty(board, bottom(move.from!)) && isEmpty(board, move.to!)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
}
}
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 (!(destination.isEmpty || !destination.piece!.isWhite)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
}
}
return false;
......@@ -53,10 +100,33 @@ export function blackPawnMove(board: Chessboard, move: Move): boolean {
export function whitePawnMove(board: Chessboard, move: Move): boolean {
// #TODO: Manage special 'En passant' move.
let positionPiece: Position = { file: move.from!.file, rank: move.from!.rank };
// 'En passant'
if (board.historique[board.historique.length - 1] != undefined) {
let file: number = board.historique[board.historique.length - 1].to!.file;
let rank: number = board.historique[board.historique.length - 1].to!.rank;
if (top(board.historique[board.historique.length - 1].to!)!.file == move.to!.file &&
top(board.historique[board.historique.length - 1].to!)!.rank == move.to!.rank &&
pieceAtPosition(board, board.historique[board.historique.length - 1].to!).name == "Black Pawn" &&
move.from!.rank + 1 == move.to!.rank &&
(
move.from!.file - 1 == move.to!.file ||
move.from!.file + 1 == move.to!.file
)
) {
board.nbCoups++;
board.historique.push(move);
let positionOfBlackPawn: Position = { file: file, rank: rank };
let squareOfBlackPawn: Square = { position: positionOfBlackPawn, isEmpty: true };
board.board[file][rank] = squareOfBlackPawn;
}
return true;
}
if (equals(move.to!, top(move.from!))) {
if (isEmpty(board, move.to!)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -67,6 +137,7 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
if (isEmpty(board, top(move.from!)) && isEmpty(board, move.to!)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -78,12 +149,12 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
if (!(destination.isEmpty || destination.piece!.isWhite)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
}
}
return false;
}
......@@ -119,6 +190,7 @@ export function kingMove(board: Chessboard, move: Move): boolean {
if (possible && arrival) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -249,6 +321,7 @@ export function queenMove(board: Chessboard, move: Move): boolean {
if (arrival && possible && !(collission)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -324,6 +397,7 @@ export function empressMove(board: Chessboard, move: Move): boolean {
if (arrival && possible && !(collission)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -406,6 +480,7 @@ export function princessMove(board: Chessboard, move: Move): boolean {
if (arrival && possible && !(collission)) {
board.nbCoups++;
squareAtPosition(board, positionPiece).isEmpty = true;
board.historique.push(move);
return true;
} else {
return false;
......@@ -434,17 +509,23 @@ export function camelMove(board: Chessboard, move: Move): boolean {
arrival = true;
}
}
return (
(
move.from!.file + 3 == move.to!.file ||
move.from!.file - 3 == move.to!.file ||
move.from!.rank + 3 == move.to!.rank ||
move.from!.rank - 3 == move.to!.rank)
if ((
move.from!.file + 3 == move.to!.file ||
move.from!.file - 3 == move.to!.file ||
move.from!.rank + 3 == move.to!.rank ||
move.from!.rank - 3 == move.to!.rank)
&& (
move.from!.rank + 1 == move.to!.rank ||
move.from!.rank - 1 == move.to!.rank ||
move.from!.file + 1 == move.to!.file ||
move.from!.file - 1 == move.to!.file)
&& (arrival)
)
&& (arrival)) {
board.nbCoups++;
board.historique.push(move);
squareAtPosition(board, positionPiece).isEmpty = true;
return true;
} else {
return false;
}
}
import { Expect, Test, Setup} from "alsatian";
import { Expect, Test, Setup } from "alsatian";
import * as isPossible from '../../main/ts/move-validation'
import * as pieces from '../../main/ts/piece'
import { Chessboard, createEmptyChessboard, putPiece } from '../../main/ts/chessboard';
import { Position, position } from '../../main/ts/position';
import { Move, move } from '../../main/ts/movements';
let chessboard : Chessboard;
const positionA4 : Position = position(0, 3) // A4
const positionA5 : Position = position(0, 4) // A5
const positionA6 : Position = position(0, 5) // A6
const positionA7 : Position = position(0, 6) // A7
const positionA8 : Position = position(0, 7) // A8
const positionB1 : Position = position(1, 0) // B1
const positionB2 : Position = position(1, 1) // B2
const positionB3 : Position = position(1, 2) // B3
const positionB5 : Position = position(1, 4) // B5
const positionB6 : Position = position(1, 5) // B6
const positionC3 : Position = position(2, 1) // C3
const positionC4 : Position = position(2, 3) // C4
const positionC5 : Position = position(2, 4) // C5
const positionC6 : Position = position(2, 5) // C6
const positionC7 : Position = position(2, 6) // C7
const positionD1 : Position = position(3, 0) // D1
const positionD2 : Position = position(3, 1) // D2
const positionD3 : Position = position(3, 2) // D3
const positionD4 : Position = position(3, 3) // D4
const positionD5 : Position = position(3, 4) // D5
const positionD6 : Position = position(3, 5) // D6
const positionD7 : Position = position(3, 6) // D7
const positionE1 : Position = position(4, 0) // E1
const positionE4 : Position = position(4, 3) // E4
const positionE8 : Position = position(4, 7) // E8
const positionF1 : Position = position(5, 0) // F1
const positionF2 : Position = position(5, 1) // F2
const positionF6 : Position = position(5, 5) // F6
const positionF7 : Position = position(5, 6) // F7
const positionG3 : Position = position(6, 2) // G3
const positionG5 : Position = position(6, 4) // G5
const positionH1 : Position = position(7, 0) // H1
const positionH3 : Position = position(7, 2) // H3
const positionH4 : Position = position(7, 3) // H4
const positionH5 : Position = position(7, 4) // H5
const positionH7 : Position = position(7, 6) // H7
let chessboard: Chessboard;
const positionA4: Position = position(0, 3) // A4
const positionA5: Position = position(0, 4) // A5
const positionA6: Position = position(0, 5) // A6
const positionA7: Position = position(0, 6) // A7
const positionA8: Position = position(0, 7) // A8
const positionB1: Position = position(1, 0) // B1
const positionB2: Position = position(1, 1) // B2
const positionB3: Position = position(1, 2) // B3
const positionB5: Position = position(1, 4) // B5
const positionB6: Position = position(1, 5) // B6
const positionC3: Position = position(2, 1) // C3
const positionC4: Position = position(2, 3) // C4
const positionC5: Position = position(2, 4) // C5
const positionC6: Position = position(2, 5) // C6
const positionC7: Position = position(2, 6) // C7
const positionD1: Position = position(3, 0) // D1
const positionD2: Position = position(3, 1) // D2
const positionD3: Position = position(3, 2) // D3
const positionD4: Position = position(3, 3) // D4
const positionD5: Position = position(3, 4) // D5
const positionD6: Position = position(3, 5) // D6
const positionD7: Position = position(3, 6) // D7
const positionE1: Position = position(4, 0) // E1
const positionE4: Position = position(4, 3) // E4
const positionE8: Position = position(4, 7) // E8
const positionF1: Position = position(5, 0) // F1
const positionF2: Position = position(5, 1) // F2
const positionF6: Position = position(5, 5) // F6
const positionF7: Position = position(5, 6) // F7
const positionG3: Position = position(6, 2) // G3
const positionG5: Position = position(6, 4) // G5
const positionH1: Position = position(7, 0) // H1
const positionH3: Position = position(7, 2) // H3
const positionH4: Position = position(7, 3) // H4
const positionH5: Position = position(7, 4) // H5
const positionH7: Position = position(7, 6) // H7
// Horizontal moves
const moveE4_H4 : Move = move(positionE4, positionH4);
const moveE4_A4 : Move = move(positionE4, positionA4);
const moveE4_H4: Move = move(positionE4, positionH4);
const moveE4_A4: Move = move(positionE4, positionA4);
// Vertical moves
const moveE4_E1 : Move = move(positionE4, positionE1);
const moveE4_E8 : Move = move(positionE4, positionE8);
const moveE4_E1: Move = move(positionE4, positionE1);
const moveE4_E8: Move = move(positionE4, positionE8);
// Diagonal moves
const moveE4_A8 : Move = move(positionE4, positionA8);
const moveE4_B1 : Move = move(positionE4, positionB1);
const moveE4_H7 : Move = move(positionE4, positionH7);
const moveE4_H1 : Move = move(positionE4, positionH1);
const moveE4_A8: Move = move(positionE4, positionA8);
const moveE4_B1: Move = move(positionE4, positionB1);
const moveE4_H7: Move = move(positionE4, positionH7);
const moveE4_H1: Move = move(positionE4, positionH1);
// Camel moves
const moveE4_F7 : Move = move(positionE4, positionF7);
const moveE4_H5 : Move = move(positionE4, positionH5);
const moveE4_F1 : Move = move(positionE4, positionF1);
const moveE4_H3 : Move = move(positionE4, positionH3);
const moveE4_D1 : Move = move(positionE4, positionD1);
const moveE4_B3 : Move = move(positionE4, positionB3);
const moveE4_B5 : Move = move(positionE4, positionB5);
const moveE4_D7 : Move = move(positionE4, positionD7);
const moveE4_F7: Move = move(positionE4, positionF7);
const moveE4_H5: Move = move(positionE4, positionH5);
const moveE4_F1: Move = move(positionE4, positionF1);
const moveE4_H3: Move = move(positionE4, positionH3);
const moveE4_D1: Move = move(positionE4, positionD1);
const moveE4_B3: Move = move(positionE4, positionB3);
const moveE4_B5: Move = move(positionE4, positionB5);
const moveE4_D7: Move = move(positionE4, positionD7);
// Impossible moves
const moveE4_C7 : Move = move(positionE4, positionC7);
const moveE4_B2 : Move = move(positionE4, positionB2);
const moveE4_C7: Move = move(positionE4, positionC7);
const moveE4_B2: Move = move(positionE4, positionB2);
export class TestBlackPawnMoves {
chessboard : Chessboard
chessboard: Chessboard
@Setup
beforeEach(){
beforeEach() {
chessboard = createEmptyChessboard();
}
@Test("Pawns can move forward")
testPawnCanMoveForward() {
putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true};
Expect(isPossible.blackPawnMove(chessboard, singleForward)).toBeTruthy();
let singleForward: Move = { from: positionA7, to: positionA6, isValid: true };
Expect(isPossible.blackPawnMove(chessboard, singleForward)).toBeTruthy();
}
@Test("Pawns cannot move backward")
testPawnCannotMoveBackward() {
putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA8, isValid: true};
Expect(isPossible.blackPawnMove(chessboard, singleForward)).not.toBeTruthy();
let singleForward: Move = { from: positionA7, to: positionA8, isValid: true };
Expect(isPossible.blackPawnMove(chessboard, singleForward)).not.toBeTruthy();
}
@Test("When in the initial position, paws can move 2 squares forward")
testPawnInitialMove() {
putPiece(chessboard, positionA7, pieces.blackPawn);
let doubleForward: Move = {from: positionA7, to: positionA5, isValid: true};
Expect(isPossible.blackPawnMove(chessboard, doubleForward)).toBeTruthy();
let doubleForward: Move = { from: positionA7, to: positionA5, isValid: true };
Expect(isPossible.blackPawnMove(chessboard, doubleForward)).toBeTruthy();
}
@Test("When a paws has already moved, it cannot move 2 squares forward")
testCannotMoveTwoSquaresIfAlreadyMoved() {
putPiece(chessboard, positionC6, pieces.blackPawn);
let doubleForward: Move = {from: positionC6, to: positionC4, isValid: true}
Expect(isPossible.blackPawnMove(chessboard, doubleForward)).not.toBeTruthy();
let doubleForward: Move = { from: positionC6, to: positionC4, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, doubleForward)).not.toBeTruthy();
}
@Test("When in the initial position, pawns cannot move 3 squares forward")
testCannotMoveThreeSquares() {
putPiece(chessboard, positionC6, pieces.blackPawn);
let tripleForward: Move = {from: positionA7, to: positionA4, isValid: true}
let tripleForward: Move = { from: positionA7, to: positionA4, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, tripleForward)).not.toBeTruthy();
}
......@@ -126,14 +126,14 @@ export class TestBlackPawnMoves {
testPawnCannotMoveForwardToFullSquare() {
putPiece(chessboard, positionA6, pieces.whitePawn);
putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true}
let singleForward: Move = { from: positionA7, to: positionA6, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, singleForward)).not.toBeTruthy();
}
@Test("Pawns cannot capture an empty square ")
@Test("Pawns cannot capture an empty square ")
testPawnCannotCaptureEmptySquare() {
putPiece(chessboard, positionA7, pieces.blackPawn);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
let diagonalCapture: Move = { from: positionA7, to: positionB6, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, diagonalCapture)).not.toBeTruthy();
}
......@@ -142,7 +142,7 @@ export class TestBlackPawnMoves {
putPiece(chessboard, positionA7, pieces.blackPawn);
putPiece(chessboard, positionB6, pieces.blackKing);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
let diagonalCapture: Move = { from: positionA7, to: positionB6, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, diagonalCapture)).not.toBeTruthy();
}
......@@ -151,7 +151,73 @@ export class TestBlackPawnMoves {
putPiece(chessboard, positionA7, pieces.blackPawn);
putPiece(chessboard, positionB6, pieces.whiteQueen);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
let diagonalCapture: Move = { from: positionA7, to: positionB6, isValid: true }
Expect(isPossible.blackPawnMove(chessboard, diagonalCapture)).toBeTruthy();
}
@Test("Pawns can capture en passant")
testPawnCanCaptureEnPassant() {
let F4: Position = {
rank: 3,
file: 5
}
putPiece(chessboard, F4, pieces.whitePawn);
let E4: Position = {
rank: 3,
file: 4
}
putPiece(chessboard, E4, pieces.blackPawn);
let E6: Position = {
rank: 5,
file: 4
}
let newMove: Move = {
isValid: true,
from: E6,
to: E4
}
let E5: Position = {
rank: 4,
file: 4
}
let moveF4_E5: Move = {
isValid: true,
from: F4,
to: E5
}
chessboard.historique.push(newMove);
Expect(isPossible.whitePawnMove(chessboard, moveF4_E5)).toBeTruthy();
let B4: Position = {
rank: 3,
file: 1
}
putPiece(chessboard, B4, pieces.blackPawn);
let C4: Position = {
rank: 3,
file: 2
}
putPiece(chessboard, C4, pieces.whitePawn);
let C2: Position = {
rank: 1,
file: 2
}
let newMove2: Move = {
isValid: true,
from: C2,
to: C4
}
let C3: Position = {
rank: 2,
file: 2
}
let moveB4_C3: Move = {
isValid: true,
from: B4,
to: C3
}
chessboard.historique.push(newMove2);
Expect(isPossible.blackPawnMove(chessboard, moveB4_C3)).toBeTruthy();
}
}
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