Skip to content

Commit

Permalink
[#27] check
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed Feb 12, 2021
1 parent d126468 commit 7ed17fb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/services/game-logic/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Queen } from './pieces/Queen';
class Board {
public static BOARD_SIZE = 8;

private state: Array<Array<Piece | null>>;
public state: Array<Array<Piece | null>>;

constructor() {
this.state = new Array(Board.BOARD_SIZE);
Expand All @@ -26,6 +26,14 @@ class Board {
return this.state[row][column];
}

public checkAllSquares = (f: any) => {
this.state.forEach((row) => {
row.forEach((square) => {
f(square);
});
});
};

public movePiece(location: Square, destination: Square): void {
this.state[location.row][location.column]?.move(destination);
this.state[destination.row][destination.column] = this.state[location.row][location.column];
Expand Down
64 changes: 55 additions & 9 deletions src/services/game-logic/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PieceNames, Constants } from '../../enums';
import { PieceNames, Constants, Colors } from '../../enums';
import { Square } from '../../models/Square';
import { Board } from './Board';
import { King } from './pieces/King';
import { Piece } from './pieces/Piece';

class GameEngine {
Expand All @@ -12,13 +13,63 @@ class GameEngine {

getLegalMoves = (square: Square): Square[] => {
const piece = this.board.getPiece(square);
let legalMoves = piece?.getPossibleMoves(this.board).filter(this.isOnBoard) ?? [];
let legalMoves = piece?.getPossibleMoves(this.board) ?? [];
if (piece?.name === PieceNames.KING) {
legalMoves = legalMoves.filter(
(move) => !this.isCastling(move, piece) || this.isCastlingLegal(move, piece)
);
}
return legalMoves.filter((destination) => !this.isOccupiedBySameColor(destination, piece));
return legalMoves
.filter((destination) => !this.isOccupiedBySameColor(destination, piece))
.filter((move) => this.removeMovesIfCheck(move, piece, square));
};

private removeMovesIfCheck(move: Square, piece: Piece | null, square: Square) {
let potentialMove: Square = { row: move.row, column: move.column };
let potentialPiece = this.board.getPiece(potentialMove);

this.movePiece(square, potentialMove);
piece!.hasMoved = true;
let isMovePossible = !this.isCheck(piece);

this.movePiece(potentialMove, square);
piece!.hasMoved = false;

if (potentialPiece)
this.board.state[potentialPiece.position.row][potentialPiece.position.column] = potentialPiece;

return isMovePossible;
}

private isCheck(piece: Piece | null): Boolean {
const color = piece?.color === Colors.BLACK ? Colors.WHITE : Colors.BLACK;
let currentPlayerKing: Square | undefined = this.getKingForCheck(piece)?.position;
const isChecked = this.findOponentLegalMoves(color, currentPlayerKing);
return isChecked;
}

findOponentLegalMoves(color: Colors, piecePosition: Square | undefined): boolean {
let checked: boolean[] = [];
this.board.checkAllSquares((square: Piece) => {
if (square && square.color === color) {
square
.getPossibleMoves(this.board)
.forEach((move) =>
move.row === piecePosition?.row && move.column === piecePosition.column
? checked.push(true)
: checked.push(false)
);
}
});
return checked.some((element) => element);
}

getKingForCheck = (piece: Piece | null): King | null => {
let king = null;
this.board.checkAllSquares((square: Piece) => {
if (square && square?.color === piece?.color && square.name === PieceNames.KING) king = square;
});
return king;
};

public movePiece(location: Square, destination: Square): void {
Expand Down Expand Up @@ -69,12 +120,7 @@ class GameEngine {
return this.getLegalMoves(from).some(({ row, column }) => row === to.row && column === to.column);
}; // might be useful for 'check'

private isOnBoard = (square: Square): boolean => {
// here or in knight.ts (for now knights can get outside the board)
return square.row >= 0 && square.row < 8 && square.column >= 0 && square.column < 8;
};

private isOccupiedBySameColor = (square: Square, piece: Piece | null): boolean =>
public isOccupiedBySameColor = (square: Square, piece: Piece | null): boolean =>
this.board.getPiece(square)?.color === piece?.color;
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/game-logic/pieces/Knight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Knight extends Piece {
destination.push(new Square(row + possibleRowMoves[i], column + possibleColumnMoves[i]));
}

return destination;
const knightPossibleMoves = destination.filter((d) => d.row >= 0 && d.row < 8 && d.column >= 0 && d.column < 8);
return knightPossibleMoves;
}
}

Expand Down

0 comments on commit 7ed17fb

Please sign in to comment.