Commit 3484d4ad authored by Gerson Sunyé's avatar Gerson Sunyé
Browse files

I added several empty testcases

parent 482a5a43
...@@ -115,12 +115,12 @@ Le traitement des déplacements se fait de la façon suivante: ...@@ -115,12 +115,12 @@ Le traitement des déplacements se fait de la façon suivante:
3. La fonction `processMove()` appelle ensuite la fonction `isMovePossible()`, qui fait appel à différentes fonctions de validation spécifiques aux pièces de l'échiquier (une par type de pièce). Le module `move-validation` contient toutes les fonctions de validation de déplacements. 3. La fonction `processMove()` appelle ensuite la fonction `isMovePossible()`, qui fait appel à différentes fonctions de validation spécifiques aux pièces de l'échiquier (une par type de pièce). Le module `move-validation` contient toutes les fonctions de validation de déplacements.
4. Par exemple, lorsqu'il s'agit d'un Pion blanc, la fonction `isMovePossible()` appelle la fonction `whitePawnMove()`, qui retourne `true` si le déplacement est possible ou `false` s'il ce n'est pas le cas. 4. Par exemple, lorsqu'il s'agit d'un Pion blanc, la fonction `isMovePossible()` appelle la fonction `whitePawnMove()`, qui retourne `true` si le déplacement est possible ou `false` si ce n'est pas le cas.
5. Si le mouvement est possible, c'est à dire la fonction `isMovePossible()` retourne `true`, la fonction `processMove()` appelle la fonction `performMove(), qui effectue le déplacement. 5. Si le mouvement est possible, c'est à dire la fonction `isMovePossible()` retourne `true`, la fonction `processMove()` appelle la fonction `performMove(), qui effectue le déplacement.
Vous devez donc parcourir le module `move-validation` et coder les fonctions de validation contenant le commentaire "TODO:". Vous devez donc parcourir le module `move-validation` et implémenter les fonctions de validation contenant le commentaire "TODO:".
### Tests unitaires ### Tests unitaires
......
import * as isValid from '../src/move-validation' import * as isValid from '../src/move-validation'
import * as pieces from '../src/piece' import * as pieces from '../src/piece'
import { Chessboard, createInitialChessboard, createEmptyChessboard, putPiece } from '../src/chessboard'; import { Chessboard, createInitialChessboard, createEmptyChessboard, putPiece } from '../src/chessboard';
import { Position, bottom } from '../src/position'; import { Position, position } from '../src/position';
import { Move, processMove } from '../src/movements'; import { Move, processMove } from '../src/movements';
let chessboard : Chessboard; let chessboard : Chessboard;
let positionA7 : Position = {column: 0, line: 6} // A7 let positionA4 : Position = position(0, 3) // A4
let positionA6 : Position = {column: 0, line: 5} // A6 let positionA5 : Position = position(0, 4) // A5
let positionA5 : Position = {column: 0, line: 4} // A5 let positionA6 : Position = position(0, 5) // A6
let positionA4 : Position = {column: 0, line: 3} // A4 let positionA7 : Position = position(0, 6) // A7
let positionB6 : Position = {column: 1, line: 5} // B6 let positionA8 : Position = position(0, 7) // A8
let positionC6 : Position = {column: 2, line: 5} // C6 let positionB6 : Position = position(1, 5) // B6
let positionC4 : Position = {column: 2, line: 3} // C4 let positionC6 : Position = position(2, 5) // C6
let positionD2 : Position = {column: 3, line: 1} // D2 let positionC4 : Position = position(2, 3) // C4
let positionD3 : Position = {column: 3, line: 2} // D3 let positionD2 : Position = position(3, 1) // D2
let positionD4 : Position = {column: 3, line: 3} // D4 let positionD3 : Position = position(3, 2) // D3
let positionD5 : Position = {column: 3, line: 4} // D5 let positionD4 : Position = position(3, 3) // D4
let positionD5 : Position = position(3, 4) // D5
describe("Test blackPawnMove()", () => { describe("Test blackPawnMove()", () => {
beforeEach( () => { beforeEach( () => {
chessboard = createInitialChessboard(); chessboard = createEmptyChessboard();
}) })
it("Valid Single foreward A7-A6", () => {
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true} it("Pawns can move forward", () => {
putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true};
expect(isValid.blackPawnMove(chessboard, singleForward)).toBeTruthy(); expect(isValid.blackPawnMove(chessboard, singleForward)).toBeTruthy();
}); });
it("Valid Double foreward A7-A5", () => { it("Pawns cannot move backward", () => {
let doubleForward: Move = {from: positionA7, to: positionA5, isValid: true} putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA8, isValid: true};
expect(isValid.blackPawnMove(chessboard, singleForward)).toBeFalsy();
});
it("When in the initial position, paws can move 2 squares forward", () => {
putPiece(chessboard, positionA7, pieces.blackPawn);
let doubleForward: Move = {from: positionA7, to: positionA5, isValid: true};
expect(isValid.blackPawnMove(chessboard, doubleForward)).toBeTruthy(); expect(isValid.blackPawnMove(chessboard, doubleForward)).toBeTruthy();
}); });
it("Invalid Double foreward C6-C4", () => { it("When a paws has already moved, it cannot move 2 squares forward", () => {
processMove(chessboard,"C7-C6"); putPiece(chessboard, positionC6, pieces.blackPawn);
let doubleForward: Move = {from: positionC6, to: positionC4, isValid: true} let doubleForward: Move = {from: positionC6, to: positionC4, isValid: true}
expect(isValid.blackPawnMove(chessboard, doubleForward)).toBeFalsy(); expect(isValid.blackPawnMove(chessboard, doubleForward)).toBeFalsy();
}); });
it("Invalid Triple foreward A7-A4", () => { it("When in the initial position, pawns cannot move 3 squares forward", () => {
putPiece(chessboard, positionC6, pieces.blackPawn);
let tripleForward: Move = {from: positionA7, to: positionA4, isValid: true} let tripleForward: Move = {from: positionA7, to: positionA4, isValid: true}
expect(isValid.blackPawnMove(chessboard, tripleForward)).toBeFalsy(); expect(isValid.blackPawnMove(chessboard, tripleForward)).toBeFalsy();
}); });
it("Invalid Single Forward (face-to-face)", () => { it("When in face of another piece, pawns cannot move foreward", () => {
processMove(chessboard,"A2-A4"); putPiece(chessboard, positionA6, pieces.whitePawn);
processMove(chessboard,"A4-A5"); putPiece(chessboard, positionA7, pieces.blackPawn);
processMove(chessboard,"A5-A6");
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true} let singleForward: Move = {from: positionA7, to: positionA6, isValid: true}
expect(isValid.blackPawnMove(chessboard, singleForward)).toBeFalsy(); expect(isValid.blackPawnMove(chessboard, singleForward)).toBeFalsy();
}) })
it("Invalid Capture Empty Square A7-B6", () => { it("Pawns cannot capture an empty square ", () => {
putPiece(chessboard, positionA7, pieces.blackPawn);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true} let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy(); expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy();
}); });
it("Invalid Capture Same Color A7-B6 ", () => { it("Pawns cannot capture pieces of the same color", () => {
processMove(chessboard,"B7-B6"); 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(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy(); expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy();
}); });
it("Valid Capture A7-B6", () => { it("Pawns can capture pieces of a different color", () => {
processMove(chessboard,"B2-B4"); putPiece(chessboard, positionA7, pieces.blackPawn);
processMove(chessboard,"B4-B5"); putPiece(chessboard, positionB6, pieces.whiteQueen);
processMove(chessboard,"B5-B6");
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true} let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeTruthy(); expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeTruthy();
...@@ -76,53 +89,114 @@ describe("Test whitePawnMove()", () => { ...@@ -76,53 +89,114 @@ describe("Test whitePawnMove()", () => {
chessboard = createEmptyChessboard(); chessboard = createEmptyChessboard();
}) })
it("Invalid face to face forward", () => { it("When there is a piece in front it, pawns cannot move forward", () => {
putPiece(chessboard, 3, 3, pieces.whitePawn); // White Pawn at D4 putPiece(chessboard, positionD4, pieces.whitePawn); // White Pawn at D4
putPiece(chessboard, 3, 4, pieces.blackPawn); // Black Pawn at D5 putPiece(chessboard, positionD5, pieces.blackPawn); // Black Pawn at D5
let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true} let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeFalsy(); expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeFalsy();
}); });
it("Valid simple forward", () => { it("Pawns can move forward", () => {
putPiece(chessboard, 3, 3, pieces.whitePawn); // White Pawn at D4 putPiece(chessboard, positionD4, pieces.whitePawn); // White Pawn at D4
let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true} let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeTruthy(); expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeTruthy();
}); });
it("Valid double forward", () => { it("When in the initial position, pawns can move two squares forward", () => {
putPiece(chessboard, 3, 1, pieces.whitePawn); // White Pawn at D2 putPiece(chessboard, positionD2, pieces.whitePawn); // White Pawn at D2
let doubleFowardMove: Move = {from: positionD2, to: positionD4, isValid: true} let doubleFowardMove: Move = {from: positionD2, to: positionD4, isValid: true}
expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeTruthy(); expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeTruthy();
}); });
it("Invalid double forward", () => { it("When they have already moved, pawns cannot move two squares forward", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3 putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
let doubleFowardMove: Move = {from: positionD3, to: positionD5, isValid: true} let doubleFowardMove: Move = {from: positionD3, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeFalsy(); expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeFalsy();
}); });
it("Invalid capture empty square", () => { it("Pawns cannot capture an empty square", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3 putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true} let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy(); expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy();
}); });
it("Invalid capture same color", () => { it("Pawns cannot capture a piece of the same color", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3 putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, 2, 3, pieces.whitePawn); // White Pawn at C4 putPiece(chessboard, positionC4, pieces.whitePawn); // White Pawn at C4
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true} let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy(); expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy();
}); });
it("Valid capture", () => { it("Pawns can capture pieces of a different color", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3 putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, 2, 3, pieces.blackKing); // Black King at C4 putPiece(chessboard, positionC4, pieces.blackKing); // Black King at C4
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true} let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeTruthy(); expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeTruthy();
}) })
});
describe("Test blackKingMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
});
});
describe("Test blackQueenMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
});
});
describe("Test blackBishopMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
});
});
describe("Test blackKnightMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
});
});
describe("Test blackRoockMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
});
it("A roock can move horizontally", () => {
// TODO
// Place a roock in E4 in the empty board
// Check the roock can move to H4
// Check the roock can mobe to A4
});
it("A roock can move vertically", () => {
// TODO
// Place a roock in E4 in the empty board
// Check the roock can move to E1
// Check the roock can mobe to E8
});
it("A roock cannot move diagonally", () => {
// TODO
// Place a roock in E4 in the empty board
// Check the roock cannot move to A8
// Check the roock cannot mobe to H7
});
});
}); describe("Test whiteKingMove()", () => {});
\ No newline at end of file describe("Test whiteQueenMove()", () => {});
describe("Test whiteBishopMove()", () => {});
describe("Test whiteKnightMove()", () => {});
describe("Test whiteRoockMove()", () => {});
\ No newline at end of file
...@@ -84,33 +84,33 @@ export function createInitialChessboard(): Chessboard { ...@@ -84,33 +84,33 @@ export function createInitialChessboard(): Chessboard {
let position: Position; let position: Position;
let square : Square; let square : Square;
for(let col: number = 0; col < 8; col++) { for(let col: number = 0; col < 8; col++) {
putPiece(chessboard, col, 1, pieces.whitePawn); putPieceAtCoordinate(chessboard, col, 1, pieces.whitePawn);
putPiece(chessboard, col, 6, pieces.blackPawn); putPieceAtCoordinate(chessboard, col, 6, pieces.blackPawn);
} }
// Kings and Queens // Kings and Queens
putPiece(chessboard, 3, 0, pieces.whiteKing); putPieceAtCoordinate(chessboard, 3, 0, pieces.whiteKing);
putPiece(chessboard, 3, 7, pieces.blackKing); putPieceAtCoordinate(chessboard, 3, 7, pieces.blackKing);
putPiece(chessboard, 4, 0, pieces.whiteQueen); putPieceAtCoordinate(chessboard, 4, 0, pieces.whiteQueen);
putPiece(chessboard, 4, 7, pieces.blackQueen); putPieceAtCoordinate(chessboard, 4, 7, pieces.blackQueen);
// Bishops // Bishops
putPiece(chessboard, 2, 0, pieces.whiteBishop); putPieceAtCoordinate(chessboard, 2, 0, pieces.whiteBishop);
putPiece(chessboard, 2, 7, pieces.blackBishop); putPieceAtCoordinate(chessboard, 2, 7, pieces.blackBishop);
putPiece(chessboard, 5, 0, pieces.whiteBishop); putPieceAtCoordinate(chessboard, 5, 0, pieces.whiteBishop);
putPiece(chessboard, 5, 7, pieces.blackBishop); putPieceAtCoordinate(chessboard, 5, 7, pieces.blackBishop);
// Knights // Knights
putPiece(chessboard, 1, 0, pieces.whiteKnight); putPieceAtCoordinate(chessboard, 1, 0, pieces.whiteKnight);
putPiece(chessboard, 1, 7, pieces.blackKnight); putPieceAtCoordinate(chessboard, 1, 7, pieces.blackKnight);
putPiece(chessboard, 6, 0, pieces.whiteKnight); putPieceAtCoordinate(chessboard, 6, 0, pieces.whiteKnight);
putPiece(chessboard, 6, 7, pieces.blackKnight); putPieceAtCoordinate(chessboard, 6, 7, pieces.blackKnight);
// Roocks // Roocks
putPiece(chessboard, 0, 0, pieces.whiteRoock); putPieceAtCoordinate(chessboard, 0, 0, pieces.whiteRoock);
putPiece(chessboard, 0, 7, pieces.blackRoock); putPieceAtCoordinate(chessboard, 0, 7, pieces.blackRoock);
putPiece(chessboard, 7, 0, pieces.whiteRoock); putPieceAtCoordinate(chessboard, 7, 0, pieces.whiteRoock);
putPiece(chessboard, 7, 7, pieces.blackRoock); putPieceAtCoordinate(chessboard, 7, 7, pieces.blackRoock);
return chessboard; return chessboard;
} }
...@@ -144,9 +144,13 @@ function createChessboard(): Chessboard { ...@@ -144,9 +144,13 @@ function createChessboard(): Chessboard {
} }
export function putPiece(chessboard: Chessboard, column : number, line: number, piece : Piece) { function putPieceAtCoordinate(chessboard: Chessboard, column : number, line: number, piece : Piece) {
let board : Array<Array<Square>> = chessboard.board;
let position : Position = {line : line, column : column}; let position : Position = {line : line, column : column};
let square : Square = {position : position, isEmpty : false, piece : piece}; return putPiece(chessboard, position, piece);
board[column][line] = square;
} }
export function putPiece(chessboard: Chessboard, position: Position, piece : Piece) {
let board : Array<Array<Square>> = chessboard.board;
let square : Square = {position : position, isEmpty : false, piece : piece};
board[position.column][position.line] = square;
}
\ No newline at end of file
...@@ -12,7 +12,7 @@ export interface Move { ...@@ -12,7 +12,7 @@ export interface Move {
to? : Position; to? : Position;
} }
export function processMove(chessboard:Chessboard, movementString: string): Chessboard { export function processMove(chessboard:Chessboard, movementString: string): boolean {
let move : Move = parseMoveString(movementString); let move : Move = parseMoveString(movementString);
//console.log("Move: " + move.from.column + move.from.line + move.to.column + move.to.line); //console.log("Move: " + move.from.column + move.from.line + move.to.column + move.to.line);
...@@ -20,8 +20,9 @@ export function processMove(chessboard:Chessboard, movementString: string): Ches ...@@ -20,8 +20,9 @@ export function processMove(chessboard:Chessboard, movementString: string): Ches
performMove(chessboard, move); performMove(chessboard, move);
} else { } else {
console.log("Invalid movement !"); console.log("Invalid movement !");
return false;
} }
return chessboard; return true;
} }
export function parseMoveString(movementString: string): Move { export function parseMoveString(movementString: string): Move {
......
...@@ -3,6 +3,18 @@ export interface Position { ...@@ -3,6 +3,18 @@ export interface Position {
column: number; column: number;
} }
/**
* Creates a new Posistion from two numbers, representing
* the new position's column and line.
*
* @param line this position line, from 0..7
* @param column this position column, from 0..7
*/
export function position(column:number, line: number): Position {
let position: Position = {line: line, column: column};
return position;
}
export function top(pos: Position) : Position { export function top(pos: Position) : Position {
let topPosition = {column: pos.column, line: pos.line - 1}; let topPosition = {column: pos.column, line: pos.line - 1};
return topPosition; return topPosition;
......
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