UNPKG

chess-easy

Version:

Chess engine that makes writing chessgame easier than writing a calculator

103 lines 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Game = void 0; const common_1 = require("../types/common"); const game_1 = require("../types/game"); const FenValidator_1 = require("./FenValidator"); const MovesGenerator_1 = require("./MovesGenerator"); const utils_1 = require("../utils"); const MoveMaker_1 = require("./MoveMaker"); const MoveIndexes_1 = require("./MoveIndexes"); const FenGenerator_1 = require("./FenGenerator"); class Game { constructor(fen) { this.START_GAME_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; this.parseGameStateRowFen = (gameRowFen) => { const symbols = gameRowFen.split(""); return symbols.reduce((acc, curr) => { const numberValue = Number(curr); return isNaN(numberValue) ? [...acc, utils_1.fenSymbolsToPiecesMapping[curr]] : [...acc, ...Array(numberValue).fill(null)]; }, []); }; this.fen = fen || this.START_GAME_FEN; this.fenToGameState(this.fen); this.getAllPossibleMoves(); } parseGameStateFen(gameStateFen) { const rows = gameStateFen.split("/").reverse(); const result = rows.map(this.parseGameStateRowFen); return result; } fenToGameState(fen) { if (!FenValidator_1.default.validateFen(fen)) { throw new Error("Fen string invalid"); } const [gameState, movesNext, castlingAvailability, enPassantPossibility, halfMoveClock, fullMoveCounter,] = fen.split(" "); this.movesNext = movesNext === "w" ? common_1.Colors.WHITE : common_1.Colors.BLACK; this.castlingAvailability = castlingAvailability; this.enPassantPossibility = enPassantPossibility; this.halfMoveClock = Number(halfMoveClock); this.fullMoveNumber = Number(fullMoveCounter); this.gameState = this.parseGameStateFen(gameState); } getAllPossibleMoves() { const movesGenerator = new MovesGenerator_1.MovesGenerator(this.gameState); const { allMoves, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, } = movesGenerator.getAllPossibleMoves(this.movesNext, this.castlingAvailability, this.enPassantPossibility); this.isCheck = isCheck; this.isCheckmate = isCheckmate; this.isStalemate = isStalemate; this.isInsufficientMaterial = isInsufficientMaterial; this.possibleMoves = allMoves; } getGameStateObject() { const gameStateObject = {}; this.gameState.forEach((row, row_index) => { row.forEach((field, column_index) => { const piecePosition = `${(0, utils_1.mapColumnIndexToLetter)(column_index)}${row_index + 1}`; if (field) { gameStateObject[piecePosition] = field; } }); }); return gameStateObject; } isPromotionMove(from, to) { const moveIndexes = new MoveIndexes_1.MoveIndexes(from, to); return MoveMaker_1.MoveMaker.isPromotionMove(this.gameState, moveIndexes, this.movesNext); } isDraw() { if (this.isStalemate) { return { isDraw: true, reason: "Stalemate" }; } if (this.isInsufficientMaterial) { return { isDraw: true, reason: "Insufficient Material" }; } return { isDraw: false }; } move(from, to, promotion = game_1.PromotionPossibility.QUEEN) { if (this.possibleMoves[from].includes(to)) { const moveMaker = new MoveMaker_1.MoveMaker(this.gameState, this.enPassantPossibility, this.movesNext, this.castlingAvailability); const { gameState, enPassantPossibility, castlingAvailability } = moveMaker.move(from, to, promotion); this.gameState = gameState; this.enPassantPossibility = enPassantPossibility; this.castlingAvailability = castlingAvailability; this.movesNext = this.movesNext === common_1.Colors.WHITE ? common_1.Colors.BLACK : common_1.Colors.WHITE; this.getAllPossibleMoves(); FenGenerator_1.default.generateFen(this.gameState, this.movesNext, this.castlingAvailability, this.enPassantPossibility, this.halfMoveClock, this.fullMoveNumber); return true; } return false; } generateFen() { return FenGenerator_1.default.generateFen(this.gameState, this.movesNext, this.castlingAvailability, this.enPassantPossibility, this.halfMoveClock, this.fullMoveNumber); } getNextColor() { return this.movesNext === common_1.Colors.WHITE ? "white" : "black"; } } exports.Game = Game; exports.default = Game; //# sourceMappingURL=Game.js.map