Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider b06af223 rédigé par Jessica RADAFY's avatar Jessica RADAFY
Parcourir les fichiers

Mise a jour

parent 86a1e6a1
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, top, bottom } from "./position";
import e = require("express");
/**
* Checks whether a Black Pawn can perform a given move.
......@@ -72,20 +71,21 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function kingMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
if (equals(move.to!, bottom(move.from!)) || equals(move.to!, top(move.from!))
|| equals(move.to!, right(move.from!)) || equals(move.to!, left(move.from!))
|| equals(move.to!, top(right(move.from!))) || equals(move.to!, top(left(move.from!)))
|| equals(move.to!, bottom(left(move.from!))) || equals(move.to!, bottom(right(move.from!))))
{
let destination : Square = squareAtPosition(board,move.to!);
let origin : Square = squareAtPosition(board, move.from!);
if (!(origin.piece!.isWhite))
{
let depart : Square = squareAtPosition(board, move.from!);
if (!(depart.piece!.isWhite))
{
return (destination.isEmpty || destination.piece!.isWhite);
}
else {
return !(destination.isEmpty || destination.piece!.isWhite);
}
}
else {
return !(destination.isEmpty || destination.piece!.isWhite);
}
}
return false;
}
......@@ -117,11 +117,11 @@ export function queenMove(board: Chessboard, move: Move): boolean {
*/
export function empressMove(board: Chessboard, move: Move): boolean {
let destination : Square = squareAtPosition(board, move.to!);
let depart : Square = squareAtPosition(board, move.from!);
//Validation du mouvement pour se déplacer verticalement
//Validation du mouvement pour se déplacer horizontalement
if (move.from?.rank === move.to?.rank)
{
return isEmpty(board, move.to!);
return isEmpty(board, move.to!);
}
//Validation du mouvement pour se déplacer verticalement
......@@ -131,18 +131,21 @@ export function empressMove(board: Chessboard, move: Move): boolean {
}
//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!))))
)
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;
}
/**
* Checks whether a Princess can perform a given move.
* A princess can move any number of squares diagonally,
......@@ -156,8 +159,24 @@ export function empressMove(board: Chessboard, move: Move): boolean {
* @param move
*/
export function princessMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
let destination: Square = squareAtPosition(board, move.to!);
let start: Square = squareAtPosition(board, move.from!);
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);
}
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 !== start.piece!.isWhite);
}
return false;
}
/**
......@@ -192,4 +211,6 @@ export function camelMove(board: Chessboard, move: Move): boolean {
return !(destination.isEmpty || destination.piece!.isWhite);
}
return false;
}
}
......@@ -162,3 +162,5 @@ export class TestCamelMoves {
Expect(isPossible.camelMove(chessboard, mvtCapture)).toBe(false);
}
}
......@@ -84,7 +84,7 @@ export class TestEmpressMoves {
beforeEach() {
chessboard = createEmptyChessboard ();
//La variable "positionE4" a été crée au début du module pour simplifier le code des tests
//Place une Impératrice noire sur la case E4 de l'échiquier
//Place une Impératrice sur la case E4 de l'échiquier
putPiece(chessboard, positionE4, pieces.blackEmpress)
}
......@@ -133,7 +133,7 @@ export class TestEmpressMoves {
}
@Test("A Empress can capture a piece from different color")
testCanCaptureDifferentColor() {
testCanCaptureDifferentColor() {
//put a white pawn in H4
// Check the move moveE4_H4 is possible
}
......@@ -141,15 +141,16 @@ export class TestEmpressMoves {
@Test("A Empress cannot capture a piece from the same color")
testCannotCaptureSameColor() {
// Put a black pawn in H4
// Check the move moveE4_H4 is impossible
// Check the move moveE4_H4 is impossibl
putPiece(chessboard, positionH4, pieces.blackPawn);
let mvtCapture: Move = {from: positionE4, to: positionH4, isValid: true}
Expect(isPossible.empressMove(chessboard, mvtCapture)).toBe(false);
Expect(isPossible.empressMove(chessboard, mvtCapture)).toBe(false);
}
@Test("A Empress cannot leap other pieces, when moving horizontally")
testCannotLeapHorizontally() {
// TODO:
// Place a black Pawn on F4
// Check the move moveE4_H4 is impossible
}
......
......@@ -87,21 +87,18 @@ const moveE4_C6 : Move = move(positionE4, positionC6);
const moveE4_G2 : Move = move(positionE4, positionG2);
const moveE4_G4 : Move = move(positionE4, positionG4);
const moveE4_G6 : Move = move(positionE4, positionG6);
export class TestKingMoves {
@Setup
beforeEach() {
// Initialize an empty chessboard
// Place a black King on E4
chessboard = createEmptyChessboard ();
chessboard = createEmptyChessboard ();
//Place un Roi noir sur la case E4 de l'échiquier
putPiece(chessboard, positionE4, pieces.blackKing)
}
@Test("A King can move 1 square in all directions")
testCanMoveOneSquare() {
// Check the following moves are possible : moveE4_D4, moveE4_F4, moveE4_E3,
// moveE4_E5, moveE4_D3, moveE4_D5, moveE4_F3, moveE4_F5
//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);
......@@ -114,8 +111,7 @@ export class TestKingMoves {
@Test("A King cannot move more than 1 square")
testCannotMoveMoreThanOneSquare() {
// Check the following moves are impossible: moveE4_C2, moveE4_C3, moveE4_C4,
// moveE4_C6, moveE4_E2, moveE4_E6, moveE4_G2, moveE4_G4, and moveE4_G6
// 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);
......@@ -129,8 +125,7 @@ export class TestKingMoves {
@Test("A King cannot capure pieces from the same color")
testCannotCaptureSameColor() {
// Place a black Pawn on E5
// Check the move moveE4_E5 is impossible
// Check the King cannot move to E5.
putPiece(chessboard, positionE5, pieces.blackPawn);
let mvtCapture: Move = {from: positionE4, to: positionE5, isValid: true}
Expect(isPossible.kingMove(chessboard, mvtCapture)).toBe(false);
......@@ -138,11 +133,10 @@ export class TestKingMoves {
@Test("A King can capure pieces from a different color")
testCanCaptureSameColor() {
// Place a white Pawn on E5
// Check the move moveE4_E5 is possible
// Check the King can move to E5.
putPiece(chessboard, positionE5, pieces.whitePawn);
let mvtCapture: Move = {from: positionE4, to: positionE5, isValid: true}
Expect(isPossible.kingMove(chessboard, mvtCapture)).toBe(true);
}
}
\ No newline at end of file
}
import { Expect, Test, Setup} from "alsatian";
import { Chessboard, createEmptyChessboard, putPiece } from '../../main/ts/chessboard';
import * as isPossible from '../../main/ts/move-validation'
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
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 positionB4 : Position = position(1,3) // B4
const positionB5 : Position = position(1, 4) // B5
const positionB6 : Position = position(1, 5) // B6
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
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 positionE2 : Position = position(4, 1) // E2
const positionE3 : Position = position(4, 2) // E3
const positionE4 : Position = position(4, 3) // E4
const positionE5 : Position = position(4, 4) // E5
const positionE6 : Position = position(4, 5) // E6
const positionE7 : Position = position(4, 6) // E7
const positionE8 : Position = position(4, 7) // E8
const positionF1 : Position = position(5, 0) // F1
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 positionF7 : Position = position(5, 6) // F7
const positionG2 : Position = position(6, 1) // G2
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 positionH2 : Position = position(7,1) // H2
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_B4 : Move = move(positionE4, positionB4);
// Vertical moves
const moveE4_E1 : Move = move(positionE4, positionE1);
const moveE4_E7 : Move = move(positionE4, positionE7);
// 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);
// Impossible moves
const moveE4_H2 : Move = move(positionE4, positionH2);
const moveE4_C7 : Move = move(positionE4, positionC7);
//Two horizontal and one vertical
const moveE4_C3 : Move = move(positionE4, positionC3);
const moveE4_C5 : Move = move(positionE4, positionC5);
//Two vertical and one horizontal
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
beforeEach() {
// TODO:
// Initialize an empty chessboard
chessboard = createEmptyChessboard ();
// Place a black Princess on E4
putPiece(chessboard, positionE4, pieces.blackPrincess)
}
@Test("A Princess can move diagonally")
testCanMoveDiagonally() {
// TODO:
// 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
}
@Test("A Princess can move two squares horizontally and one square vertically")
testCanMoveTwoHorizontalAndOneVertical() {
// TODO
Expect(isPossible.princessMove(chessboard, moveE4_C3)).toBeTruthy()
Expect(isPossible.princessMove(chessboard, moveE4_C5)).toBeTruthy()
// moveE4_C3, moveE4_C5,
}
@Test("A Princess can move two squares vertically and one square horizontally")
testCanMoveTwoVerticalAndOneHorizontal() {
// TODO
Expect(isPossible.princessMove(chessboard, moveE4_F2)).toBeTruthy()
Expect(isPossible.princessMove(chessboard, moveE4_F6)).toBeTruthy()
// moveE4_F2, moveE4_F6,
}
@Test("A Princess cannot move horizontally")
testCannotMoveHorizontally() {
// TODO:
// Check the following moves are impossible: moveE4_H4, moveE4_A4
// 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()
}
@Test("A Princess cannot move vertically")
testCannotMoveVertically() {
// TODO:
// Check the following moves are impossible: moveE4_E1, moveE4_E8
// 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()
}
@Test("A Princess can capture a piece from another color")
testCanCaptureDifferentColor() {
// TODO:
// Place a white Pawn on A8
// Check the move moveE4_A8 is possible
putPiece(chessboard, positionA8, pieces.whitePawn)
// Check the move moveE4_A8 is possible
Expect(isPossible.princessMove(chessboard, moveE4_A8)).toBeTruthy()
}
@Test("A Princess cannot capture a piece from the same color")
testCannotCaptureSameColor() {
// TODO:
// Place a black Pawn on A8
// Check the move moveE4_A8 is impossible
putPiece(chessboard, positionA8, pieces.blackPawn)
// Check the move moveE4_A8 is impossible
Expect(isPossible.princessMove(chessboard, moveE4_A8)).not.toBeTruthy()
}
@Test("A Princess cannot leap other pieces")
testCannotLeapDiagonally() {
// TODO:
// Place a white Pawn on C6
// Check the move moveE4_A8 is impossible
putPiece(chessboard, positionC6, pieces.whitePawn)
// Check the move moveE4_A8 is impossible
Expect(isPossible.princessMove(chessboard, moveE4_A8)).not.toBeTruthy()
}
@Test("A Princess can leap other pieces when moving in L ")
testCanLeapOtherPiecesWhenMovingInL() {
// TODO:
// Place a white Pawn on E5
putPiece(chessboard, positionE5, pieces.whitePawn)
// Check the move moveE4_F6 is impossible
Expect(isPossible.princessMove(chessboard, moveE4_F6)).toBeTruthy()
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter