Skip to content

Instantly share code, notes, and snippets.

@luooooob
Created April 14, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luooooob/146c1ef7d9cc8f83b32cb7246cb77054 to your computer and use it in GitHub Desktop.
Save luooooob/146c1ef7d9cc8f83b32cb7246cb77054 to your computer and use it in GitHub Desktop.
conversion of tenhon-style string and number array
// 牌面代码转天凤字符串
export function tilesToTenhon(tiles: number[]) {
const getType = (tile: number) => Math.floor((tile - 1) / 9)
const getNum = (tile: number) => (((tile - 1) % 9) + 1)
const m = tiles.filter(tile => getType(tile) == 0).map(tile => getNum(tile))
const p = tiles.filter(tile => getType(tile) == 1).map(tile => getNum(tile))
const s = tiles.filter(tile => getType(tile) == 2).map(tile => getNum(tile))
const z = tiles.filter(tile => getType(tile) == 3).map(tile => getNum(tile))
const mStr = m.length > 0 ? m.join("") + "m" : ""
const pStr = p.length > 0 ? p.join("") + "p" : ""
const sStr = s.length > 0 ? s.join("") + "s" : ""
const zStr = z.length > 0 ? z.join("") + "z" : ""
return [mStr, pStr, sStr, zStr].join("")
}
// 天凤字符串转牌面代码
export function tenhonToTiles(tenhon: string) {
const mMatch = tenhon.match(/^[1-9]*(?=m)/)
const pMatch = tenhon.match(/[^|m][1-9]*(?=m)/)
const sMatch = tenhon.match(/[^|m|p][1-9]*(?=m)/)
const zMatch = tenhon.match(/[^|m|p|s][1-9]*(?=m)/)
const mStr = mMatch ? mMatch[0] : ""
const pStr = pMatch ? pMatch[0] : ""
const sStr = sMatch ? sMatch[0] : ""
const zStr = zMatch ? zMatch[0] : ""
const m = mStr.split("").map(n => Number(n))
const p = pStr.split("").map(n => Number(n) + 9)
const s = sStr.split("").map(n => Number(n) + 18)
const z = sStr.split("").map(n => Number(n) + 27)
return [...m, ...p, ...s, ...z]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment