Skip to content

Instantly share code, notes, and snippets.

@hogashi

hogashi/random-mahjong.ts

Last active Jun 8, 2020
Embed
What would you like to do?
ランダム麻雀
const allHais = [
'一萬', '二萬', '三萬', '四萬', '五萬', '六萬', '七萬', '八萬', '九萬',
'一筒', '二筒', '三筒', '四筒', '五筒', '六筒', '七筒', '八筒', '九筒',
'一索', '二索', '三索', '四索', '五索', '六索', '七索', '八索', '九索',
'東 ', '南 ', '西 ', '北 ', '白 ', '発 ', '中 ',
] as const;
type HaiName = typeof allHais[number];
type HaiNameArray = HaiName[];
type HaiNameSetForShow = {
upper: string[];
lower: string[];
};
const show = (hais: HaiNameArray): void => {
const { upper, lower }: HaiNameSetForShow = hais.reduce(
(set: HaiNameSetForShow, hai: HaiName) => ({
upper: [...set.upper, hai[0]],
lower: [...set.lower, hai[1]],
}),
{ upper: [], lower: [] }
);
console.log(`${upper.join('')}\n${lower.join('')}`);
};
const [dragon, hiddenDragon, ...tehai] = [...allHais, ...allHais, ...allHais, ...allHais].sort(() => Math.random() - 0.5).slice(0, 16);
show([dragon, hiddenDragon]);
show(tehai.sort((a, b) => allHais.findIndex((v) => v === a) - allHais.findIndex((v) => v === b)));
$ deno run https://gist.githubusercontent.com/hogashi/e19701da1ff5c2934bab915d695978a7/raw/ecb5acc41ce74c1d72486af4a9e4edc88673f302/random-mahjong.ts
七六
萬索
四六八一二三五一三八南白白発
萬萬萬筒筒筒筒索索索
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.