Skip to content

Commit

Permalink
[#139] Fixed pawn movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Jan 18, 2024
1 parent 3f9a397 commit 1e17c8f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
5 changes: 3 additions & 2 deletions development/code/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ function resetVariables() {

pieces.forEach((piece) => {
if (piece.player !== getCurrentPlayer() && piece instanceof Pawn) {
piece.enPassantPositions = undefined;
piece.isDiagonalAttack = false;
piece.possibleEnPassantPositions = undefined;
piece.isInitialDoubleStep = false;
piece.diagonalAttackPosition = undefined;
}
});
}
Expand Down
14 changes: 10 additions & 4 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,17 @@ function onActionPieceToSquare(
}
}

if (draggedPiece instanceof Pawn && draggedPiece.isDiagonalAttack) {
const pawn = draggedPiece.isValidEnPassant(targetSquare.position);
if (!pawn) return;
if (draggedPiece instanceof Pawn) {
draggedPiece.checkInitialDoubleStep(targetSquare.position);

killPiece(draggedPiece, pawn, targetSquare.position);
if (draggedPiece.diagonalAttackPosition) {
if (comparePositions(draggedPiece.diagonalAttackPosition, targetSquare.position)) {
const enPassantPiece = draggedPiece.getEnPassantPiece(targetSquare.position);
if (!enPassantPiece) return;

killPiece(draggedPiece, enPassantPiece, targetSquare.position);
}
}
}

move(draggedPiece, targetSquare);
Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Pawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Piece movements', () => {
blackPawnPosition,
blackPlayer,
);
blackPawn.enPassantPositions = [
blackPawn.possibleEnPassantPositions = [
{
coordinates: [1, 5],
boardId: OVERWORLD_BOARD_ID,
Expand Down
47 changes: 34 additions & 13 deletions development/code/logic/pieces/Pawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,46 @@ import { game } from '../../Game';
import { comparePositions, getPieceByPosition } from '../Utilities';

export class Pawn extends Piece {
enPassantPositions: [Position, Position] | undefined;
isDiagonalAttack: boolean;
possibleEnPassantPositions: [Position, Position] | undefined;
isInitialDoubleStep: boolean;
diagonalAttackPosition: Position | undefined;

constructor(position: Position, player: Player) {
const logo = player.color === PlayerColors.WHITE
? '♙'
: '♟';
super(position, player, pawnResource, 'Pawn', logo);
this.enPassantPositions = undefined;
this.isDiagonalAttack = false;
this.possibleEnPassantPositions = undefined;
this.isInitialDoubleStep = false;
this.diagonalAttackPosition = undefined;
}

isValidEnPassant(targetPosition: Position) {
checkInitialDoubleStep(targetPosition: Position): boolean {
const currentCoordinates = this.position.coordinates;
const currentPlayer = game.getCurrentPlayer();
// Determine the direction of pawn movement based on the player's color
const stepY = currentPlayer.color === PlayerColors.WHITE ? -1 : 1;

const twoSquaresForward: Position = {
coordinates: [currentCoordinates[0], currentCoordinates[1] + 2 * stepY],
boardId: this.position.boardId,
};

if (comparePositions(twoSquaresForward, targetPosition)) {
this.isInitialDoubleStep = true;
return true;
}

return false;
}

getEnPassantPiece(targetPosition: Position): Piece | undefined {
const pawns = game.getPieces().filter(piece => piece instanceof Pawn && piece !== this) as Array<Pawn>;
if (!pawns) return;
if (!pawns.length) return;

const enPassantPawns = pawns.filter(pawn => {
if (pawn.enPassantPositions) {
return comparePositions(pawn.enPassantPositions[0], targetPosition);
if (pawn.isInitialDoubleStep && pawn.possibleEnPassantPositions) {
return comparePositions(pawn.possibleEnPassantPositions[0], targetPosition);
}
});
if (!enPassantPawns.length) return;
Expand Down Expand Up @@ -57,7 +78,7 @@ export class Pawn extends Piece {
};

if (!getPieceByPosition(twoSquaresForward) && !getPieceByPosition(oneSquareForward)) {
this.enPassantPositions = [oneSquareForward, twoSquaresForward];
this.possibleEnPassantPositions = [oneSquareForward, twoSquaresForward];
validMoves.push(twoSquaresForward);
}
}
Expand All @@ -74,13 +95,13 @@ export class Pawn extends Piece {
boardId: this.position.boardId,
};

if (getPieceByPosition(leftDiagonal) || this.isValidEnPassant(leftDiagonal)) {
this.isDiagonalAttack = true;
if (getPieceByPosition(leftDiagonal) || this.getEnPassantPiece(leftDiagonal)) {
this.diagonalAttackPosition = leftDiagonal;
validMoves.push(leftDiagonal);
}

if (getPieceByPosition(rightDiagonal) || this.isValidEnPassant(rightDiagonal)) {
this.isDiagonalAttack = true;
if (getPieceByPosition(rightDiagonal) || this.getEnPassantPiece(rightDiagonal)) {
this.diagonalAttackPosition = rightDiagonal;
validMoves.push(rightDiagonal);
}

Expand Down

0 comments on commit 1e17c8f

Please sign in to comment.