UNPKG

tsgammon-core

Version:

A Backgammon library for Typescript, formerly developed as a part of tsgammon-ui

73 lines (72 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatState = void 0; const SGResult_1 = require("../../records/SGResult"); const defaultNames_1 = require("../../records/utils/defaultNames"); const formatPly_1 = require("../../utils/formatPly"); /** * 現局面を単純な文字列で表記する * * @param sgState * @param cbState * @param cpState * @param moveFormatDirection * @param red * @param white * @returns */ function formatState(sgState, cbState, cpState, moveFormatDirection, red = defaultNames_1.defaultNames.red, white = defaultNames_1.defaultNames.white) { if (cbState && cbState.tag !== 'CBInPlay') { switch (cbState.tag) { case 'CBOpening': return 'Opening roll.'; case 'CBResponse': return `${cbState.isDoubleFromRed ? red : white} offers double.`; case 'CBAction': { const lastPly = sgState.tag === 'SGToRoll' ? sgState.lastPly : undefined; return formatToRoll(cbState.isRed, lastPly, moveFormatDirection, red, white); } case 'CBToRoll': { const lastPly = sgState.tag === 'SGToRoll' ? sgState.lastPly : undefined; return cbState.lastAction === 'Take' ? `${cbState.isRed ? white : red} accepts the cube.` : formatToRoll(cbState.isRed, lastPly, moveFormatDirection, red, white); } case 'CBEoG': { return `${cbState.result === SGResult_1.SGResult.REDWON ? `${red} win.` : cbState.result === SGResult_1.SGResult.WHITEWON ? `${white} win.` : ''}`; } } } return formatSGState(sgState, cpState, moveFormatDirection, red, white); } exports.formatState = formatState; function formatToRoll(isRed, lastPly, moveFormatDirection, red = defaultNames_1.defaultNames.red, white = defaultNames_1.defaultNames.white) { return (`${isRed ? red : white} to roll.` + (lastPly ? ` ( last play ${(0, formatPly_1.formatPlyAbbr)(lastPly, moveFormatDirection)} )` : '')); } function formatSGState(sgState, cpState, moveFormatDirection, red = defaultNames_1.defaultNames.red, white = defaultNames_1.defaultNames.white) { // 移動中 if (cpState) { return (0, formatPly_1.formatPly)(cpState.curPly); } switch (sgState.tag) { case 'SGOpening': return 'Opening roll.'; case 'SGToRoll': return formatToRoll(sgState.isRed, sgState.lastPly, moveFormatDirection, red, white); case 'SGInPlay': { return (0, formatPly_1.formatPly)(sgState.curPly); } case 'SGEoG': { return sgState.result === SGResult_1.SGResult.NOGAME ? 'No game' : `${sgState.result === SGResult_1.SGResult.REDWON ? red : white} wins ${sgState.stake.value}pt.`; } } }