UNPKG

chlss

Version:

Open-Source Chess Engine in TypeScript.

120 lines (119 loc) 5.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoardPosition = void 0; const piece_1 = require("./piece"); const pieces_1 = __importDefault(require("./pieces")); const coords_1 = require("./coords"); const colour_1 = require("./colour"); exports.BoardPosition = { getAllPiecesByColour(boardPosition, colour) { return boardPosition.filter(function (p) { const pieceColour = piece_1.Piece.getColour(p); if (pieceColour == null) return false; return pieceColour == colour; }); }, createEmpty() { let boardArray = new Array(); for (let x = 0; x < 64; x++) { boardArray.push(pieces_1.default.Empty); } return boardArray; }, copyAsync(boardPosition) { const copy = exports.BoardPosition.createEmpty(); for (let index = 0; index < 64; index++) { const squareIndex = index; exports.BoardPosition.setPiece(copy, squareIndex, exports.BoardPosition.getPiece(boardPosition, squareIndex)); } return copy; }, setEmpty(boardPosition, square) { boardPosition[square] = pieces_1.default.Empty; }, createDefault() { return [pieces_1.default.BlackRook, pieces_1.default.BlackKnight, pieces_1.default.BlackBishop, pieces_1.default.BlackQueen, pieces_1.default.BlackKing, pieces_1.default.BlackBishop, pieces_1.default.BlackKnight, pieces_1.default.BlackRook, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.BlackPawn, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.Empty, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhitePawn, pieces_1.default.WhiteRook, pieces_1.default.WhiteKnight, pieces_1.default.WhiteBishop, pieces_1.default.WhiteQueen, pieces_1.default.WhiteKing, pieces_1.default.WhiteBishop, pieces_1.default.WhiteKnight, pieces_1.default.WhiteRook ]; }, isEmpty(boardPosition, x, y) { return this.isSquareEmpty(boardPosition, coords_1.Coords.toSquareIndex(x, y)); }, isSquareEmpty(boardPosition, square) { return piece_1.Piece.isEmpty(this.getPiece(boardPosition, square)); }, isSquaresEmpty(boardPosition, ...squares) { for (let index of squares) { if (!(this.isSquareEmpty(boardPosition, index))) return false; } return true; }, setPiece(boardPosition, square, piece) { boardPosition[square] = piece; }, getPiece(boardPosition, square) { return boardPosition[square]; }, getPieceByCoords(boardPosition, x, y) { return boardPosition[coords_1.Coords.toSquareIndex(x, y)]; }, getPieceOrNull(boardPosition, square) { const piece = this.getPiece(boardPosition, square); return piece == pieces_1.default.Empty ? null : piece; }, isInBoard(x, y) { return x >= 0 && x <= 7 && y >= 0 && y <= 7; }, isIndexInBoard(index) { return index >= 0 && index <= 63; }, isSquareUnderAttack(squareIndex, moveList) { return moveList.filter(function (move) { return move.to == squareIndex; }).length > 0; }, isSquaresNotUnderAttack(enemyColour, moveList, boardPosition, ...squareIndexes) { for (const move of moveList) { for (let squareIndex of squareIndexes) { if (squareIndex == move.to) return false; } } function isEnemyPawnLookingAt(enemyColour, square) { const yDiff = enemyColour == colour_1.Colours.white ? 1 : -1; const x1 = coords_1.Coords.toX(square) + 1; const x2 = coords_1.Coords.toX(square) - 1; const y = coords_1.Coords.toY(square) + yDiff; const _x1 = exports.BoardPosition.getPieceByCoords(boardPosition, x1, y); const _x2 = exports.BoardPosition.getPieceByCoords(boardPosition, x2, y); function isEnemyPawn(piece) { return piece_1.Piece.isPawn(piece) && piece_1.Piece.getColour(piece) == enemyColour; } if (exports.BoardPosition.isInBoard(x1, y)) { if (isEnemyPawn(exports.BoardPosition.getPieceByCoords(boardPosition, x1, y))) return true; } if (exports.BoardPosition.isInBoard(x2, y)) { if (isEnemyPawn(exports.BoardPosition.getPieceByCoords(boardPosition, x2, y))) return true; } return false; } for (const squareIndex of squareIndexes) { if (isEnemyPawnLookingAt(enemyColour, squareIndex)) return false; } return true; } };