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:
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.
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
......
import * as isValid from '../src/move-validation'
import * as pieces from '../src/piece'
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';
let chessboard : Chessboard;
let positionA7 : Position = {column: 0, line: 6} // A7
let positionA6 : Position = {column: 0, line: 5} // A6
let positionA5 : Position = {column: 0, line: 4} // A5
let positionA4 : Position = {column: 0, line: 3} // A4
let positionB6 : Position = {column: 1, line: 5} // B6
let positionC6 : Position = {column: 2, line: 5} // C6
let positionC4 : Position = {column: 2, line: 3} // C4
let positionD2 : Position = {column: 3, line: 1} // D2
let positionD3 : Position = {column: 3, line: 2} // D3
let positionD4 : Position = {column: 3, line: 3} // D4
let positionD5 : Position = {column: 3, line: 4} // D5
let positionA4 : Position = position(0, 3) // A4
let positionA5 : Position = position(0, 4) // A5
let positionA6 : Position = position(0, 5) // A6
let positionA7 : Position = position(0, 6) // A7
let positionA8 : Position = position(0, 7) // A8
let positionB6 : Position = position(1, 5) // B6
let positionC6 : Position = position(2, 5) // C6
let positionC4 : Position = position(2, 3) // C4
let positionD2 : Position = position(3, 1) // D2
let positionD3 : Position = position(3, 2) // D3
let positionD4 : Position = position(3, 3) // D4
let positionD5 : Position = position(3, 4) // D5
describe("Test blackPawnMove()", () => {
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();
});
it("Valid Double foreward A7-A5", () => {
let doubleForward: Move = {from: positionA7, to: positionA5, isValid: true}
it("Pawns cannot move backward", () => {
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();
});
it("Invalid Double foreward C6-C4", () => {
processMove(chessboard,"C7-C6");
it("When a paws has already moved, it cannot move 2 squares forward", () => {
putPiece(chessboard, positionC6, pieces.blackPawn);
let doubleForward: Move = {from: positionC6, to: positionC4, isValid: true}
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}
expect(isValid.blackPawnMove(chessboard, tripleForward)).toBeFalsy();
});
it("Invalid Single Forward (face-to-face)", () => {
processMove(chessboard,"A2-A4");
processMove(chessboard,"A4-A5");
processMove(chessboard,"A5-A6");
it("When in face of another piece, pawns cannot move foreward", () => {
putPiece(chessboard, positionA6, pieces.whitePawn);
putPiece(chessboard, positionA7, pieces.blackPawn);
let singleForward: Move = {from: positionA7, to: positionA6, isValid: true}
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}
expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy();
});
it("Invalid Capture Same Color A7-B6 ", () => {
processMove(chessboard,"B7-B6");
it("Pawns cannot capture pieces of the same color", () => {
putPiece(chessboard, positionA7, pieces.blackPawn);
putPiece(chessboard, positionB6, pieces.blackKing);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeFalsy();
});
it("Valid Capture A7-B6", () => {
processMove(chessboard,"B2-B4");
processMove(chessboard,"B4-B5");
processMove(chessboard,"B5-B6");
it("Pawns can capture pieces of a different color", () => {
putPiece(chessboard, positionA7, pieces.blackPawn);
putPiece(chessboard, positionB6, pieces.whiteQueen);
let diagonalCapture: Move = {from: positionA7, to: positionB6, isValid: true}
expect(isValid.blackPawnMove(chessboard, diagonalCapture)).toBeTruthy();
......@@ -76,53 +89,114 @@ describe("Test whitePawnMove()", () => {
chessboard = createEmptyChessboard();
})
it("Invalid face to face forward", () => {
putPiece(chessboard, 3, 3, pieces.whitePawn); // White Pawn at D4
putPiece(chessboard, 3, 4, pieces.blackPawn); // Black Pawn at D5
it("When there is a piece in front it, pawns cannot move forward", () => {
putPiece(chessboard, positionD4, pieces.whitePawn); // White Pawn at D4
putPiece(chessboard, positionD5, pieces.blackPawn); // Black Pawn at D5
let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeFalsy();
});
it("Valid simple forward", () => {
putPiece(chessboard, 3, 3, pieces.whitePawn); // White Pawn at D4
it("Pawns can move forward", () => {
putPiece(chessboard, positionD4, pieces.whitePawn); // White Pawn at D4
let simpleFowardMove: Move = {from: positionD4, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, simpleFowardMove)).toBeTruthy();
});
it("Valid double forward", () => {
putPiece(chessboard, 3, 1, pieces.whitePawn); // White Pawn at D2
it("When in the initial position, pawns can move two squares forward", () => {
putPiece(chessboard, positionD2, pieces.whitePawn); // White Pawn at D2
let doubleFowardMove: Move = {from: positionD2, to: positionD4, isValid: true}
expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeTruthy();
});
it("Invalid double forward", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3
it("When they have already moved, pawns cannot move two squares forward", () => {
putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
let doubleFowardMove: Move = {from: positionD3, to: positionD5, isValid: true}
expect(isValid.whitePawnMove(chessboard, doubleFowardMove)).toBeFalsy();
});
it("Invalid capture empty square", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3
it("Pawns cannot capture an empty square", () => {
putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy();
});
it("Invalid capture same color", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, 2, 3, pieces.whitePawn); // White Pawn at C4
it("Pawns cannot capture a piece of the same color", () => {
putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, positionC4, pieces.whitePawn); // White Pawn at C4
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
expect(isValid.whitePawnMove(chessboard, diagonalCapture)).toBeFalsy();
});
it("Valid capture", () => {
putPiece(chessboard, 3, 2, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, 2, 3, pieces.blackKing); // Black King at C4
it("Pawns can capture pieces of a different color", () => {
putPiece(chessboard, positionD3, pieces.whitePawn); // White Pawn at D3
putPiece(chessboard, positionC4, pieces.blackKing); // Black King at C4
let diagonalCapture: Move = {from: positionD3, to: positionC4, isValid: true}
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()", () => {});
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 {
let position: Position;
let square : Square;
for(let col: number = 0; col < 8; col++) {
putPiece(chessboard, col, 1, pieces.whitePawn);
putPiece(chessboard, col, 6, pieces.blackPawn);
putPieceAtCoordinate(chessboard, col, 1, pieces.whitePawn);
putPieceAtCoordinate(chessboard, col, 6, pieces.blackPawn);
}
// Kings and Queens
putPiece(chessboard, 3, 0, pieces.whiteKing);
putPiece(chessboard, 3, 7, pieces.blackKing);
putPiece(chessboard, 4, 0, pieces.whiteQueen);
putPiece(chessboard, 4, 7, pieces.blackQueen);
putPieceAtCoordinate(chessboard, 3, 0, pieces.whiteKing);
putPieceAtCoordinate(chessboard, 3, 7, pieces.blackKing);
putPieceAtCoordinate(chessboard, 4, 0, pieces.whiteQueen);
putPieceAtCoordinate(chessboard, 4, 7, pieces.blackQueen);
// Bishops
putPiece(chessboard, 2, 0, pieces.whiteBishop);
putPiece(chessboard, 2, 7, pieces.blackBishop);
putPiece(chessboard, 5, 0, pieces.whiteBishop);
putPiece(chessboard, 5, 7, pieces.blackBishop);
putPieceAtCoordinate(chessboard, 2, 0, pieces.whiteBishop);
putPieceAtCoordinate(chessboard, 2, 7, pieces.blackBishop);
putPieceAtCoordinate(chessboard, 5, 0, pieces.whiteBishop);
putPieceAtCoordinate(chessboard, 5, 7, pieces.blackBishop);
// Knights
putPiece(chessboard, 1, 0, pieces.whiteKnight);
putPiece(chessboard, 1, 7, pieces.blackKnight);
putPiece(chessboard, 6, 0, pieces.whiteKnight);
putPiece(chessboard, 6, 7, pieces.blackKnight);
putPieceAtCoordinate(chessboard, 1, 0, pieces.whiteKnight);
putPieceAtCoordinate(chessboard, 1, 7, pieces.blackKnight);
putPieceAtCoordinate(chessboard, 6, 0, pieces.whiteKnight);
putPieceAtCoordinate(chessboard, 6, 7, pieces.blackKnight);
// Roocks
putPiece(chessboard, 0, 0, pieces.whiteRoock);
putPiece(chessboard, 0, 7, pieces.blackRoock);
putPiece(chessboard, 7, 0, pieces.whiteRoock);
putPiece(chessboard, 7, 7, pieces.blackRoock);
putPieceAtCoordinate(chessboard, 0, 0, pieces.whiteRoock);
putPieceAtCoordinate(chessboard, 0, 7, pieces.blackRoock);
putPieceAtCoordinate(chessboard, 7, 0, pieces.whiteRoock);
putPieceAtCoordinate(chessboard, 7, 7, pieces.blackRoock);
return chessboard;
}
......@@ -144,9 +144,13 @@ function createChessboard(): Chessboard {
}
export function putPiece(chessboard: Chessboard, column : number, line: number, piece : Piece) {
let board : Array<Array<Square>> = chessboard.board;
function putPieceAtCoordinate(chessboard: Chessboard, column : number, line: number, piece : Piece) {
let position : Position = {line : line, column : column};
return putPiece(chessboard, position, piece);
}
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[column][line] = square;
board[position.column][position.line] = square;
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ export interface Move {
to? : Position;
}
export function processMove(chessboard:Chessboard, movementString: string): Chessboard {
export function processMove(chessboard:Chessboard, movementString: string): boolean {
let move : Move = parseMoveString(movementString);
//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
performMove(chessboard, move);
} else {
console.log("Invalid movement !");
return false;
}
return chessboard;
return true;
}
export function parseMoveString(movementString: string): Move {
......
......@@ -3,6 +3,18 @@ export interface Position {
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 {
let topPosition = {column: pos.column, line: pos.line - 1};
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