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EpB,CAEA,WAAA,CAAY,GACR,KAAK,UAAU,KAAK,EACxB","file":"/npm/cm-chess@4.0.2/src/Chess.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.48.0.\n * Original file: /npm/cm-chess@4.0.2/src/Chess.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/**\n * Author and copyright: Stefan Haack (https://shaack.com)\n * Repository: https://github.com/shaack/cm-chess\n * License: MIT, see file 'LICENSE'\n */\nimport {Pgn} from \"cm-pgn/src/Pgn.js\"\nimport {TAGS} from \"cm-pgn/src/Header.js\"\nimport {Chess as ChessJs, SQUARES} from \"chess.mjs/src/Chess.js\"\n\nexport const PIECES = {\n    p: {name: \"pawn\", value: 1},\n    n: {name: \"knight\", value: 3},\n    b: {name: \"bishop\", value: 3},\n    r: {name: \"rook\", value: 5},\n    q: {name: \"queen\", value: 9},\n    k: {name: \"king\", value: Infinity}\n}\n\nexport const COLOR = {\n    white: \"w\",\n    black: \"b\"\n}\n\nexport const FEN = {\n    empty: \"8/8/8/8/8/8/8/8 w - - 0 1\",\n    start: \"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"\n}\n\nexport const EVENT_TYPE = {\n    illegalMove: \"illegalMove\",\n    legalMove: \"legalMove\",\n    undoMove: \"undoMove\",\n    initialized: \"initialized\"\n}\n\nexport const GAME_VARIANT = {\n    standard: \"standard\",\n    chess960: \"chess960\"\n//  kingOfTheHill: \"kingOfTheHill\",\n//  threeCheck: \"threeCheck\",\n//  antichess: \"antichess\",\n//  atomic: \"atomic\",\n//  horde: \"horde\",\n//  racingKings: \"racingKings\",\n//  crazyhouse: \"crazyhouse\"\n}\n\nfunction publishEvent(observers, event) {\n    for (const observer of observers) {\n        setTimeout(() => {\n            observer(event)\n        })\n    }\n}\n\n/**\n * Like chess.js, but handles variations and is written in ES5\n * Uses chess.js for validation and cm-pgn for the history and PGN header\n */\nexport class Chess {\n\n    /**\n     * @param props {object}\n     * - an object with these properties:\n     *      fen: a FEN string\n     *      pgn: a PGN\n     *      gameVariant:\n     *      chess960: true, if chess960 are used\n     *      sloppy: true, if sloppy parsing is allowed\n     */\n    constructor(props = {}) {\n        this.observers = []\n        this.props = {\n            fen: undefined, // use a fen or a pgn with setUpFen\n            pgn: undefined,\n            gameVariant: GAME_VARIANT.standard,\n            chess960: undefined,\n            sloppy: true, // sloppy parsing allows small mistakes in SAN\n            ...props\n        }\n        if(this.props.chess960 !== undefined) {\n            console.warn(\"props.chess960 is deprecated, use GAME_VARIANT\")\n            this.props.gameVariant = GAME_VARIANT.chess960\n        }\n        if (typeof props === \"string\") {\n            console.warn(\"directly passing a FEN is deprecated, use `{fen: \\\"\" + props + \"\\\"}`\")\n            this.props.fen = props\n        }\n        if (!this.props.fen && !this.props.pgn) {\n            this.props.fen = FEN.start\n        }\n        if (this.props.fen) {\n            this.load(this.props.fen)\n        } else if (this.props.pgn) {\n            this.loadPgn(this.props.pgn)\n        } else {\n            this.load(FEN.start)\n        }\n    }\n\n    /**\n     * @returns {string} the FEN of the last move, or the setUpFen(), if no move was made.\n     */\n    fen(move = this.lastMove()) {\n        if (move) {\n            return move.fen\n        } else {\n            return this.setUpFen()\n        }\n    }\n\n    /**\n     * @returns {string} the setUp FEN in the header or the default start-FEN\n     */\n    setUpFen() {\n        if (this.pgn.header.tags[TAGS.SetUp]) {\n            return this.pgn.header.tags[TAGS.FEN]\n        } else {\n            return FEN.start\n        }\n    }\n\n    /**\n     * @returns {Map<string, string>} the header tags of the PGN.\n     */\n    header() {\n        return this.pgn.header.tags\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is over at that move\n     */\n    gameOver(move = this.lastMove()) {\n        if (move) {\n            return move.gameOver\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).game_over()\n        }\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in draw at that move\n     */\n    inDraw(move = this.lastMove()) {\n        if (move) {\n            return move.inDraw === true\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).in_draw()\n        }\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in statemate at that move\n     */\n    inStalemate(move = this.lastMove()) {\n        if (move) {\n            return move.inStalemate === true\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).in_stalemate()\n        }\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in draw, because of unsufficiant material at that move\n     */\n    insufficientMaterial(move = this.lastMove()) {\n        if (move) {\n            return move.insufficientMaterial === true\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).insufficient_material()\n        }\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in draw, because of threefold repetition at that move\n     */\n    inThreefoldRepetition(move = this.lastMove()) {\n        return move && move.inThreefoldRepetition === true\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in checkmate at that move\n     */\n    inCheckmate(move = this.lastMove()) {\n        if (move) {\n            return move.inCheckmate === true\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).in_checkmate()\n        }\n    }\n\n    /**\n     * @param move optional\n     * @returns {boolean} true, if the game is in check at that move\n     */\n    inCheck(move = this.lastMove()) {\n        if (move) {\n            return move.inCheck === true\n        } else {\n            return new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960}).in_check()\n        }\n    }\n\n    /**\n     * cm-chess uses cm-pgn for the history and header. See https://github.com/shaack/cm-pgn\n     * @returns {[]} the moves of the game history\n     */\n    history() {\n        return this.pgn.history.moves\n    }\n\n    /**\n     * @returns {null|move} the last move of the main variation or `null`, if no move was made\n     */\n    lastMove() {\n        if (this.pgn.history.moves.length > 0) {\n            return this.pgn.history.moves[this.pgn.history.moves.length - 1]\n        } else {\n            return null\n        }\n    }\n\n    /**\n     * Load a FEN\n     * @param fen\n     */\n    load(fen) {\n        const chess = new ChessJs(fen, {chess960: this.props.gameVariant === GAME_VARIANT.chess960})\n        if (chess && chess.fen() === fen) {\n            this.pgn = new Pgn(undefined, {chess960: this.props.gameVariant === GAME_VARIANT.chess960})\n            if (fen !== FEN.start) {\n                this.pgn.header.tags[TAGS.SetUp] = \"1\"\n                this.pgn.header.tags[TAGS.FEN] = chess.fen()\n                this.pgn.history.props.setUpFen = fen\n            }\n        } else {\n            throw Error(\"Invalid fen \" + fen)\n        }\n        publishEvent(this.observers, {type: EVENT_TYPE.initialized, fen: fen})\n    }\n\n    /**\n     * Load a PGN with variations, NAGs, header and annotations. cm-chess uses cm-pgn\n     * for the header and history. See https://github.com/shaack/cm-pgn\n     * @param pgn\n     * @param sloppy to allow sloppy SAN\n     */\n    loadPgn(pgn, sloppy = this.props.sloppy) {\n        this.pgn = new Pgn(pgn, {sloppy: sloppy})\n        if(this.pgn.props.chess960) {\n            this.props.gameVariant = GAME_VARIANT.chess960\n        }\n        publishEvent(this.observers, {type: EVENT_TYPE.initialized, pgn: pgn})\n    }\n\n    /**\n     * Make a move in the game.\n     * @param move\n     * @param previousMove optional, the previous move (for variations)\n     * @param sloppy to allow sloppy SAN\n     * @returns {{}|null}\n     */\n    move(move, previousMove = undefined, sloppy = this.props.sloppy) {\n        try {\n            const moveResult = this.pgn.history.addMove(move, previousMove, sloppy)\n            publishEvent(this.observers,\n                {type: EVENT_TYPE.legalMove, move: moveResult, previousMove: previousMove})\n            return moveResult\n        } catch (e) {\n            publishEvent(this.observers,\n                {type: EVENT_TYPE.illegalMove, move: move, previousMove: previousMove})\n            return null\n        }\n    }\n\n    /**\n     * Return all valid moves\n     * @param options {{ square: \"e2\", piece: \"n\", verbose: true }}\n     * Fields with { verbose: true }\n     * - `color` indicates the color of the moving piece (w or b).\n     * - `from` and `to` fields are from and to squares in algebraic notation.\n     * - `piece`, `captured`, and `promotion` fields contain the lowercase representation of the applicable piece (pnbrqk). The captured and promotion fields are only present when the move is a valid capture or promotion.\n     * - `san` is the move in Standard Algebraic Notation (SAN).\n     * - `flags` field contains one or more of the string values:\n     *      n - a non-capture\n     *      b - a pawn push of two squares\n     *      e - an en passant capture\n     *      c - a standard capture\n     *      p - a promotion\n     *      k - kingside castling\n     *      q - queenside castling\n     *   A flags value of pc would mean that a pawn captured a piece on the 8th rank and promoted.\n     * @param move\n     * @returns {{}}\n     */\n    moves(options = undefined, move = this.lastMove()) {\n        const chessJs = new ChessJs(this.fen(move), {chess960: this.props.gameVariant === GAME_VARIANT.chess960})\n        return chessJs.moves(options)\n    }\n\n    /**\n     * Don't make a move, just validate, if it would be a correct move\n     * @param move\n     * @param previousMove optional, the previous move (for variations)\n     * @param sloppy to allow sloppy SAN\n     * @returns the move object or null if not valid\n     */\n    validateMove(move, previousMove = undefined, sloppy = this.props.sloppy) {\n        return this.pgn.history.validateMove(move, previousMove, sloppy)\n    }\n\n    /**\n     * Render the game as PGN with header, comments and NAGs\n     * @param renderHeader optional, default true\n     * @param renderComments optional, default true\n     * @param renderNags optional, default true\n     * @returns {string} the PGN of the game.\n     */\n    renderPgn(renderHeader = true, renderComments = true, renderNags = true) {\n        return this.pgn.render(renderHeader, renderComments, renderNags)\n    }\n\n    /**\n     * Get the position of the specified figures at a specific move\n     * @param type \"p\", \"n\", \"b\",...\n     * @param color \"b\" or \"w\"\n     * @param move\n     * @returns {[]} the pieces (positions) at a specific move\n     */\n    pieces(type = undefined, color = undefined, move = this.lastMove()) {\n        const chessJs = move ? new ChessJs(move.fen, {chess960: this.props.gameVariant === GAME_VARIANT.chess960}) : new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960})\n        let result = []\n        for (let i = 0; i < 64; i++) {\n            const square = SQUARES[i]\n            const piece = chessJs.get(square)\n            if (piece) {\n                piece.square = square\n            }\n            if (!type) {\n                if (!color && piece) {\n                    result.push(piece)\n                }\n            } else if (!color && piece && piece.type === type) {\n                result.push(piece)\n            } else if (piece && piece.color === color && piece.type === type) {\n                result.push(piece)\n            }\n        }\n        return result\n    }\n\n    /**\n     * get the piece on a square\n     * @param square\n     * @param move\n     * @returns {{color: any, type: any}|null}\n     */\n    piece(square, move = this.lastMove()) {\n        const chessJs = move ? new ChessJs(move.fen, {chess960: this.props.gameVariant === GAME_VARIANT.chess960}) : new ChessJs(this.fen(), {chess960: this.props.gameVariant === GAME_VARIANT.chess960})\n        return chessJs.get(square)\n    }\n\n    /**\n     * @param move\n     * @returns {string} \"b\" or \"w\" the color to move in the main variation\n     */\n    turn(move = this.lastMove()) {\n        let factor = 0\n        if (this.setUpFen()) {\n            const fenParts = this.setUpFen().split(\" \")\n            if (fenParts[1] === COLOR.black) {\n                factor = 1\n            }\n        }\n        const ply = move ? move.ply : 0\n        return (ply) % 2 === factor ? COLOR.white : COLOR.black\n    }\n\n    /**\n     * Undo a move and all moves after it\n     * @param move\n     */\n    undo(move = this.lastMove()) {\n        // decouple from previous\n        if (move.previous) {\n            move.previous.next = undefined\n        }\n        // splice all next moves\n        const index = move.variation.findIndex(element => {\n            return element.ply === move.ply\n        })\n        move.variation = move.variation.splice(index)\n        publishEvent(this.observers, {type: EVENT_TYPE.undoMove, move: move})\n    }\n\n    plyCount() {\n        return this.history().length\n    }\n\n    fenOfPly(plyNumber) {\n        if (plyNumber > 0) {\n            return this.history()[plyNumber - 1].fen\n        } else {\n            return this.setUpFen()\n        }\n    }\n\n    addObserver(callback) {\n        this.observers.push(callback)\n    }\n}\n"]}