Commit c58423fa authored by Saad MAHBOUBI's avatar Saad MAHBOUBI
Browse files

ajout de la fonction king() mais ca ne mange pas, qlq test

parent dd85c813
......@@ -27,13 +27,19 @@ 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 positionE1 : Position = position(4, 0) // E1
const positionE3 : Position = position(4, 2) // E3
const positionE4 : Position = position(4, 3) // E4
const positionE8 : Position = position(4, 7) // E8
const positionF2 : Position = position(5, 1) // F2
const positionF4 : Position = position(5, 3) // F4
const positionF6 : Position = position(5, 5) // F6
const positionG3 : Position = position(6, 2) // G3
const positionG5 : Position = position(6, 4) // G5
const positionH1 : Position = position(7, 0) // H1
const positionH4 : Position = position(7, 3) // H4
const positionH7 : Position = position(7, 8) // H7
......@@ -192,12 +198,21 @@ describe("Test whitePawnMove()", () => {
describe("Test kingMove()", () => {
beforeEach( () => {
// TODO:
// Initialize an empty chessboard
chessboard = createEmptyChessboard();
// Place a black King on E4
putPiece(chessboard, positionE4, pieces.blackKing);
});
it("A King can move 1 square in all directions", () => {
// Check it can move to squares D3, D4, D5, E3, E5, F3, F4, and F5
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();
})
it("A King cannot move more than 1 square", () => {
......
import { Chessboard, isEmpty, Square, squareAtPosition } from "./chessboard";
import { Move } from "./movements";
import { equals, left, right, bottom, top } from "./position";
import * as pieces from './piece';
/**
* Checks whether a Black Pawn can perform a given move.
......@@ -76,7 +77,62 @@ export function whitePawnMove(board: Chessboard, move: Move): boolean {
*/
export function kingMove(board: Chessboard, move: Move): boolean {
// #TODO: Implement this function
return true;
if (equals(move.to!, bottom(move.from!))) {
return isEmpty(board, move.to!);
}
if (equals(move.to!, top(move.from!))) {
return isEmpty(board, move.to!);
}
if (equals(move.to!, right(move.from!))) {
return isEmpty(board, move.to!);
}
if (equals(move.to!, left(move.from!))) {
return isEmpty(board, move.to!);
}
if(equals(move.to!, bottom(left(move.from!)))){
return isEmpty(board, move.to!);
}
if(equals(move.to!, bottom(right(move.from!)))){
return isEmpty(board, move.to!);
}
if(equals(move.to!, top(right(move.from!)))){
return isEmpty(board, move.to!);
}
if(equals(move.to!, top(left(move.from!)))){
return isEmpty(board, move.to!);
}
let depart: Square = squareAtPosition(board, move.from!);
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 (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 (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 (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)
}
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