Commit f7e077df authored by Saad MAHBOUBI's avatar Saad MAHBOUBI 💬
Browse files

fontion kingMove : le Rois ne peut toujours pas manger,test de la fonction king move presque fini

parent f233fef0
......@@ -16,7 +16,8 @@ const positionB1 : Position = position(1, 0) // B1
const positionB2 : Position = position(1, 1) // B2
const positionB6 : Position = position(1, 5) // B6
const positionC3 : Position = position(2, 1) // C3
const positionC2 : Position = position(2, 1) // C2
const positionC3 : Position = position(2, 2) // C3
const positionC4 : Position = position(2, 3) // C4
const positionC5 : Position = position(2, 4) // C5
const positionC6 : Position = position(2, 5) // C6
......@@ -31,14 +32,19 @@ const positionD6 : Position = position(3, 5) // D6
const positionE1 : Position = position(4, 0) // E1
const positionE3 : Position = position(4, 2) // E3
const positionE4 : Position = position(4, 3) // E4
const positionE5 : Position = position(4, 4) // E5
const positionE8 : Position = position(4, 7) // E8
const positionF2 : Position = position(5, 1) // F2
const positionF3 : Position = position(5, 2) // F3
const positionF4 : Position = position(5, 3) // F4
const positionF5 : Position = position(5, 4) // F5
const positionF6 : Position = position(5, 5) // F6
const positionG3 : Position = position(6, 2) // G3
const positionG4 : Position = position(6, 3) // G4
const positionG5 : Position = position(6, 4) // G5
const positionG6 : Position = position(6, 5) // G6
const positionH1 : Position = position(7, 0) // H1
const positionH4 : Position = position(7, 3) // H4
......@@ -199,8 +205,7 @@ describe("Test kingMove()", () => {
beforeEach( () => {
// TODO:
chessboard = createEmptyChessboard();
// Place a black King on E4
putPiece(chessboard, positionE4, pieces.blackKing);
putPiece(chessboard, positionE4, pieces.blackKing); // Black King at E4
});
it("A King can move 1 square in all directions", () => {
......@@ -208,20 +213,56 @@ describe("Test kingMove()", () => {
let simpleBackwardMove: Move = {from: positionE4, to: positionE3, isValid: true}
expect(isPossible.kingMove(chessboard, simpleBackwardMove)).toBeTruthy();
let simpleForwardMove: Move = {from: positionE4, to: positionF4, isValid: true}
expect(isPossible.kingMove(chessboard, simpleBackwardMove)).toBeTruthy();
let simpleForwardMove: Move = {from: positionE4, to: positionE5, isValid: true}
expect(isPossible.kingMove(chessboard, simpleForwardMove)).toBeTruthy();
let simpleLeftMove: Move = {from: positionE4, to: positionD4, isValid: true}
expect(isPossible.kingMove(chessboard, simpleLeftMove)).toBeTruthy();
let simpleRightMove: Move = {from: positionE4, to: positionF4, isValid: true}
expect(isPossible.kingMove(chessboard, simpleRightMove)).toBeTruthy();
let topLeftMove: Move = {from: positionE4, to: positionD5, isValid: true}
expect(isPossible.kingMove(chessboard, topLeftMove)).toBeTruthy();
let topRightMove: Move = {from: positionE4, to: positionF5, isValid: true}
expect(isPossible.kingMove(chessboard, topRightMove)).toBeTruthy();
let bottomLeftMove: Move = {from: positionE4, to: positionD3, isValid: true}
expect(isPossible.kingMove(chessboard, bottomLeftMove)).toBeTruthy();
let bottomRightMove: Move = {from: positionE4, to: positionF3, isValid: true}
expect(isPossible.kingMove(chessboard, bottomRightMove)).toBeTruthy();
})
it("A King cannot move more than 1 square", () => {
// Check it cannot move to squares C2, C3, C4, C6, D4, and F4
// Check it cannot move to squares C2, C3, C4, C6, G4, and G6
let impossibleMoveToC2: Move = {from: positionE4, to: positionC2, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToC2)).toBeFalsy();
let impossibleMoveToC3: Move = {from: positionE4, to: positionC3, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToC3)).toBeFalsy();
let impossibleMoveToC4: Move = {from: positionE4, to: positionC4, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToC4)).toBeFalsy();
let impossibleMoveToC6: Move = {from: positionE4, to: positionC6, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToC6)).toBeFalsy();
let impossibleMoveToG4: Move = {from: positionE4, to: positionG4, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToG4)).toBeFalsy();
let impossibleMoveToG6: Move = {from: positionE4, to: positionG6, isValid: true}
expect(isPossible.kingMove(chessboard, impossibleMoveToG6)).toBeFalsy();
})
it("A King cannot capure pieces from the same color", () => {
// Place a black Pawn on E5
putPiece(chessboard, positionE5, pieces.blackPawn); // Black Pawn at E5
// Check the King cannot move to E5.
let cannotCapture: Move = {from: positionE4, to: positionE5, isValid: true}
expect(isPossible.kingMove(chessboard, cannotCapture)).toBeFalsy();
})
it("A King can capure pieces from a different color", () => {
......
......@@ -109,27 +109,24 @@ export function kingMove(board: Chessboard, move: Move): boolean {
return isEmpty(board, move.to!);
}
let depart: Square = squareAtPosition(board, move.from!);
let start: Square = squareAtPosition(board, move.from!);
let destination: Square = squareAtPosition(board, move.to!);
if (depart.piece!.isWhite == false && equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(top(move.from!))) ){
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || destination.piece!.isWhite)
if (start.piece!.isWhite == false && equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(top(move.from!))) ){
return !(destination.isEmpty || !destination.piece!.isWhite);
}
if (depart.piece!.isWhite == false && equals(move.to!, left(move.from!)) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, top(move.from!)) || equals(move.to!, bottom(move.from!)) ){
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || destination.piece!.isWhite)
if (start.piece!.isWhite == false && equals(move.to!, left(move.from!)) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, top(move.from!)) || equals(move.to!, bottom(move.from!)) ){
return !(destination.isEmpty || !destination.piece!.isWhite);
}
if (depart.piece!.isWhite == true && equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(top(move.from!))) ){
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || !destination.piece!.isWhite)
if (start.piece!.isWhite == true && equals(move.to!, left(bottom(move.from!))) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, right(top(move.from!))) || equals(move.to!, left(top(move.from!))) ){
return !(destination.isEmpty || destination.piece!.isWhite);
}
if (depart.piece!.isWhite == true && equals(move.to!, left(move.from!)) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, top(move.from!)) || equals(move.to!, bottom(move.from!)) ){
let destination: Square = squareAtPosition(board, move.to!);
return !(destination.isEmpty || !destination.piece!.isWhite)
if (start.piece!.isWhite == true && equals(move.to!, left(move.from!)) || equals(move.to!, right(bottom(move.from!))) || equals(move.to!, top(move.from!)) || equals(move.to!, bottom(move.from!)) ){
return !(destination.isEmpty || 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