Skip to content

Commit

Permalink
fix: error when pawn or knight is near edge
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperwyczawski committed Aug 21, 2024
1 parent 21d2ff9 commit fb240ba
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/pieces/knight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Knight implements Piece {
];

const points = potentialMoves
.filter((p) => board.cellAt(p) !== undefined)
.filter((p) => p.x >= 0 && p.x < board.width && p.y >= 0 && p.y < board.height)
.filter((p) => board.cellAt(p).piece?.color !== this.#color)
.filter((p) => board.cellAt(p).building !== "wall");

Expand Down
7 changes: 2 additions & 5 deletions src/pieces/pawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export class Pawn implements Piece {
this.#color = color;
}

getAvailableMoves(board: Board, point: Point): Point[] {
const x = point.x;
const y = point.y;

getAvailableMoves(board: Board, { x, y }: Point): Point[] {
let points = [
{ x: x - 1, y: y - 1 },
{ x: x, y: y - 1 },
Expand All @@ -41,7 +38,7 @@ export class Pawn implements Piece {
];

points = points
.filter((p) => board.cellAt(p) !== undefined)
.filter((p) => p.x >= 0 && p.x < board.width && p.y >= 0 && p.y < board.height)
.filter((p) => board.cellAt(p).piece?.color !== this.#color)
.filter((p) => board.cellAt(p).building !== "wall");

Expand Down

0 comments on commit fb240ba

Please sign in to comment.