diff --git a/src/main/ts/move-validation.ts b/src/main/ts/move-validation.ts index 6f0f5ae5bcb11d218a0dca77ee88c94db7f49964..67a1c877632de44de954e6a88fd1d638b35f56e7 100644 --- a/src/main/ts/move-validation.ts +++ b/src/main/ts/move-validation.ts @@ -1,6 +1,6 @@ import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard"; import { Move } from "./movements"; -import { equals, left, right, top, bottom } from "./position"; +import { equals, left, right, top, bottom, position } from "./position"; /** * Checks whether a Black Pawn can perform a given move. @@ -121,34 +121,77 @@ export function queenMove(board: Chessboard, move: Move): boolean { * @param move */ export function empressMove(board: Chessboard, move: Move): boolean { - let destination : Square = squareAtPosition(board, move.to!); - //Validation du mouvement pour se déplacer horizontalement - if (move.from?.rank === move.to?.rank) - { - return isEmpty(board, move.to!); - + + let destination : Square = squareAtPosition(board,move.to!); + let origin : Square = squareAtPosition(board, move.from!); + + // Horizontal move + if (origin.position.rank == destination.position.rank) { + if (origin.position.file < destination.position.file) { + for (let i = origin.position.file +1; i < destination.position.file; i++) { + + let squareVerification : Square = origin; + squareVerification.position = position(i, origin.position.rank); + + if (!isEmpty(board, squareVerification.position)) { + return false; + } + } + } + if (origin.position.file > destination.position.file) { + for (let i = origin.position.file -1; i < destination.position.file; i--) { + + let squareVerification : Square = origin; + squareVerification.position = position(origin.position.file, i); + + if (!isEmpty(board, squareVerification.position)) { + return false; + } + } + } + return (destination.isEmpty || destination.piece!.isWhite); + } + // Vertical move + if (origin.position.file == destination.position.file) { + if (origin.position.rank < destination.position.rank) { + for (let i = origin.position.rank +1; i < destination.position.rank; i++) { + + let squareVerification : Square = origin; + squareVerification.position = position(origin.position.file, i); + + if (!isEmpty(board, squareVerification.position)) { + return false; + } + } + } + if (origin.position.rank > destination.position.rank) { + for (let i = origin.position.rank -1; i < destination.position.rank; i--) { + + let squareVerification : Square = origin; + squareVerification.position = position(origin.position.file, i); + + if (!isEmpty(board, squareVerification.position)) { + return false; + } + } + } + return (destination.isEmpty || destination.piece!.isWhite); } - //Validation du mouvement pour se déplacer verticalement - if (move.from?.file === move.to?.file) - { - return isEmpty(board, move.to!); + // L move + if (equals(move.to!, left(top(top(move.from!)))) + || equals(move.to!, left(bottom(bottom(move.from!)))) + || equals(move.to!, right(top(top(move.from!)))) + || equals(move.to!, right(bottom(bottom(move.from!)))) + || equals(move.to!, top(left(left(move.from!)))) + || equals(move.to!, top(right(right(move.from!)))) + || equals(move.to!, bottom(left(left(move.from!)))) + || equals(move.to!, bottom(right(right(move.from!)))) + ) + { //alors vérifie que "case est vide ou pièce à capturer est blanche" est vrai + return (destination.isEmpty || destination.piece!.isWhite); } - - //Validation du mouvement en forme de L - if (equals(move.to!, left(top(top(move.from!)))) || - equals(move.to!, left(bottom(bottom(move.from!)))) || - equals(move.to!, right(top(top(move.from!)))) || - equals(move.to!, right(bottom(bottom(move.from!)))) || - equals(move.to!, top(left(left(move.from!)))) || - equals(move.to!, top(right(right(move.from!)))) || - equals(move.to!, bottom(left(left(move.from!)))) || - equals(move.to!, bottom(right(right(move.from!)))) - ) - { - return (destination.isEmpty || !destination.piece!.isWhite); - } - return false; +return false; } /** @@ -167,9 +210,12 @@ export function princessMove(board: Chessboard, move: Move): boolean { let destination: Square = squareAtPosition(board, move.to!); let start: Square = squareAtPosition(board, move.from!); + // Diagonal move if (Math.abs(move.from!.rank - move.to!.rank) === Math.abs(move.from!.file - move.to!.file)){ return (destination.isEmpty||destination.piece!.isWhite !== start.piece!.isWhite); } + + // "L" move if (equals(move.to!, left(top(top(move.from!)))) || equals(move.to!, left(bottom(bottom(move.from!)))) || equals(move.to!, right(top(top(move.from!)))) || @@ -198,22 +244,15 @@ export function princessMove(board: Chessboard, move: Move): boolean { export function camelMove(board: Chessboard, move: Move): boolean { let destination : Square = squareAtPosition(board,move.to!); let origin : Square = squareAtPosition(board, move.from!); - + + // "L" move if (equals(move.to!, left(top(top(top(move.from!))))) || equals(move.to!, left(bottom(bottom(bottom(move.from!))))) || equals(move.to!, right(top(top(top(move.from!))))) || equals(move.to!, right(bottom(bottom(bottom(move.from!))))) || equals(move.to!, top(right(right(right(move.from!))))) || equals(move.to!, top(left(left(left(move.from!))))) || equals(move.to!, bottom(right(right(right(move.from!))))) || equals(move.to!, bottom(left(left(left(move.from!))))) ) { - return (destination.isEmpty || !destination.piece!.isWhite); - } - - if (!(origin.piece!.isWhite)) - { - return (destination.isEmpty || destination.piece!.isWhite); - } - else { - return !(destination.isEmpty || destination.piece!.isWhite); + return (destination.isEmpty||destination.piece!.isWhite !== origin.piece!.isWhite); } return false; } diff --git a/src/test/ts/camel-move-validation.spec.ts b/src/test/ts/camel-move-validation.spec.ts index 1318ee97711bce3ec5e6a102f321ea1ccd0409b5..d54b8810ca44d009819e604dfec47305c110374e 100644 --- a/src/test/ts/camel-move-validation.spec.ts +++ b/src/test/ts/camel-move-validation.spec.ts @@ -57,13 +57,13 @@ const positionH4 : Position = position(7, 3) // H4 const positionH5 : Position = position(7, 4) // H5 const positionH7 : Position = position(7, 6) // H7 -// Tree square horizontally and one square vertically moves +// Tree square horizontally and one square vertically const moveE4_B3 : Move = move(positionE4, positionB3); const moveE4_B5 : Move = move(positionE4, positionB5); const moveE4_H3 : Move = move(positionE4, positionH3); const moveE4_H5 : Move = move(positionE4, positionH5); -// Tree square vertically and one square horizontally moves +// Tree square vertically and one square horizontally const moveE4_F1 : Move = move(positionE4, positionF1); const moveE4_F7 : Move = move(positionE4, positionF7); const moveE4_D1 : Move = move(positionE4, positionD1); @@ -84,82 +84,81 @@ export class TestCamelMoves { @Setup beforeEach() { // Initialize an empty chessboard - // Place a white Camel on E4 chessboard = createEmptyChessboard (); + // Place a white Camel on E4 putPiece(chessboard, positionE4, pieces.whiteCamel) } @Test("A Camel can move three squares horizontally and one square vertically") testCanMoveThreeHorizontalAndOneVertical() { // Check the following moves are possible: moveE4_H3, moveE4_H5, moveE4_B3, moveE4_B5 - Expect(isPossible.camelMove(chessboard, moveE4_B3)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_B5)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_H3)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_H5)).toBe(true); + Expect(isPossible.camelMove(chessboard, moveE4_B3)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_B5)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_H3)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_H5)).toBeTruthy(); } @Test("A Camel can move three squares vertically and one square horizontally") testCanMoveThreeVerticalAndOneHorizontal() { // Check the following moves are possible: moveE4_F1, moveE4_F7, moveE4_D1, moveE4_D7 - Expect(isPossible.camelMove(chessboard, moveE4_F1)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_F7)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_D1)).toBe(true); - Expect(isPossible.camelMove(chessboard, moveE4_D7)).toBe(true); + Expect(isPossible.camelMove(chessboard, moveE4_F1)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_F7)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_D1)).toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_D7)).toBeTruthy(); } @Test("A Camel can leap other pieces") testCanLeapOtherPieces() { // Place a black Pawn on E1 // Place a white Pawn on D2 - // Check the move moveE4_D1 is possible putPiece(chessboard, positionE1, pieces.blackPawn); putPiece(chessboard, positionD2, pieces.whitePawn); - let mvtLeap: Move = {from: positionE4, to: positionD1, isValid: true} - Expect(isPossible.camelMove(chessboard, mvtLeap)).toBe(true); - + let mvtCanLeap: Move = {from: positionE4, to: positionD1, isValid: true} + // Check the move moveE4_D1 is possible + Expect(isPossible.camelMove(chessboard, mvtCanLeap)).toBeTruthy(); } @Test("A Camel cannot move diagonally") testCannotMoveDiagonally() { // Check the following moves are impossible: moveE4_D5, moveE4_H1, moevE4_G6 - Expect(isPossible.camelMove(chessboard, moveE4_D5)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_G6)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_H1)).toBe(false); + Expect(isPossible.camelMove(chessboard, moveE4_D5)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_G6)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_H1)).not.toBeTruthy(); } @Test("A Camel cannot move horizontally") testCannotMoveHorizontally() { // Check the following moves are impossible : moveE4_D4, moveE4_B4, moveE4_G4 - Expect(isPossible.camelMove(chessboard, moveE4_D4)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_B4)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_G4)).toBe(false); + Expect(isPossible.camelMove(chessboard, moveE4_D4)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_B4)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_G4)).not.toBeTruthy(); } @Test("A Camel cannot move vertically") testCannotMoveVertically() { // Check the following moves are impossible : moveE4_E1, moveE4_E3, moveE4_E6 - Expect(isPossible.camelMove(chessboard, moveE4_E1)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_E3)).toBe(false); - Expect(isPossible.camelMove(chessboard, moveE4_E6)).toBe(false); + Expect(isPossible.camelMove(chessboard, moveE4_E1)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_E3)).not.toBeTruthy(); + Expect(isPossible.camelMove(chessboard, moveE4_E6)).not.toBeTruthy(); } @Test("A Camel can capture a piece from another color") testCanCaptureAnotherColor() { // Place a black Pawn on D1 - // Check the move moveE4_D1 is possible putPiece(chessboard, positionD1, pieces.blackPawn); let mvtCapture: Move = {from: positionE4, to: positionD1, isValid: true} - Expect(isPossible.camelMove(chessboard, mvtCapture)).toBe(true); + // Check the move moveE4_D1 is possible + Expect(isPossible.camelMove(chessboard, mvtCapture)).toBeTruthy(); } @Test("A Camel cannot capture a piece from the same color") testCannotCaptureSameColor() { // Place a white Pawn on F1 - // Check the move moveE4_F1 is impossible putPiece(chessboard, positionF1, pieces.whitePawn); let mvtCapture: Move = {from: positionE4, to: positionF1, isValid: true} - Expect(isPossible.camelMove(chessboard, mvtCapture)).toBe(false); + // Check the move moveE4_F1 is impossible + Expect(isPossible.camelMove(chessboard, mvtCapture)).not.toBeTruthy(); } } diff --git a/src/test/ts/empress-move-validation.spec.ts b/src/test/ts/empress-move-validation.spec.ts index fa5d42ee39940940b90aa969adeb44be066394e1..650c982d49b26acafcf3e9b8539174d9c130c8d1 100644 --- a/src/test/ts/empress-move-validation.spec.ts +++ b/src/test/ts/empress-move-validation.spec.ts @@ -4,7 +4,6 @@ import * as pieces from '../../main/ts/piece' import { Chessboard, createEmptyChessboard, putPiece, pieceAtPosition } 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 @@ -41,6 +40,7 @@ const positionE8 : Position = position(4, 7) // E8 const positionF1 : Position = position(5, 0) // F1 const positionF2 : Position = position(5, 1) // F2 +const positionF4 : Position = position(5, 3) // F4 const positionF6 : Position = position(5, 5) // F6 const positionF7 : Position = position(5, 6) // F7 @@ -82,84 +82,87 @@ const moveE4_A8 : Move = move(positionE4, positionA8); export class TestEmpressMoves { @Setup beforeEach() { + // Initialize an empty chessboard chessboard = createEmptyChessboard (); - //La variable "positionE4" a été crée au début du module pour simplifier le code des tests - //Place une Impératrice sur la case E4 de l'échiquier + // Place a black Empress on E4 putPiece(chessboard, positionE4, pieces.blackEmpress) - } @Test("An Empress can move horizontally") testCanMoveHorizontally() { - // Check it can move to square H4, A4 + // Check the following moves are possible : moveE4_H4, moveE4_A4 Expect(isPossible.empressMove(chessboard, moveE4_H4)).toBeTruthy(); Expect(isPossible.empressMove(chessboard, moveE4_A4)).toBeTruthy(); } @Test("A Empress can move vertically") - testCanMoveVertically() { - // Check it can move to square E8, E1 + testCanMoveVertically() { + // Check the following moves are possible : moveE4_E8, moveE4_E1 Expect(isPossible.empressMove(chessboard, moveE4_E8)).toBeTruthy(); - Expect(isPossible.empressMove(chessboard, moveE4_E1)).toBeTruthy(); - + Expect(isPossible.empressMove(chessboard, moveE4_E1)).toBeTruthy(); } @Test("An Empress can move two squares horizontally and one square vertically") testCanMoveTwoHorizontalAndOneVertical() { - // Check it can move to square C3, C5, G3, G5 - Expect(isPossible.empressMove(chessboard, moveE4_C3)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_C5)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_G3)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_G5)).toBe(true); - } + // Check the following moves are possible : moveE4_C3, moveE4_C5, moveE4_G3, moveE4_G5 + Expect(isPossible.empressMove(chessboard, moveE4_C3)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_C5)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_G3)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_G5)).toBeTruthy(); + } @Test("An Empress can move two squares vertically and one square horizontally") - testCanMoveTwoVerticalAndOneHorizontal() { - //Check it can move to square F2, F6, D2, D6 - Expect(isPossible.empressMove(chessboard, moveE4_F2)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_F6)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_D2)).toBe(true); - Expect(isPossible.empressMove(chessboard, moveE4_D6)).toBe(true); + testCanMoveTwoVerticalAndOneHorizontal() {//done + //Check the following moves are possible : moveE4_F2, moveE4_F6, moveE4_D2, moveE4_D6 + Expect(isPossible.empressMove(chessboard, moveE4_D2)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_D6)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_F2)).toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_F6)).toBeTruthy(); } @Test("A Empress cannot move diagonally") - testCannotMoveDiagonally() { - //Check it cannot move to square A8, B1, H1, H7 - Expect(isPossible.empressMove(chessboard, moveE4_A8)).toBe(false); - Expect(isPossible.empressMove(chessboard, moveE4_B1)).toBe(false); - Expect(isPossible.empressMove(chessboard, moveE4_H1)).toBe(false); - Expect(isPossible.empressMove(chessboard, moveE4_H7)).toBe(false); + testCannotMoveDiagonally() {//done + //Check the following moves are impossible : moveE4_A8, moveE4_B1, moveE4_H1, moveE4_H7 + Expect(isPossible.empressMove(chessboard, moveE4_A8)).not.toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_B1)).not.toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_H1)).not.toBeTruthy(); + Expect(isPossible.empressMove(chessboard, moveE4_H7)).not.toBeTruthy(); } @Test("A Empress can capture a piece from different color") - testCanCaptureDifferentColor() { - //put a white pawn in H4 + testCanCaptureDifferentColor() { + // Place a white pawn in H4 + putPiece(chessboard, positionH4, pieces.whitePawn); + let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true} // Check the move moveE4_H4 is possible + Expect(isPossible.empressMove(chessboard, mvtCapture)).toBeTruthy(); } - + @Test("A Empress cannot capture a piece from the same color") - testCannotCaptureSameColor() { - // Put a black pawn in H4 - // Check the move moveE4_H4 is impossibl + testCannotCaptureSameColor() { + // Place a black pawn in H4 putPiece(chessboard, positionH4, pieces.blackPawn); let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true} - Expect(isPossible.empressMove(chessboard, mvtCapture)).toBe(false); - + // Check the move moveE4_H4 is impossible + Expect(isPossible.empressMove(chessboard, mvtCapture)).not.toBeTruthy(); } @Test("A Empress cannot leap other pieces, when moving horizontally") - testCannotLeapHorizontally() { - // TODO: + testCannotLeapHorizontally() { // Place a black Pawn on F4 + putPiece(chessboard, positionF4, pieces.blackPawn); + let mvtCannotLeap: Move = {from: positionE4, to: positionH4, isValid: true} // Check the move moveE4_H4 is impossible + Expect(isPossible.empressMove(chessboard, mvtCannotLeap)).not.toBeTruthy(); } @Test("A Empress cannot leap other pieces, when moving vertically") - testCannotLeapvertically() { + testCannotLeapvertically() { //ECHEC // Place a black Pawn on E3 + putPiece(chessboard, positionE3, pieces.blackPawn); + let mvtCannotLeap: Move = {from: positionE4, to: positionE1, isValid: true} // Check the move moveE4_E1 is impossible + Expect(isPossible.empressMove(chessboard, mvtCannotLeap)).not.toBeTruthy(); } -} - - \ No newline at end of file +} \ No newline at end of file diff --git a/src/test/ts/king-move-validation.spec.ts b/src/test/ts/king-move-validation.spec.ts index b2f9096180296e2704caf7bddf411f7e31dcd6a6..2049bf08b4c08b00229370a5a139ff48a8e1a947 100644 --- a/src/test/ts/king-move-validation.spec.ts +++ b/src/test/ts/king-move-validation.spec.ts @@ -91,52 +91,55 @@ export class TestKingMoves { @Setup beforeEach() { + // Initialize an empty chessboard chessboard = createEmptyChessboard (); - //Place un Roi noir sur la case E4 de l'échiquier + // Place a black King on E4 putPiece(chessboard, positionE4, pieces.blackKing) } @Test("A King can move 1 square in all directions") testCanMoveOneSquare() { //Check it can move to square D4, F4, E3, E5, D3, D5, F3, F5 - Expect(isPossible.kingMove(chessboard, moveE4_D4)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_F4)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_E3)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_E5)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_D3)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_D5)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_F3)).toBe(true); - Expect(isPossible.kingMove(chessboard, moveE4_F5)).toBe(true); + Expect(isPossible.kingMove(chessboard, moveE4_D4)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_F4)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_E3)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_E5)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_D3)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_D5)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_F3)).toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_F5)).toBeTruthy(); } @Test("A King cannot move more than 1 square") testCannotMoveMoreThanOneSquare() { - // Check it cannot move to squares C2, C3, C4, C6, E2, E6, G2, G4, and G6 - Expect(isPossible.kingMove(chessboard, moveE4_C2)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_C3)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_C4)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_C6)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_E2)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_E6)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_G2)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_G4)).toBe(false); - Expect(isPossible.kingMove(chessboard, moveE4_G6)).toBe(false); + // Check the following moves are impossible : moveE4_C2, moveE4_C3, moveE4_C4, + //moveE4_C6, moveE4_E2, moveE4_E6, moveE4_G2, moveE4_G4, moveE4_ G6 + Expect(isPossible.kingMove(chessboard, moveE4_C2)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_C3)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_C4)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_C6)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_E2)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_E6)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_G2)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_G4)).not.toBeTruthy(); + Expect(isPossible.kingMove(chessboard, moveE4_G6)).not.toBeTruthy(); } @Test("A King cannot capure pieces from the same color") testCannotCaptureSameColor() { - // Check the King cannot move to E5. + // Put a black pawn on E5 putPiece(chessboard, positionE5, pieces.blackPawn); let mvtCapture: Move = {from: positionE4, to: positionE5, isValid: true} + // Check the following move is impossible : moveE4_E5 Expect(isPossible.kingMove(chessboard, mvtCapture)).toBe(false); } @Test("A King can capure pieces from a different color") testCanCaptureSameColor() { - // Check the King can move to E5. + // Put a white pawn on E5. putPiece(chessboard, positionE5, pieces.whitePawn); let mvtCapture: Move = {from: positionE4, to: positionE5, isValid: true} - Expect(isPossible.kingMove(chessboard, mvtCapture)).toBe(true); - + // Check the following move is possible : moveE4_E5 + Expect(isPossible.kingMove(chessboard, mvtCapture)).toBe(true); } } diff --git a/src/test/ts/princess-move-validation.spec.ts b/src/test/ts/princess-move-validation.spec.ts index e1a2210c3d064f1b7670699192b21469b9901e94..d1abe9a77b457da64ef5f937bb4f545e5f8f9f76 100644 --- a/src/test/ts/princess-move-validation.spec.ts +++ b/src/test/ts/princess-move-validation.spec.ts @@ -66,13 +66,6 @@ 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); - -// Vertical moves -const moveE4_E1 : Move = move(positionE4, positionE1); -const moveE4_E8 : Move = move(positionE4, positionE8); // Diagonal moves const moveE4_A8 : Move = move(positionE4, positionA8); @@ -83,24 +76,23 @@ const moveE4_H1 : Move = move(positionE4, positionH1); // Impossible moves const moveE4_H2 : Move = move(positionE4, positionH2); const moveE4_C7 : Move = move(positionE4, positionC7); +const moveE4_E1 : Move = move(positionE4, positionE1); +const moveE4_E8 : Move = move(positionE4, positionE8); +const moveE4_H4 : Move = move(positionE4, positionH4); +const moveE4_A4 : Move = move(positionE4, positionA4); -//Two horizontal and one vertical +// Two squares horizontally and one square vertically const moveE4_C3 : Move = move(positionE4, positionC3); const moveE4_C5 : Move = move(positionE4, positionC5); +const moveE4_G3 : Move = move(positionE4, positionG3); +const moveE4_G5 : Move = move(positionE4, positionG5); -//Two vertical and one horizontal +// Two squares vertically and one square horizontally +const moveE4_D2 : Move = move(positionE4, positionD2); +const moveE4_D6 : Move = move(positionE4, positionD6); const moveE4_F2 : Move = move(positionE4, positionF2); const moveE4_F6 : Move = move(positionE4, positionF6); -//One square arround E4 -const moveE4_E5 : Move = move(positionE4, positionE5); -const moveE4_D5 : Move = move(positionE4, positionD5); -const moveE4_D4 : Move = move(positionE4, positionD4); -const moveE4_D3 : Move = move(positionE4, positionD3); -const moveE4_E3 : Move = move(positionE4, positionE3); -const moveE4_F3 : Move = move(positionE4, positionF3); -const moveE4_F4 : Move = move(positionE4, positionF4); -const moveE4_F5 : Move = move(positionE4, positionF5); export class TestPrincessMoves { @Setup @@ -114,73 +106,79 @@ export class TestPrincessMoves { @Test("A Princess can move diagonally") testCanMoveDiagonally() { - // Check the following moves are possible: - Expect(isPossible.princessMove(chessboard, moveE4_A8)).toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_B1)).toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_H7)).toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_H1)).toBeTruthy() - // moveE4_A8, moveE4_B1, moveE4_H7, moveE4_H1 + // Check the following moves are possible: moveE4_A8, moveE4_B1, moveE4_H7, moveE4_H1 + Expect(isPossible.princessMove(chessboard, moveE4_A8)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_B1)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_H7)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_H1)).toBeTruthy(); } @Test("A Princess can move two squares horizontally and one square vertically") testCanMoveTwoHorizontalAndOneVertical() { - Expect(isPossible.princessMove(chessboard, moveE4_C3)).toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_C5)).toBeTruthy() - // moveE4_C3, moveE4_C5, - + // Check the following moves are possible : moveE4_C3, moveE4_C5, moveE4_G3, moveE4_G5 + Expect(isPossible.princessMove(chessboard, moveE4_C3)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_C5)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_G3)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_G5)).toBeTruthy(); } @Test("A Princess can move two squares vertically and one square horizontally") testCanMoveTwoVerticalAndOneHorizontal() { - Expect(isPossible.princessMove(chessboard, moveE4_F2)).toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_F6)).toBeTruthy() - // moveE4_F2, moveE4_F6, + // Check the following moves are possible : moveE4_D2, mveE4_D6 moveE4_F2, moveE4_F6 + Expect(isPossible.princessMove(chessboard, moveE4_D2)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_D6)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_F2)).toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_F6)).toBeTruthy(); } @Test("A Princess cannot move horizontally") testCannotMoveHorizontally() { // Check the following moves are impossible: moveE4_H4, moveE4_A4 - Expect(isPossible.princessMove(chessboard, moveE4_H4)).not.toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_A4)).not.toBeTruthy() + Expect(isPossible.princessMove(chessboard, moveE4_H4)).not.toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_A4)).not.toBeTruthy(); } @Test("A Princess cannot move vertically") testCannotMoveVertically() { // Check the following moves are impossible: moveE4_E1, moveE4_E8 - Expect(isPossible.princessMove(chessboard, moveE4_E1)).not.toBeTruthy() - Expect(isPossible.princessMove(chessboard, moveE4_E8)).not.toBeTruthy() + Expect(isPossible.princessMove(chessboard, moveE4_E1)).not.toBeTruthy(); + Expect(isPossible.princessMove(chessboard, moveE4_E8)).not.toBeTruthy(); } @Test("A Princess can capture a piece from another color") testCanCaptureDifferentColor() { // Place a white Pawn on A8 - putPiece(chessboard, positionA8, pieces.whitePawn) + putPiece(chessboard, positionA8, pieces.whitePawn); + let mvtCapture: Move = {from: positionE4, to: positionA8, isValid: true} // Check the move moveE4_A8 is possible - Expect(isPossible.princessMove(chessboard, moveE4_A8)).toBeTruthy() + Expect(isPossible.princessMove(chessboard, mvtCapture)).toBeTruthy(); } @Test("A Princess cannot capture a piece from the same color") testCannotCaptureSameColor() { // Place a black Pawn on A8 - putPiece(chessboard, positionA8, pieces.blackPawn) + putPiece(chessboard, positionA8, pieces.blackPawn); + let mvtCapture: Move = {from: positionE4, to: positionA8, isValid: true} // Check the move moveE4_A8 is impossible - Expect(isPossible.princessMove(chessboard, moveE4_A8)).not.toBeTruthy() + Expect(isPossible.princessMove(chessboard, mvtCapture)).not.toBeTruthy(); } @Test("A Princess cannot leap other pieces") testCannotLeapDiagonally() { // Place a white Pawn on C6 - putPiece(chessboard, positionC6, pieces.whitePawn) + putPiece(chessboard, positionC6, pieces.whitePawn); + let mvtCannotLeap: Move = {from: positionE4, to: positionA8, isValid: true} // Check the move moveE4_A8 is impossible - Expect(isPossible.princessMove(chessboard, moveE4_A8)).not.toBeTruthy() + Expect(isPossible.princessMove(chessboard, mvtCannotLeap)).not.toBeTruthy(); } @Test("A Princess can leap other pieces when moving in L ") testCanLeapOtherPiecesWhenMovingInL() { // Place a white Pawn on E5 - putPiece(chessboard, positionE5, pieces.whitePawn) + putPiece(chessboard, positionE5, pieces.whitePawn); + let mvtCanLeap: Move = {from: positionE4, to: positionF6, isValid : true} // Check the move moveE4_F6 is impossible - Expect(isPossible.princessMove(chessboard, moveE4_F6)).toBeTruthy() + Expect(isPossible.princessMove(chessboard, mvtCanLeap)).toBeTruthy(); } } diff --git a/src/test/ts/queen-move-validation.spec.ts b/src/test/ts/queen-move-validation.spec.ts index 06f45467ea8c32a6a5787b7ec47c017ab02ebdd9..fe363382d41fe8f7b26ec9f04ad3ed2bdbaac337 100644 --- a/src/test/ts/queen-move-validation.spec.ts +++ b/src/test/ts/queen-move-validation.spec.ts @@ -82,9 +82,12 @@ const moveE4_H7 : Move = move(positionE4, positionH7); const moveE4_H1 : Move = move(positionE4, positionH1); // Impossible moves +const moveE4_B2 : Move = move(positionE4, positionB2); const moveE4_C3 : Move = move(positionE4, positionC3); +const moveE4_C7 : Move = move(positionE4, positionC7); const moveE4_F6 : Move = move(positionE4, positionF6); + export class TestQueenMoves { @Setup beforeEach() { @@ -96,59 +99,62 @@ export class TestQueenMoves { @Test("A Queen can move diagonally") testCanMoveDiagonally() { - // Check the following moves are possible: - Expect(isPossible.queenMove(chessboard, moveE4_A8)).toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_B1)).toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_H7)).toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_H1)).toBeTruthy() - // moveE4_A8, moveE4_B1, moveE4_H7, moveE4_H1 + // Check the following moves are possible: moveE4_A8, moveE4_B1, moveE4_H7, moveE4_H1 + Expect(isPossible.queenMove(chessboard, moveE4_A8)).toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_B1)).toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_H7)).toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_H1)).toBeTruthy(); } @Test("A Queen can move horizontally") testCanMoveHorizontally() { // Check the following moves are possible: moveE4_H4, moveE4_A4 - Expect(isPossible.queenMove(chessboard, moveE4_H4)).toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_A4)).toBeTruthy() - + Expect(isPossible.queenMove(chessboard, moveE4_H4)).toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_A4)).toBeTruthy(); } @Test("A Queen can move vertically") testCanMoveVertically() { // Check the following moves are possible: moveE4_E1, moveE4_E8 - Expect(isPossible.queenMove(chessboard, moveE4_E1)).toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_E8)).toBeTruthy() + Expect(isPossible.queenMove(chessboard, moveE4_E1)).toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_E8)).toBeTruthy(); } @Test("A Queen can only move horizontally, vertically, and diagonally") testForbiddenMoves() { // Check the following moves are impossible: moveE4_C7, moveE4_B2 - Expect(isPossible.queenMove(chessboard, moveE4_C7)).not.toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_B2)).not.toBeTruthy() + Expect(isPossible.queenMove(chessboard, moveE4_C7)).not.toBeTruthy(); + Expect(isPossible.queenMove(chessboard, moveE4_B2)).not.toBeTruthy(); } @Test("A Queen cannot leap other pieces") testCannotLeap() { // Place a white Pawn on C6 and a black Pawn on F4 - putPiece(chessboard, positionC6, pieces.whitePawn) - putPiece(chessboard, positionF4, pieces.blackPawn) + putPiece(chessboard, positionC6, pieces.blackPawn); + putPiece(chessboard, positionF4, pieces.blackPawn); + let mvtCannotLeap1: Move = {from: positionE4, to: positionA8, isValid : true} + let mvtCannotLeap2: Move = {from: positionE4, to: positionH4, isValid : true} // Check the moves moveE4_A8 and moveE4_H4 are impossible - Expect(isPossible.queenMove(chessboard, moveE4_A8)).not.toBeTruthy() - Expect(isPossible.queenMove(chessboard, moveE4_H4)).not.toBeTruthy() + Expect(isPossible.queenMove(chessboard, mvtCannotLeap1)).not.toBeTruthy(); + Expect(isPossible.queenMove(chessboard, mvtCannotLeap2)).not.toBeTruthy(); } @Test("A Queen cannot capure pieces from the same color") testCannotCaptureSameColor() { // Place a white Pawn on H4 - putPiece(chessboard, positionH4, pieces.whitePawn) - // Check the move moveE4_H4 is impossible - Expect(isPossible.queenMove(chessboard, moveE4_H4)).not.toBeTruthy() + putPiece(chessboard, positionH4, pieces.whitePawn); + let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true} + // Check the move moveE4_H4 is impossible * + Expect(isPossible.queenMove(chessboard, mvtCapture)).not.toBeTruthy(); } @Test("A Queen can capure pieces from a different color") testCanCaptureDifferentColor() { // Place a black Pawn on H4 - putPiece(chessboard, positionH4, pieces.blackPawn) + putPiece(chessboard, positionH4, pieces.whitePawn); + let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true} // Check the move moveE4_H4 is possible - Expect(isPossible.queenMove(chessboard, moveE4_H4)).toBeTruthy() + Expect(isPossible.queenMove(chessboard, mvtCapture)).toBeTruthy(); + } }