當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript shogi.js.Shogi類代碼示例

本文整理匯總了TypeScript中shogi.js.Shogi的典型用法代碼示例。如果您正苦於以下問題:TypeScript js.Shogi類的具體用法?TypeScript js.Shogi怎麽用?TypeScript js.Shogi使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了js.Shogi類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: normalizeKIFMoves

function normalizeKIFMoves(shogi: Shogi, moves: IMoveFormat[], lastMove?: IMoveFormat) {
    for (let i = 0; i < moves.length; i++) {
        const last = i === 0 ? lastMove : moves[i - 1];
        const move = moves[i].move;
        if (!move) { continue; }
        // 手番
        move.color = shogi.turn;
        if (move.from) {
            // move

            // sameからto復元
            if (move.same) { move.to = last.move.to; }

            // capture復元
            addCaptureInformation(shogi, move);

            // 不成復元
            if (!move.promote && !Piece.isPromoted(move.piece) && Piece.canPromote(move.piece)) {
                // 成ってない
                if (canPromote(move.to, shogi.turn) || canPromote(move.from, shogi.turn)) {
                    move.promote = false;
                }
            }
            // relative復元
            addRelativeInformation(shogi, move);

            try {
                shogi.move(move.from.x, move.from.y, move.to.x, move.to.y, move.promote);
            } catch (e) {
                throw new Error(i + "手目で失敗しました: " + e);
            }
        } else {
            // drop
            if (shogi.getMovesTo(move.to.x, move.to.y, move.piece).length > 0) {
                move.relative = "H";
            }
            shogi.drop(move.to.x, move.to.y, move.piece);
        }
    }
    restoreColorOfIllegalAction(moves, shogi);
    for (let i = moves.length - 1; i >= 0; i--) {
        const move = moves[i].move;
        if (move) {
            if (move.from) {
                shogi.unmove(move.from.x, move.from.y, move.to.x, move.to.y, move.promote, move.capture);
            } else {
                shogi.undrop(move.to.x, move.to.y);
            }
        }
        const last = i === 0 ? lastMove : moves[i - 1]; // When first fork has fork, use lastMove of this fork
        if (moves[i].forks) {
            for (const fork of moves[i].forks) {
                normalizeKIFMoves(shogi, fork, last);
            }
        }
    }
}
開發者ID:na2hiro,項目名稱:json-kifu-format,代碼行數:57,代碼來源:normalizer.ts

示例2: normalizeKI2Moves

function normalizeKI2Moves(shogi: Shogi, moves: IMoveFormat[], lastMove?: IMoveFormat) {
    for (let i = 0; i < moves.length; i++) {
        const last = i === 0 ? lastMove : moves[i - 1];
        const move = moves[i].move;
        if (!move) { continue; }

        // 手番
        move.color = shogi.turn;
        // 同からto復元
        if (move.same) { move.to = last.move.to; }

        // from復元
        const candMoves = shogi.getMovesTo(move.to.x, move.to.y, move.piece);
        if (move.relative === "H" || candMoves.length === 0) {
            // ok
        } else if (candMoves.length === 1) {
            move.from = candMoves[0].from;
        } else {
            // 相対逆算
            const moveAns = filterMovesByRelatives(move.relative, shogi.turn, candMoves);
            if (moveAns.length !== 1) { throw new Error("相対情報が不完全で複數の候補があります"); }
            move.from = moveAns[0].from;
        }

        if (move.from) {
            // move

            // capture復元
            addCaptureInformation(shogi, move);

            try {
                shogi.move(move.from.x, move.from.y, move.to.x, move.to.y, move.promote);
            } catch (e) {
                throw new Error(i + "手目で失敗しました: " + e);
            }
        } else {
            // drop
            shogi.drop(move.to.x, move.to.y, move.piece);
        }
    }
    restoreColorOfIllegalAction(moves, shogi);
    for (let i = moves.length - 1; i >= 0; i--) {
        const move = moves[i].move;
        if (!move) { continue; }
        if (move.from) {
            shogi.unmove(move.from.x, move.from.y, move.to.x, move.to.y, move.promote, move.capture);
        } else {
            shogi.undrop(move.to.x, move.to.y);
        }
        const last = i <= 1 ? lastMove : moves[i - 1];
        if (moves[i].forks) {
            for (const fork of moves[i].forks) {
                normalizeKI2Moves(shogi, fork, last);
            }
        }
    }
}
開發者ID:na2hiro,項目名稱:json-kifu-format,代碼行數:57,代碼來源:normalizer.ts

