Skip to content

Commit

Permalink
Amends produceSquares() to also function for Knight moves.
Browse files Browse the repository at this point in the history
  • Loading branch information
aryann committed Nov 25, 2024
1 parent 5779d28 commit edba247
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 3 additions & 9 deletions engine/src/moves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,15 @@ export class MoveGenerator {
}

private generateKnightMoves(from: TSquare, piece: "N" | "n"): TMove[] {
const fromIndex = SQUARES.indexOf(from);
const rank = Math.floor(fromIndex / NUM_FILES);
const file = fromIndex % NUM_FILES;

const moves: TMove[] = [];

for (const offset of KNIGHT_OFFSETS) {
const newRank = rank + offset.rank;
const newFile = file + offset.file;

if (!isInRange(newFile, newRank)) {
const squares = produceSquares(from, offset);
if (squares.length !== 1) {
continue;
}
const to = squares[0];

const to = SQUARES[newRank * NUM_FILES + newFile];
const destinationPiece = this.board.get(to);
if (destinationPiece && getSide(piece) === getSide(destinationPiece)) {
// There is a piece in the destination and it's of the same
Expand Down
6 changes: 6 additions & 0 deletions engine/src/offsets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const produceSquares = (
let file = fromIndex % NUM_FILES;
let rank = Math.floor(fromIndex / NUM_FILES);

const isKnightOffset = Math.abs(offset.file) + Math.abs(offset.rank) === 3;

for (;;) {
file += offset.file;
rank += offset.rank;
Expand All @@ -76,6 +78,10 @@ export const produceSquares = (
if (square === last) {
break;
}

if (isKnightOffset) {
break;
}
}

return squares;
Expand Down

0 comments on commit edba247

Please sign in to comment.