tsgammon-core
Version:
A Backgammon library for Typescript, formerly developed as a part of tsgammon-ui
73 lines (72 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAbsMoves = exports.formatAbsMove = exports.MoveFormatDirection = void 0;
/**
* 駒の移動の表記方法の指定
*/
var MoveFormatDirection;
(function (MoveFormatDirection) {
/** Whiteは開始点を0とし、増加方向に駒を進める表記。Redは24から減少方向に進む */
MoveFormatDirection[MoveFormatDirection["ABSOLUTE"] = 1] = "ABSOLUTE";
/** ABSOLUTEを反転させ、Redの開始点を0とする表記 */
MoveFormatDirection[MoveFormatDirection["ABSOLUTE_INV"] = 2] = "ABSOLUTE_INV";
/** WhiteもRedも開始点を0とし、増加方向に駒を進める表記 */
MoveFormatDirection[MoveFormatDirection["RELATIVE_ASC"] = 3] = "RELATIVE_ASC";
/** RELATIVE_ASCを反転させ、開始点を24とする表記 */
MoveFormatDirection[MoveFormatDirection["RELATIVE_DEC"] = 4] = "RELATIVE_DEC";
})(MoveFormatDirection = exports.MoveFormatDirection || (exports.MoveFormatDirection = {}));
/**
* ムーブ1つを文字列表現に変換する。
*
* ※連続するムーブ(e.g. 1/3 3/4 を1/3/4または1/4とまとめる機能は未実装)
*
* @param move 対象ムーブ
* @param direction 表記方法
* @returns 変換後の文字列
*/
function formatAbsMove(move, direction = MoveFormatDirection.RELATIVE_DEC) {
return moveFormatter(direction)(move);
}
exports.formatAbsMove = formatAbsMove;
/**
* ムーブの配列を文字列表現、またはムーブなしの表記の配列に変換する
*
* ※連続するムーブ(e.g. 1/3 3/4 を1/3/4または1/4とまとめる機能は未実装)
*
* @param move 対象ムーブ
* @param direction 表記方法
* @param labelNoMove ムーブがない(空配列が渡された場合)の表記
* @returns 各ムーブを変換した文字列の配列
*/
function formatAbsMoves(moves, direction = MoveFormatDirection.RELATIVE_DEC, labelNoMove = '') {
if (moves.length === 0) {
return [labelNoMove];
}
return moves.map((move) => formatAbsMove(move, direction));
}
exports.formatAbsMoves = formatAbsMoves;
function moveFormatter(direction) {
const getter = (() => {
switch (direction) {
case MoveFormatDirection.ABSOLUTE: {
return (move) => [move.fromAbs, move.toAbs];
}
case MoveFormatDirection.ABSOLUTE_INV: {
return (move) => [move.fromAbsInv, move.toAbsInv];
}
case MoveFormatDirection.RELATIVE_ASC: {
return (move) => [move.fromAsc, move.toAsc];
}
case MoveFormatDirection.RELATIVE_DEC: {
return (move) => [move.fromDec, move.toDec];
}
}
})();
return (move) => {
const [mFrom, mTo] = getter(move);
const from = move.isReenter ? 'Bar' : mFrom;
const to = move.isBearOff ? 'Off' : mTo;
const hit = move.isHit ? '*' : '';
return `${from}/${to}${hit}`;
};
}