示例3: normalizeCSA

export function normalizeCSA(obj: IJSONKifuFormat): IJSONKifuFormat {
    restorePreset(obj);
    const shogi = new Shogi(obj.initial || undefined);
    for (let i = 0; i < obj.moves.length; i++) {
        restoreTotalTime(obj.moves[i].time, i >= 2 ? obj.moves[i - 2].time : void 0);
        const move = obj.moves[i].move;
        if (!move) { continue; }
        // 手番
        move.color = shogi.turn;
        if (move.from) {
            // move

            // same復元
            if (i > 0 && obj.moves[i - 1].move && obj.moves[i - 1].move.to.x === move.to.x
                    && obj.moves[i - 1].move.to.y === move.to.y) {
                move.same = true;
            }

            // capture復元
            addCaptureInformation(shogi, move);
            if (Piece.isPromoted(move.piece)) {
                // 成かも
                const from = shogi.get(move.from.x, move.from.y);
                if (from.kind !== move.piece) {
                    move.piece = from.kind;
                    move.promote = true;
                }
            } else if (Piece.canPromote(move.piece)) {
                // 不成かも
                if (canPromote(move.to, shogi.turn) || canPromote(move.from, shogi.turn)) {
                    move.promote = false;
                }
            }
            // relative復元
            addRelativeInformation(shogi, move);

            try {
                shogi.move(move.from.x, move.from.y, move.to.x, move.to.y, move.promote);
            } catch (e) {
                throw new Error(i + "手目で失敗しました: " + e);
            }
        } else {
            // drop
            if (shogi.getMovesTo(move.to.x, move.to.y, move.piece).length > 0) {
                move.relative = "H";
            }
            shogi.drop(move.to.x, move.to.y, move.piece);
        }
    }
    return obj;
}
開發者ID:na2hiro,項目名稱:json-kifu-format,代碼行數:51,代碼來源:normalizer.ts

示例4: addRelativeInformation

function addRelativeInformation(shogi: Shogi, move: IMoveMoveFormat) {
    const moveVectors = shogi.getMovesTo(move.to.x, move.to.y, move.piece)
            .map((mv) => flipVector(shogi.turn, spaceshipVector(mv.to, mv.from)));
    if (moveVectors.length >= 2) {
        const realVector = flipVector(shogi.turn, spaceshipVector(move.to, move.from));
        move.relative = (() => {
            // 上下方向唯一
            if (moveVectors.filter((mv) => mv.y === realVector.y).length === 1) { return YToUMD(realVector.y); }
            // 左右方向唯一
            if (moveVectors.filter((mv) => mv.x === realVector.x).length === 1) {
                if ((move.piece === "UM" || move.piece === "RY") && realVector.x === 0) {
                    // 直はだめ
                    return XToLCR(moveVectors.filter((mv) => mv.x < 0).length === 0 ? -1 : 1);
                } else {
                    return XToLCR(realVector.x);
                }
            }
            // 上下も左右も他の駒がいる
            return XToLCR(realVector.x) + YToUMD(realVector.y);
        })();
    }
}
開發者ID:na2hiro,項目名稱:json-kifu-format,代碼行數:22,代碼來源:normalizer.ts

示例5: addCaptureInformation

function addCaptureInformation(shogi: Shogi, move: IMoveMoveFormat) {
    const to = shogi.get(move.to.x, move.to.y);
    if (to) { move.capture = to.kind; }
}
開發者ID:na2hiro,項目名稱:json-kifu-format,代碼行數:4,代碼來源:normalizer.ts


注:本文中的shogi.js.Shogi類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。