Skip to content

Commit

Permalink
[#1] Made tests for En Passant
Browse files Browse the repository at this point in the history
  • Loading branch information
Monkey-bored committed Dec 16, 2023
1 parent a77ce90 commit e205201
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 16 additions & 2 deletions development/src/logic/pieces.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OVERWORLD_BOARD_ID } from './constants';
import { Player, PlayerColors } from './players';
import { Position, Pawn, Bishop, Knight, Rook, Queen, King } from './pieces';
import { Position, Pawn, Bishop, Knight, Rook, Queen, King, setEnPassantPosition } from './pieces';

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);
Expand Down Expand Up @@ -42,6 +42,20 @@ describe('Piece movements', () => {
});
expect(singleStepValidMove).toEqual(singleStepMove);

pawn.position = initialPosition;
setEnPassantPosition({
coordinates: [1,6],
board: OVERWORLD_BOARD_ID,
});
const enPassantMove: Position = {
coordinates: [1,5],
board: OVERWORLD_BOARD_ID,
};
const enPassantValidMove = pawn.validateMove({
position: enPassantMove,
});
expect(enPassantValidMove).toEqual(enPassantMove);

pawn.position = initialPosition;
const twoStepsInitialMove: Position = {
coordinates: [0, 4],
Expand All @@ -61,7 +75,7 @@ describe('Piece movements', () => {
);
const diagonalAttackValidMove = pawn.validateMove(diagonalAttackMove);
expect(diagonalAttackValidMove).toEqual(diagonalAttackMove.position);

pawn.position = initialPosition;
const invalidPosition: Position = {
coordinates: [0, 3],
Expand Down
10 changes: 7 additions & 3 deletions development/src/logic/pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export let enPassantPosition: Position | undefined;
export function resetEnPassantPosition() {
enPassantPosition = undefined;
}
export function setEnPassantPosition(position: Position) {
enPassantPosition = position;
}

interface PieceType {
position: Position;
Expand Down Expand Up @@ -113,19 +116,20 @@ export class Pawn extends Piece {
) {
return this.position;
}

console.log(enPassantPosition);
// Pawns can attack diagonally.
const isDiagonalMovement = absDeltaY === 1 && absDeltaX === 1;
this.enPassant =
!!enPassantPosition &&
(absDeltaY === 1 && absDeltaX === 1) &&
isDiagonalMovement &&
(targetCoordinates[0] === enPassantPosition.coordinates[0]) &&
Math.abs(targetCoordinates[1] - enPassantPosition.coordinates[1]) === 1;

if (
(this.enPassant) ||
(target as Piece).name !== undefined
){
return absDeltaY === 1 && absDeltaX === 1
return isDiagonalMovement
? target.position
: this.position;
}
Expand Down

0 comments on commit e205201

Please sign in to comment.