From 3996141f831868bce32e83d8f2ca47da6d9a3323 Mon Sep 17 00:00:00 2001 From: ssttkkl Date: Fri, 10 Feb 2023 02:13:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9E=84=E9=80=A0Furo=E4=B8=8EMentsu?= =?UTF-8?q?=E4=B8=8ETatsu=E6=97=B6=E5=A2=9E=E5=8A=A0=E5=90=88=E6=B3=95?= =?UTF-8?q?=E6=80=A7=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 1 + package.json | 2 +- src/hora/coder.js | 65 ----------- src/hora/index.js | 39 ------- src/hora/models.js | 2 - src/hora/yaku.js | 77 ------------- src/models/Furo.js | 120 --------------------- src/models/Furo.ts | 19 ++++ src/models/Mentsu.js | 71 ------------ src/models/Mentsu.ts | 10 +- src/models/Tatsu.js | 77 ------------- src/models/Tatsu.ts | 20 +++- src/models/Tile.js | 203 ----------------------------------- src/models/Wind.js | 10 -- src/models/index.js | 23 ---- src/models/types.js | 2 - src/point-by-han-hu/index.js | 14 --- src/shanten/coder.js | 116 -------------------- src/shanten/index.js | 66 ------------ src/shanten/models.js | 2 - src/utils/obj-map.js | 17 --- 21 files changed, 48 insertions(+), 908 deletions(-) delete mode 100644 src/hora/coder.js delete mode 100644 src/hora/index.js delete mode 100644 src/hora/models.js delete mode 100644 src/hora/yaku.js delete mode 100644 src/models/Furo.js delete mode 100644 src/models/Mentsu.js delete mode 100644 src/models/Tatsu.js delete mode 100644 src/models/Tile.js delete mode 100644 src/models/Wind.js delete mode 100644 src/models/index.js delete mode 100644 src/models/types.js delete mode 100644 src/point-by-han-hu/index.js delete mode 100644 src/shanten/coder.js delete mode 100644 src/shanten/index.js delete mode 100644 src/shanten/models.js delete mode 100644 src/utils/obj-map.js diff --git a/jest.config.js b/jest.config.js index b413e10..537d1fb 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,4 +2,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + rootDir: "./src" }; \ No newline at end of file diff --git a/package.json b/package.json index 6410eb2..9d29b9e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "types": "dist/index.d.ts", "scripts": { "build": "tsc", - "test": "jest" + "test": "jest " }, "devDependencies": { "@types/jest": "^29.4.0", diff --git a/src/hora/coder.js b/src/hora/coder.js deleted file mode 100644 index d7ccdf7..0000000 --- a/src/hora/coder.js +++ /dev/null @@ -1,65 +0,0 @@ -"use strict"; -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; -exports.__esModule = true; -exports.decodeHora = exports.decodeHoraHandPattern = void 0; -var models_1 = require("../models"); -var decodeRegularHoraHandPattern = function (raw) { - return { - agari: models_1.Tile.decode(raw.agari), - tsumo: raw.tsumo, - hu: raw.hu, - selfWind: raw.selfWind, - roundWind: raw.roundWind, - k: raw.pattern.k, - jyantou: models_1.Tile.decode(raw.pattern.jyantou), - menzenMentsu: raw.pattern.menzenMentsu.map(models_1.Mentsu.decode), - furo: raw.pattern.furo.map(models_1.Furo.decode), - tatsu: raw.pattern.tatsu.map(models_1.Tatsu.decode), - remaining: raw.pattern.remaining.map(models_1.Tile.decode) - }; -}; -var decodeChitoiHoraHandPattern = function (raw) { - return { - agari: models_1.Tile.decode(raw.agari), - tsumo: raw.tsumo, - hu: 25, - selfWind: raw.selfWind, - roundWind: raw.roundWind, - pairs: raw.pairs.map(models_1.Tile.decode) - }; -}; -var decodeKokushiHoraHandPattern = function (raw) { - return { - agari: models_1.Tile.decode(raw.agari), - tsumo: raw.tsumo, - hu: 20, - selfWind: raw.selfWind, - roundWind: raw.roundWind, - repeated: models_1.Tile.decode(raw.repeated) - }; -}; -var decodeHoraHandPattern = function (raw) { - switch (raw.type) { - case 'RegularHoraHandPattern': - return __assign({ type: 'RegularHoraHandPattern' }, decodeRegularHoraHandPattern(raw)); - case 'ChitoiHoraHandPattern': - return __assign({ type: 'ChitoiHoraHandPattern' }, decodeChitoiHoraHandPattern(raw)); - case 'KokushiHoraHandPattern': - return __assign({ type: 'KokushiHoraHandPattern' }, decodeKokushiHoraHandPattern(raw)); - } -}; -exports.decodeHoraHandPattern = decodeHoraHandPattern; -var decodeHora = function (raw) { - return __assign(__assign({}, raw), { pattern: (0, exports.decodeHoraHandPattern)(raw.pattern) }); -}; -exports.decodeHora = decodeHora; diff --git a/src/hora/index.js b/src/hora/index.js deleted file mode 100644 index 1cfeee3..0000000 --- a/src/hora/index.js +++ /dev/null @@ -1,39 +0,0 @@ -"use strict"; -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); -}; -exports.__esModule = true; -exports.buildHora = void 0; -var entry_1 = require("../entry"); -var models_1 = require("../models"); -var coder_1 = require("../shanten/coder"); -var coder_2 = require("./coder"); -__exportStar(require("./models"), exports); -function buildHora(opts) { - var _a, _b; - var result = entry_1.ENTRY.call("hora", __assign(__assign({}, opts), { tiles: (_a = opts.tiles) === null || _a === void 0 ? void 0 : _a.map(models_1.Tile.encode), furo: (_b = opts.furo) === null || _b === void 0 ? void 0 : _b.map(models_1.Furo.encode), shantenResult: opts.shantenResult ? (0, coder_1.encodeShantenWithGot)(opts.shantenResult) : undefined, agari: models_1.Tile.encode(opts.agari) })); - return (0, coder_2.decodeHora)((0, entry_1.handleResult)(result)); -} -exports.buildHora = buildHora; diff --git a/src/hora/models.js b/src/hora/models.js deleted file mode 100644 index 0e34578..0000000 --- a/src/hora/models.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -exports.__esModule = true; diff --git a/src/hora/yaku.js b/src/hora/yaku.js deleted file mode 100644 index 6523ac6..0000000 --- a/src/hora/yaku.js +++ /dev/null @@ -1,77 +0,0 @@ -"use strict"; -var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { - if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { - if (ar || !(i in from)) { - if (!ar) ar = Array.prototype.slice.call(from, 0, i); - ar[i] = from[i]; - } - } - return to.concat(ar || Array.prototype.slice.call(from)); -}; -exports.__esModule = true; -exports.getAllYaku = exports.getAllExtraYakuman = exports.getAllExtraYaku = exports.getAllYakuman = exports.getAllCommonYaku = void 0; -function getAllCommonYaku() { - return ['Tsumo', - 'Pinhu', - 'Tanyao', - 'Ipe', - 'SelfWind', - 'RoundWind', - 'Haku', - 'Hatsu', - 'Chun', - 'Sanshoku', - 'Ittsu', - 'Chanta', - 'Chitoi', - 'Toitoi', - 'Sananko', - 'Honroto', - 'Sandoko', - 'Sankantsu', - 'Shosangen', - 'Honitsu', - 'Junchan', - 'Ryanpe', - 'Chinitsu']; -} -exports.getAllCommonYaku = getAllCommonYaku; -function getAllYakuman() { - return ['Tenhou', - 'Chihou', - 'Kokushi', - 'Suanko', - 'Daisangen', - 'Tsuiso', - 'Shousushi', - 'Lyuiso', - 'Chinroto', - 'Sukantsu', - 'Churen', - 'Daisushi', - 'SuankoTanki', - 'ChurenNineWaiting', - 'KokushiThirteenWaiting']; -} -exports.getAllYakuman = getAllYakuman; -function getAllExtraYaku() { - return ['Richi', - 'Ippatsu', - 'Rinshan', - 'Chankan', - 'Haitei', - 'Houtei', - 'WRichi', - 'Tenhou', - 'Chihou']; -} -exports.getAllExtraYaku = getAllExtraYaku; -function getAllExtraYakuman() { - return ['Tenhou', - 'Chihou']; -} -exports.getAllExtraYakuman = getAllExtraYakuman; -function getAllYaku() { - return __spreadArray(__spreadArray(__spreadArray([], getAllCommonYaku(), true), getAllYakuman(), true), getAllExtraYaku(), true); -} -exports.getAllYaku = getAllYaku; diff --git a/src/models/Furo.js b/src/models/Furo.js deleted file mode 100644 index 1460860..0000000 --- a/src/models/Furo.js +++ /dev/null @@ -1,120 +0,0 @@ -"use strict"; -var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { - if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { - if (ar || !(i in from)) { - if (!ar) ar = Array.prototype.slice.call(from, 0, i); - ar[i] = from[i]; - } - } - return to.concat(ar || Array.prototype.slice.call(from)); -}; -exports.__esModule = true; -exports.Furo = exports.FuroType = void 0; -var Tile_1 = require("./Tile"); -var FuroType; -(function (FuroType) { - FuroType["Chi"] = "Chi"; - FuroType["Pon"] = "Pon"; - FuroType["Minkan"] = "Minkan"; - FuroType["Ankan"] = "Ankan"; -})(FuroType = exports.FuroType || (exports.FuroType = {})); -var Furo = /** @class */ (function () { - function Furo(type, tile) { - this.type = type; - this.tile = tile; - } - Furo.parse = function (tiles, ankan) { - if (typeof tiles === 'string') { - var ankan_ = ankan; - var tiles_ = void 0; - if (tiles.length === 5 && tiles[0] == tiles[3] && tiles[0] == '0' && tiles[1] == tiles[2]) { - ankan_ = true; - tiles_ = Tile_1.Tile.parseTiles("".concat(tiles[1]).concat(tiles[1]).concat(tiles[1]).concat(tiles[1]).concat(tiles[4])); - } - else { - tiles_ = Tile_1.Tile.parseTiles(tiles); - } - if (tiles_ === undefined) { - return undefined; - } - return Furo.parse(tiles_, ankan_); - } - else { - if (tiles.length == 3) { - if (tiles[0] == tiles[1] && tiles[1] == tiles[2]) { - return new Furo(FuroType.Pon, tiles[0]); - } - else { - if (tiles.some(function (it) { return it.type === Tile_1.TileType.Z; })) { - return undefined; - } - var tiles_ = __spreadArray([], tiles, true); - tiles_.sort(function (a, b) { return a.compareTo(b); }); - if (tiles[1].distance(tiles[0]) === 1 && tiles[2].distance(tiles[1]) === 1) { - return new Furo(FuroType.Chi, tiles[0]); - } - else { - return undefined; - } - } - } - else if (tiles.length == 4) { - if (tiles[0] == tiles[1] && tiles[1] == tiles[2] && tiles[2] == tiles[3]) { - return ankan ? new Furo(FuroType.Ankan, tiles[0]) : new Furo(FuroType.Minkan, tiles[0]); - } - } - return undefined; - } - }; - Object.defineProperty(Furo.prototype, "tiles", { - get: function () { - switch (this.type) { - case FuroType.Chi: - return [this.tile, this.tile.advance(1), this.tile.advance(2)]; - case FuroType.Pon: - return [this.tile, this.tile, this.tile]; - case FuroType.Minkan: - case FuroType.Ankan: - return [this.tile, this.tile, this.tile, this.tile]; - } - }, - enumerable: false, - configurable: true - }); - Furo.prototype.toString = function () { - switch (this.type) { - case FuroType.Chi: - return "".concat(this.tile.num).concat(this.tile.num + 1).concat(this.tile.num + 2).concat(FuroType[this.type]).toLowerCase(); - case FuroType.Pon: - return "".concat(this.tile.num).concat(this.tile.num).concat(this.tile.num).concat(FuroType[this.type]).toLowerCase(); - case FuroType.Minkan: - return "".concat(this.tile.num).concat(this.tile.num).concat(this.tile.num).concat(this.tile.num).concat(FuroType[this.type]).toLowerCase(); - case FuroType.Ankan: - return "0".concat(this.tile.num).concat(this.tile.num, "0").concat(FuroType[this.type]).toLowerCase(); - } - }; - Furo.encode = function (data) { - var raw = { type: data.type, tile: Tile_1.Tile.encode(data.tile) }; - if (data.type === FuroType.Minkan) { - raw.type = "Kan"; - raw.ankan = false; - } - else if (data.type === FuroType.Ankan) { - raw.type = "Kan"; - raw.ankan = true; - } - return raw; - }; - Furo.decode = function (raw) { - var type; - if (raw.type === "Kan") { - type = raw.ankan ? FuroType.Ankan : FuroType.Minkan; - } - else { - type = raw.type; - } - return new Furo(type, Tile_1.Tile.decode(raw.tile)); - }; - return Furo; -}()); -exports.Furo = Furo; diff --git a/src/models/Furo.ts b/src/models/Furo.ts index 5595439..3e859de 100644 --- a/src/models/Furo.ts +++ b/src/models/Furo.ts @@ -1,3 +1,4 @@ +import { Mentsu, MentsuType } from "./Mentsu"; import { Tile, TileType } from "./Tile"; import { Decoder, Encoder } from "./types"; @@ -10,6 +11,14 @@ export enum FuroType { export class Furo { constructor(readonly type: FuroType, readonly tile: Tile) { + let valid = true + if (this.type === FuroType.Chi) { + valid &&= tile.num >= 1 && tile.num <= 7 && tile.type !== TileType.Z + } + + if (!valid) { + throw new Error(`${tile} cannot be the tile of ${type}`) + } } static encode: Encoder = (data) => { @@ -100,7 +109,17 @@ export class Furo { return `${this.tile.num}${this.tile.num}${this.tile.num}${this.tile.num}${FuroType[this.type]}`.toLowerCase() case FuroType.Ankan: return `0${this.tile.num}${this.tile.num}0${FuroType[this.type]}`.toLowerCase() + } + } + asMentsu(): Mentsu { + switch (this.type) { + case FuroType.Chi: + return new Mentsu(MentsuType.Shuntsu, this.tile) + case FuroType.Pon: + case FuroType.Minkan: + case FuroType.Ankan: + return new Mentsu(MentsuType.Kotsu, this.tile) } } } diff --git a/src/models/Mentsu.js b/src/models/Mentsu.js deleted file mode 100644 index b82b6a6..0000000 --- a/src/models/Mentsu.js +++ /dev/null @@ -1,71 +0,0 @@ -"use strict"; -exports.__esModule = true; -exports.Mentsu = exports.MentsuType = void 0; -var Tile_1 = require("./Tile"); -var MentsuType; -(function (MentsuType) { - MentsuType["Shuntsu"] = "Shuntsu"; - MentsuType["Kotsu"] = "Kotsu"; -})(MentsuType = exports.MentsuType || (exports.MentsuType = {})); -var Mentsu = /** @class */ (function () { - function Mentsu(type, tile) { - this.type = type; - this.tile = tile; - } - Object.defineProperty(Mentsu.prototype, "tiles", { - get: function () { - switch (this.type) { - case MentsuType.Kotsu: - return [this.tile, this.tile, this.tile]; - case MentsuType.Shuntsu: - return [this.tile, this.tile.advance(1), this.tile.advance(2)]; - } - }, - enumerable: false, - configurable: true - }); - Mentsu.prototype.toString = function () { - switch (this.type) { - case MentsuType.Kotsu: - return "".concat(this.tile.num).concat(this.tile.num).concat(this.tile.num).concat(this.tile.type); - case MentsuType.Shuntsu: - return "".concat(this.tile.num).concat(this.tile.num + 1).concat(this.tile.num + 2).concat(this.tile.type); - } - }; - Mentsu.parse = function (tiles) { - if (typeof tiles === 'string') { - var tiles_ = Tile_1.Tile.parseTiles(tiles); - if (tiles_ === undefined) { - return undefined; - } - return Mentsu.parse(tiles_); - } - else { - if (tiles.length !== 3) { - return undefined; - } - tiles.sort(function (a, b) { return a.compareTo(b); }); - if (tiles[0] === tiles[1] && tiles[1] === tiles[2]) { - return new Mentsu(MentsuType.Kotsu, tiles[0]); - } - else if (tiles[2].distance(tiles[1]) === 1 && tiles[1].distance(tiles[0]) === 1) { - return new Mentsu(MentsuType.Shuntsu, tiles[0]); - } - else { - return undefined; - } - } - }; - Mentsu.encode = function (data) { - return data.toString(); - }; - Mentsu.decode = function (raw) { - var mentsu = Mentsu.parse(raw); - if (mentsu === undefined) { - throw new Error("invalid value: " + raw); - } - return mentsu; - }; - return Mentsu; -}()); -exports.Mentsu = Mentsu; diff --git a/src/models/Mentsu.ts b/src/models/Mentsu.ts index 6ed1a48..c193d86 100644 --- a/src/models/Mentsu.ts +++ b/src/models/Mentsu.ts @@ -1,4 +1,4 @@ -import { Tile } from "./Tile"; +import { Tile, TileType } from "./Tile"; import { Decoder, Encoder } from "./types"; export enum MentsuType { @@ -8,6 +8,14 @@ export enum MentsuType { export class Mentsu { constructor(readonly type: MentsuType, readonly tile: Tile) { + let valid = true + if (this.type === MentsuType.Shuntsu) { + valid &&= tile.num >= 1 && tile.num <= 7 && tile.type !== TileType.Z + } + + if (!valid) { + throw new Error(`${tile} cannot be the tile of ${type}`) + } } static encode: Encoder = (data) => { diff --git a/src/models/Tatsu.js b/src/models/Tatsu.js deleted file mode 100644 index 9dd450e..0000000 --- a/src/models/Tatsu.js +++ /dev/null @@ -1,77 +0,0 @@ -"use strict"; -exports.__esModule = true; -exports.Tatsu = exports.TatsuType = void 0; -var Tile_1 = require("./Tile"); -var TatsuType; -(function (TatsuType) { - TatsuType["Ryanmen"] = "Ryanmen"; - TatsuType["Penchan"] = "Penchan"; - TatsuType["Kanchan"] = "Kanchan"; - TatsuType["Toitsu"] = "Toitsu"; -})(TatsuType = exports.TatsuType || (exports.TatsuType = {})); -var Tatsu = /** @class */ (function () { - function Tatsu(type, first) { - this.type = type; - this.first = first; - } - Object.defineProperty(Tatsu.prototype, "second", { - get: function () { - switch (this.type) { - case TatsuType.Ryanmen: - case TatsuType.Penchan: - return this.first.advance(1); - case TatsuType.Toitsu: - return this.first; - case TatsuType.Kanchan: - return this.first.advance(2); - } - }, - enumerable: false, - configurable: true - }); - Tatsu.prototype.toString = function () { - return "".concat(this.first.num).concat(this.second.num).concat(Tile_1.TileType[this.first.type]); - }; - Tatsu.parse = function (tiles) { - if (typeof tiles === 'string') { - var tiles_ = Tile_1.Tile.parseTiles(tiles); - if (tiles_ === undefined) { - return undefined; - } - return Tatsu.parse(tiles_); - } - else { - if (tiles.length !== 2) { - return undefined; - } - tiles.sort(function (a, b) { return a.compareTo(b); }); - switch (tiles[1].distance(tiles[0])) { - case 0: - return new Tatsu(TatsuType.Toitsu, tiles[0]); - case 1: - if (tiles[0].num === 1 || tiles[1].num === 9) { - return new Tatsu(TatsuType.Penchan, tiles[0]); - } - else { - return new Tatsu(TatsuType.Ryanmen, tiles[0]); - } - case 2: - return new Tatsu(TatsuType.Kanchan, tiles[0]); - default: - return undefined; - } - } - }; - Tatsu.encode = function (data) { - return data.toString(); - }; - Tatsu.decode = function (raw) { - var t = Tatsu.parse(raw); - if (t === undefined) { - throw new Error("invalid value: " + raw); - } - return t; - }; - return Tatsu; -}()); -exports.Tatsu = Tatsu; diff --git a/src/models/Tatsu.ts b/src/models/Tatsu.ts index 5403127..7f569cf 100644 --- a/src/models/Tatsu.ts +++ b/src/models/Tatsu.ts @@ -1,5 +1,5 @@ -import {Tile, TileType} from "./Tile"; -import {Decoder, Encoder} from "./types"; +import { Tile, TileType } from "./Tile"; +import { Decoder, Encoder } from "./types"; export enum TatsuType { Ryanmen = "Ryanmen", @@ -10,6 +10,22 @@ export enum TatsuType { export class Tatsu { constructor(readonly type: TatsuType, readonly first: Tile) { + let valid = true + switch (this.type) { + case TatsuType.Ryanmen: + valid &&= first.num >= 2 && first.num <= 7 && first.type !== TileType.Z + break + case TatsuType.Penchan: + valid &&= (first.num === 1 || first.num ===8) && first.type !== TileType.Z + break + case TatsuType.Kanchan: + valid &&= first.num >= 1 && first.num <= 7 && first.type !== TileType.Z + break + } + + if (!valid) { + throw new Error(`${first} cannot be the first tile of ${type}`) + } } static encode: Encoder = (data) => { diff --git a/src/models/Tile.js b/src/models/Tile.js deleted file mode 100644 index a7e8046..0000000 --- a/src/models/Tile.js +++ /dev/null @@ -1,203 +0,0 @@ -"use strict"; -var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { - if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { - if (ar || !(i in from)) { - if (!ar) ar = Array.prototype.slice.call(from, 0, i); - ar[i] = from[i]; - } - } - return to.concat(ar || Array.prototype.slice.call(from)); -}; -exports.__esModule = true; -exports.Tile = exports.TileType = void 0; -var TileType; -(function (TileType) { - TileType[TileType["M"] = 0] = "M"; - TileType[TileType["P"] = 1] = "P"; - TileType[TileType["S"] = 2] = "S"; - TileType[TileType["Z"] = 3] = "Z"; -})(TileType = exports.TileType || (exports.TileType = {})); -var Tile = /** @class */ (function () { - function Tile(type, num) { - this.type = type; - this.num = num; - } - Object.defineProperty(Tile.prototype, "realNum", { - get: function () { - if (this.type !== TileType.Z && this.num === 0) { - return 5; - } - else { - return this.num; - } - }, - enumerable: false, - configurable: true - }); - Object.defineProperty(Tile.prototype, "code", { - get: function () { - return this.type * 10 + this.num; - }, - enumerable: false, - configurable: true - }); - Tile.prototype.compareTo = function (other) { - if (this.type !== other.type) { - return this.type - other.type; - } - else if (this.num !== 0 && other.num !== 0) { - return this.num - other.num; - } - else if (this.num === 0 && other.num === 0) { - return 0; - } - else if (this.num === 0) { - return other.num > 5 ? -1 : 1; - } - else { - return other.num <= 5 ? -1 : 1; - } - }; - Tile.prototype.distance = function (other) { - var a = this.num === 0 ? Tile.byTypeAndNum(this.type, 5) : this; - var b = other.num === 0 ? Tile.byTypeAndNum(other.type, 5) : other; - return a.type === b.type ? a.num - b.num : Infinity; - }; - Tile.prototype.advance = function (step) { - return Tile.byTypeAndNum(this.type, this.num + step); - }; - Tile.prototype.toString = function () { - return "".concat(this.num).concat(TileType[this.type]); - }; - Tile.byTypeAndNum = function (type, num) { - var code = type * 10 + num; - var tile = Tile.pool[code]; - if (num < 0 || num > 9) { - return undefined; - } - return tile; - }; - Tile.byCode = function (code) { - var type = Math.floor(code / 10); - var num = code % 10; - if (type < 0 || type > 3) { - return undefined; - } - return Tile.byTypeAndNum(type, num); - }; - Tile.byText = function (text) { - if (text.length != 2) { - return undefined; - } - var type; - switch (text[1].toLowerCase()) { - case 'm': - type = TileType.M; - break; - case 'p': - type = TileType.P; - break; - case 's': - type = TileType.S; - break; - case 'z': - type = TileType.Z; - break; - default: - return undefined; - } - var num = parseInt(text[0]); - if (isNaN(num)) { - return undefined; - } - return Tile.byTypeAndNum(type, num); - }; - Tile.parseTiles = function (code) { - var pending = []; - var tiles = []; - var _loop_1 = function (c) { - if (c === 'M' || c === 'm' || c === 'P' || c === 'p' || c === 'S' || c === 's') { - tiles = __spreadArray(__spreadArray([], tiles, true), pending.map(function (value) { return Tile.byText("".concat(value).concat(c)); }), true); - pending = []; - } - else if (c === 'Z' || c === 'z') { - if (pending.every(function (value) { return value >= 1 && value <= 7; })) { - tiles = __spreadArray(__spreadArray([], tiles, true), pending.map(function (value) { return Tile.byText("".concat(value).concat(c)); }), true); - pending = []; - } - else { - return { value: undefined }; - } - } - else if (c >= '1' && c <= '9') { - pending.push(parseInt(c)); - } - else { - return { value: undefined }; - } - }; - for (var _i = 0, code_1 = code; _i < code_1.length; _i++) { - var c = code_1[_i]; - var state_1 = _loop_1(c); - if (typeof state_1 === "object") - return state_1.value; - } - if (pending.length > 0) { - return undefined; - } - return tiles; - }; - Tile.tilesToString = function (tiles, opts) { - if (opts === void 0) { opts = {}; } - var tiles_; - if (opts.sorted) { - tiles_ = __spreadArray([], tiles, true); - tiles_.sort(function (a, b) { return a.compareTo(b); }); - } - else { - tiles_ = tiles; - } - var result = ""; - var prev = null; - for (var _i = 0, tiles_1 = tiles_; _i < tiles_1.length; _i++) { - var t = tiles_1[_i]; - if (prev !== null && prev.type !== t.type) { - result += TileType[prev.type]; - } - result += t.num.toString(); - prev = t; - } - if (prev !== null) { - result += TileType[prev.type]; - } - return result; - }; - Tile.encode = function (data) { - return "".concat(data.num).concat(TileType[data.type]); - }; - Tile.decode = function (raw) { - var t = Tile.byText(raw); - if (t === undefined) { - throw new Error("invalid value: " + raw); - } - return t; - }; - Tile.pool = []; - (function () { - for (var i = 0; i <= 9; i++) { - Tile.pool.push(new Tile(TileType.M, i)); - } - for (var i = 0; i <= 9; i++) { - Tile.pool.push(new Tile(TileType.P, i)); - } - for (var i = 0; i <= 9; i++) { - Tile.pool.push(new Tile(TileType.S, i)); - } - Tile.pool.push(undefined); - for (var i = 1; i <= 7; i++) { - Tile.pool.push(new Tile(TileType.Z, i)); - } - })(); - return Tile; -}()); -exports.Tile = Tile; diff --git a/src/models/Wind.js b/src/models/Wind.js deleted file mode 100644 index 0008229..0000000 --- a/src/models/Wind.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; -exports.__esModule = true; -exports.Wind = void 0; -var Wind; -(function (Wind) { - Wind["East"] = "East"; - Wind["South"] = "South"; - Wind["West"] = "West"; - Wind["North"] = "North"; -})(Wind = exports.Wind || (exports.Wind = {})); diff --git a/src/models/index.js b/src/models/index.js deleted file mode 100644 index 38b68b3..0000000 --- a/src/models/index.js +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); -}; -exports.__esModule = true; -__exportStar(require("./Furo"), exports); -__exportStar(require("./hand/Hand"), exports); -__exportStar(require("./hand/HandPattern"), exports); -__exportStar(require("./Mentsu"), exports); -__exportStar(require("./Tatsu"), exports); -__exportStar(require("./Tile"), exports); -__exportStar(require("./Wind"), exports); diff --git a/src/models/types.js b/src/models/types.js deleted file mode 100644 index 0e34578..0000000 --- a/src/models/types.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -exports.__esModule = true; diff --git a/src/point-by-han-hu/index.js b/src/point-by-han-hu/index.js deleted file mode 100644 index afb252f..0000000 --- a/src/point-by-han-hu/index.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; -exports.__esModule = true; -exports.getChildPointByHanHu = exports.getParentPointByHanHu = void 0; -var entry_1 = require("../entry"); -function getParentPointByHanHu(han, hu) { - var result = entry_1.ENTRY.call("getParentPointByHanHu", { han: han, hu: hu }); - return (0, entry_1.handleResult)(result); -} -exports.getParentPointByHanHu = getParentPointByHanHu; -function getChildPointByHanHu(han, hu) { - var result = entry_1.ENTRY.call("getChildPointByHanHu", { han: han, hu: hu }); - return (0, entry_1.handleResult)(result); -} -exports.getChildPointByHanHu = getChildPointByHanHu; diff --git a/src/shanten/coder.js b/src/shanten/coder.js deleted file mode 100644 index ca09686..0000000 --- a/src/shanten/coder.js +++ /dev/null @@ -1,116 +0,0 @@ -"use strict"; -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; -exports.__esModule = true; -exports.decodeShantenResult = exports.encodeShantenInfo = exports.decodeShantenInfo = exports.decodeShantenWithFuroChance = exports.encodeShantenWithGot = exports.decodeShantenWithGot = exports.encodeShantenWithoutGot = exports.decodeShantenWithoutGot = void 0; -var models_1 = require("../models"); -var obj_map_1 = require("../utils/obj-map"); -var coder_1 = require("../models/hand/coder"); -var decodeShantenWithoutGot = function (raw) { - var _a, _b, _c; - return { - type: 'ShantenWithoutGot', - shantenNum: raw.shantenNum, - advance: raw.advance.map(models_1.Tile.decode), - advanceNum: (_a = raw.advanceNum) !== null && _a !== void 0 ? _a : undefined, - goodShapeAdvance: (_b = raw.goodShapeAdvance) === null || _b === void 0 ? void 0 : _b.map(models_1.Tile.decode), - goodShapeAdvanceNum: (_c = raw.goodShapeAdvanceNum) !== null && _c !== void 0 ? _c : undefined - }; -}; -exports.decodeShantenWithoutGot = decodeShantenWithoutGot; -var encodeShantenWithoutGot = function (data) { - var _a, _b, _c, _d; - return { - shantenNum: data.shantenNum, - advance: data.advance.map(models_1.Tile.encode), - advanceNum: (_a = data.advanceNum) !== null && _a !== void 0 ? _a : null, - goodShapeAdvance: (_c = (_b = data.goodShapeAdvance) === null || _b === void 0 ? void 0 : _b.map(models_1.Tile.encode)) !== null && _c !== void 0 ? _c : null, - goodShapeAdvanceNum: (_d = data.goodShapeAdvanceNum) !== null && _d !== void 0 ? _d : null - }; -}; -exports.encodeShantenWithoutGot = encodeShantenWithoutGot; -var decodeShantenWithGot = function (raw) { - return { - type: 'ShantenWithGot', - shantenNum: raw.shantenNum, - discardToAdvance: (0, obj_map_1.objToMap)(raw.discardToAdvance, { - keyMapper: models_1.Tile.decode, - valueMapper: exports.decodeShantenWithoutGot - }), - ankanToAdvance: (0, obj_map_1.objToMap)(raw.ankanToAdvance, { - keyMapper: models_1.Tile.decode, - valueMapper: exports.decodeShantenWithoutGot - }) - }; -}; -exports.decodeShantenWithGot = decodeShantenWithGot; -var encodeShantenWithGot = function (data) { - return { - shantenNum: data.shantenNum, - discardToAdvance: (0, obj_map_1.mapToObj)(data.discardToAdvance, { - keyMapper: models_1.Tile.encode, - valueMapper: exports.encodeShantenWithoutGot - }), - ankanToAdvance: (0, obj_map_1.mapToObj)(data.ankanToAdvance, { - keyMapper: models_1.Tile.encode, - valueMapper: exports.encodeShantenWithoutGot - }) - }; -}; -exports.encodeShantenWithGot = encodeShantenWithGot; -var decodeShantenWithFuroChance = function (raw) { - return { - type: 'ShantenWithFuroChance', - shantenNum: raw.shantenNum, - pass: raw.pass !== null ? (0, exports.decodeShantenWithoutGot)(raw.pass) : undefined, - chi: Object.entries(raw.chi).map(function (_a) { - var key = _a[0], value = _a[1]; - return [models_1.Tatsu.decode(key), (0, exports.decodeShantenWithGot)(value)]; - }), - pon: raw.pon !== null ? (0, exports.decodeShantenWithGot)(raw.pon) : undefined, - minkan: raw.minkan !== null ? (0, exports.decodeShantenWithoutGot)(raw.minkan) : undefined - }; -}; -exports.decodeShantenWithFuroChance = decodeShantenWithFuroChance; -var decodeShantenInfo = function (raw) { - switch (raw.type) { - case 'ShantenWithGot': - return (0, exports.decodeShantenWithGot)(raw); - case 'ShantenWithoutGot': - return (0, exports.decodeShantenWithoutGot)(raw); - case 'ShantenWithFuroChance': - return (0, exports.decodeShantenWithFuroChance)(raw); - } -}; -exports.decodeShantenInfo = decodeShantenInfo; -var encodeShantenInfo = function (data) { - switch (data.type) { - case 'ShantenWithGot': - return __assign(__assign({}, (0, exports.encodeShantenWithGot)(data)), { type: 'ShantenWithGot' }); - case 'ShantenWithoutGot': - return __assign(__assign({}, (0, exports.encodeShantenWithoutGot)(data)), { type: 'ShantenWithoutGot' }); - default: - throw new Error("invalid type: " + data.type); - } -}; -exports.encodeShantenInfo = encodeShantenInfo; -var decodeShantenResult = function (raw) { - return { - type: raw.type, - hand: (0, coder_1.decodeHand)(raw.hand), - shantenInfo: (0, exports.decodeShantenInfo)(raw.shantenInfo), - regular: raw.regular ? (0, exports.decodeShantenResult)(raw.regular) : undefined, - chitoi: raw.chitoi ? (0, exports.decodeShantenResult)(raw.chitoi) : undefined, - kokushi: raw.kokushi ? (0, exports.decodeShantenResult)(raw.kokushi) : undefined - }; -}; -exports.decodeShantenResult = decodeShantenResult; diff --git a/src/shanten/index.js b/src/shanten/index.js deleted file mode 100644 index 3045755..0000000 --- a/src/shanten/index.js +++ /dev/null @@ -1,66 +0,0 @@ -"use strict"; -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); -}; -exports.__esModule = true; -exports.furoChanceShanten = exports.kokushiShanten = exports.chitoiShanten = exports.regularShanten = exports.shanten = void 0; -var entry_1 = require("../entry"); -var models_1 = require("../models"); -var coder_1 = require("./coder"); -__exportStar(require("./models"), exports); -function shanten(tiles, opts) { - var _a; - if (opts === void 0) { opts = {}; } - var result = entry_1.ENTRY.call("shanten", __assign(__assign({}, opts), { tiles: tiles.map(function (it) { return models_1.Tile.encode(it); }), furo: (_a = opts.furo) === null || _a === void 0 ? void 0 : _a.map(function (it) { return models_1.Furo.encode(it); }) })); - return (0, coder_1.decodeShantenResult)((0, entry_1.handleResult)(result)); -} -exports.shanten = shanten; -function regularShanten(tiles, opts) { - var _a; - if (opts === void 0) { opts = {}; } - var result = entry_1.ENTRY.call("regularShanten", __assign(__assign({}, opts), { tiles: tiles.map(function (it) { return models_1.Tile.encode(it); }), furo: (_a = opts.furo) === null || _a === void 0 ? void 0 : _a.map(function (it) { return models_1.Furo.encode(it); }) })); - return (0, coder_1.decodeShantenResult)((0, entry_1.handleResult)(result)); -} -exports.regularShanten = regularShanten; -function chitoiShanten(tiles, opts) { - var _a; - if (opts === void 0) { opts = {}; } - var result = entry_1.ENTRY.call("chitoiShanten", __assign(__assign({}, opts), { tiles: tiles.map(function (it) { return models_1.Tile.encode(it); }), furo: (_a = opts.furo) === null || _a === void 0 ? void 0 : _a.map(function (it) { return models_1.Furo.encode(it); }) })); - return (0, coder_1.decodeShantenResult)((0, entry_1.handleResult)(result)); -} -exports.chitoiShanten = chitoiShanten; -function kokushiShanten(tiles, opts) { - var _a; - if (opts === void 0) { opts = {}; } - var result = entry_1.ENTRY.call("kokushiShanten", __assign(__assign({}, opts), { tiles: tiles.map(function (it) { return models_1.Tile.encode(it); }), furo: (_a = opts.furo) === null || _a === void 0 ? void 0 : _a.map(function (it) { return models_1.Furo.encode(it); }) })); - return (0, coder_1.decodeShantenResult)((0, entry_1.handleResult)(result)); -} -exports.kokushiShanten = kokushiShanten; -function furoChanceShanten(tiles, chanceTile, opts) { - if (opts === void 0) { opts = {}; } - var result = entry_1.ENTRY.call("furoChanceShanten", __assign(__assign({}, opts), { tiles: tiles.map(function (it) { return models_1.Tile.encode(it); }), chanceTile: models_1.Tile.encode(chanceTile) })); - return (0, coder_1.decodeShantenResult)((0, entry_1.handleResult)(result)); -} -exports.furoChanceShanten = furoChanceShanten; diff --git a/src/shanten/models.js b/src/shanten/models.js deleted file mode 100644 index 0e34578..0000000 --- a/src/shanten/models.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -exports.__esModule = true; diff --git a/src/utils/obj-map.js b/src/utils/obj-map.js deleted file mode 100644 index f911555..0000000 --- a/src/utils/obj-map.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; -exports.__esModule = true; -exports.mapToObj = exports.objToMap = void 0; -function objToMap(obj, opts) { - return new Map(Object.keys(obj).map(function (key) { - return [opts.keyMapper(key), opts.valueMapper(obj[key])]; - })); -} -exports.objToMap = objToMap; -function mapToObj(map, opts) { - var obj = {}; - map.forEach(function (value, key) { - obj[opts.keyMapper(key)] = opts.valueMapper(value); - }); - return obj; -} -exports.mapToObj = mapToObj;