Skip to content

Commit

Permalink
[#1] Implemented en passant logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Monkey-bored committed Dec 16, 2023
1 parent 8c00e67 commit e32fe0e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
9 changes: 6 additions & 3 deletions development/src/logic/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ function actOnTurn(
actOnTurnPieceToSquare(draggedPiece, targetSquare);
}
}

function actOnTurnPieceToPiece(draggedPiece: Piece, targetPiece: Piece) {
export function actOnTurnPieceToPiece(
draggedPiece: Piece,
targetPiece: Piece,
shouldEndTurn = true,
) {
isFriendlyFire = targetPiece.player === draggedPiece.player;
draggedPiece.hasKilled = true;

Expand Down Expand Up @@ -296,7 +299,7 @@ function actOnTurnPieceToPiece(draggedPiece: Piece, targetPiece: Piece) {
}

const targetSquare: Square = { position: targetPiece.position };
move(draggedPiece, targetSquare);
move(draggedPiece, targetSquare, shouldEndTurn);
}

function actOnTurnPieceToSquare(draggedPiece: Piece, targetSquare: Square) {
Expand Down
48 changes: 41 additions & 7 deletions development/src/logic/pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getPieceByPositionAndBoard,
items,
comparePositions,
actOnTurnPieceToPiece,
} from './logic';
import { Item } from './items';
import { OVERWORLD_BOARD_ID } from './constants';
Expand All @@ -38,7 +39,6 @@ export class Piece implements PieceType {
name: string;
hasMoved: boolean;
hasKilled: boolean;

constructor(
position: Position,
player: Player,
Expand Down Expand Up @@ -78,8 +78,36 @@ export type Square = {
};

export class Pawn extends Piece {

enPassant: boolean;

constructor(position: Position, player: Player) {
super(position, player, pawnResource, 'Pawn');
this.enPassant = false;
}

enPassantCheck(targetPosition: Position, draggedPiece: Piece){

let changeInPosition = 0;

if (targetPosition.coordinates[1] === 2) {
changeInPosition = 1;
}
else if (targetPosition.coordinates[1] === 5){
changeInPosition = -1;
} else return false;

targetPosition.coordinates[1] += changeInPosition;
const targetPiece = getPieceByPositionAndBoard(targetPosition);
if ((targetPiece instanceof Pawn) && targetPiece.enPassant){
actOnTurnPieceToPiece(draggedPiece,targetPiece, false);
targetPosition.coordinates[1] -= changeInPosition;
console.log('en passant intiated');
return true;
}

targetPosition.coordinates[1] -= changeInPosition;
return false;
}

validateMove(target: Piece | Square) {
Expand All @@ -90,7 +118,6 @@ export class Pawn extends Piece {

const absDeltaX = Math.abs(deltaX);
const absDeltaY = Math.abs(deltaY);

const stepY =
target.position.coordinates[1] > this.position.coordinates[1]
? 1
Expand All @@ -107,25 +134,32 @@ export class Pawn extends Piece {
return this.position;
}

// Pawns attack diagonally.
if ((target as Piece).name !== undefined) {
return absDeltaY === 1 && absDeltaX === 1
// Pawns can attack diagonally.
const isEatingMove = absDeltaY === 1 && absDeltaX === 1;

if (
(isEatingMove && this.enPassantCheck(target.position,this)) ||
(target as Piece).name !== undefined
){
return isEatingMove
? target.position
: this.position;
}

// Pawns can have an initial two-square move.
if (!this.hasMoved && absDeltaY === 2 && absDeltaX === 0) {
return simulateMove(
const validatedMove = simulateMove(
this.copyPosition(),
target.position,
0,
stepY,
2,
);
this.enPassant = validatedMove === target.position;
return validatedMove;
}

// Pawns move one square forward.
// Pawns can move one square forward.
return absDeltaY === 1 && absDeltaX === 0
? target.position
: this.position;
Expand Down

0 comments on commit e32fe0e

Please sign in to comment.