UNPKG

tsgammon-core

Version:

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

130 lines (129 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchStateNewGame = exports.matchStateLastGame = exports.matchStateEoG = exports.isCubeMaxForMatch = exports.shouldSkipCubeAction = exports.matchStateForPointMatch = exports.matchStateForUnlimitedMatch = void 0; const CubeState_1 = require("./CubeState"); const Score_1 = require("./Score"); /** * 無制限のマッチゲームを開始する * * @param scoreBefore 開始時点での両プレイヤーのスコア * @param jacobyRule ジャコビールールを適用する場合はtrue * @returns */ function matchStateForUnlimitedMatch(scoreBefore = (0, Score_1.score)(), jacobyRule = true) { return matchStateInPlay(0, scoreBefore, jacobyRule, false); } exports.matchStateForUnlimitedMatch = matchStateForUnlimitedMatch; /** * ポイントマッチを開始する。 * * @param matchLength マッチの長さ * @param scoreBefore 開始時点での両プレイヤーのスコア * @param isCrawford 次ゲームをクロフォードとするならtrue。スコアとマッチの長さについてのチェックは行わない * @returns */ function matchStateForPointMatch(matchLength, scoreBefore = (0, Score_1.score)(), isCrawford = false) { return matchStateInPlay(matchLength, scoreBefore, false, isCrawford); } exports.matchStateForPointMatch = matchStateForPointMatch; /** * 現在のマッチ状態で、isRedで指定されたプレイヤーについてキューブアクション可能かどうかを返す * * @param matchState マッチの状況 * @param cubeValue キューブの値 * @param isRed Redについて確認するならtrue / Whiteならfalse * @returns */ function shouldSkipCubeAction(matchState, cubeValue, isRed) { return (matchState.isCrawford || // isCubeMaxFor(matchState, cubeValue, isRed)); } exports.shouldSkipCubeAction = shouldSkipCubeAction; function isCubeMaxFor(matchState, cubeValue, isRed) { return (matchState.matchLength > 0 && matchState.matchLength <= cubeValue + (isRed ? matchState.score.redScore : matchState.score.whiteScore)); } /** * 現在のマッチの状態で、キューブが最大値に達しているかどうかをキューブの状態から判定する(したがって、クロフォードは影響しない)。 * * shouldSkipCubeAction()とは、プレイヤーの指定を必要としないことと、クロフォードを含めない点で異なっている。 * * (これは、キューブが使えない理由があるのはキューブ状態かクロフォードルールかを呼び出し側で把握するために判定から除外した)。 * * @param matchState マッチの状態 * @param cubeState キューブの状態 * @returns */ function isCubeMaxForMatch(matchState, cubeState) { return _isCubeMaxForMatch(matchState, cubeState.value, cubeState.owner); function _isCubeMaxForMatch(matchState, cubeValue, cubeOwner) { return cubeOwner === undefined ? isDoubleMatchPoint(matchState) : isCubeMaxFor(matchState, cubeValue, cubeOwner === CubeState_1.CubeOwner.RED); function isDoubleMatchPoint(matchState) { return (matchState.score.redScore === matchState.matchLength - 1 && matchState.score.whiteScore === matchState.matchLength - 1); } } } exports.isCubeMaxForMatch = isCubeMaxForMatch; function matchStateInPlay(matchLength, scoreBefore, jacobyRule, isCrawford = false) { return { isEoG: false, matchLength, scoreBefore, score: scoreBefore, stakeConf: { jacobyRule }, isCrawford, }; } /** * ゲーム終了時のマッチ状態を生成する * @param matchState マッチ状態 * @param stake 獲得したスコア * @param eogStatus 終局状態 * @returns */ function matchStateEoG(matchState, stake, eogStatus) { const scoreAfter = matchState.scoreBefore.add(stake); return Object.assign(Object.assign({}, matchState), { isEoG: true, score: scoreAfter, stake, eogStatus, scoreAfter, isEoM: isEndOfMatch(matchState.matchLength, scoreAfter), isCrawfordNext: isCrawfordNext(matchState.matchLength, matchState.scoreBefore, scoreAfter) }); } exports.matchStateEoG = matchStateEoG; /** * 終局時のマッチ状態から、終局直前のマッチ状態を生成する。 * * これは、記録を遡って再開する時、終局状態を解除するために使用している。 * * @param matchState 終局時のマッチ状態 * @returns */ function matchStateLastGame(matchState) { return matchStateInPlay(matchState.matchLength, matchState.scoreBefore, matchState.stakeConf.jacobyRule, matchState.isCrawford); } exports.matchStateLastGame = matchStateLastGame; /** * 終局時のマッチ状態から、次のゲームを開始したマッチ状態を生成する * @param matchState 終局時のマッチ状態 * @returns */ function matchStateNewGame(matchState) { return matchStateInPlay(matchState.matchLength, matchState.scoreAfter, matchState.stakeConf.jacobyRule, matchState.isCrawfordNext); } exports.matchStateNewGame = matchStateNewGame; function isCrawfordNext(matchLength, scoreBefore, scoreAfter) { return (matchLength !== 0 && scoreBefore.redScore < matchLength - 1 && scoreBefore.whiteScore < matchLength - 1 && (scoreAfter.redScore === matchLength - 1 || scoreAfter.whiteScore === matchLength - 1)); } function isEndOfMatch(matchLength, score) { return (matchLength !== 0 && (score.redScore >= matchLength || score.whiteScore >= matchLength)); }