1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-31 05:43:57 +02:00
This commit is contained in:
syuilo
2024-02-05 15:02:43 +09:00
parent b785793e41
commit b32022c20c
14 changed files with 695 additions and 374 deletions

View File

@@ -52,6 +52,7 @@ export type TileInstance = {
export type TileId = number;
// NOTE: 0 は"不明"(他プレイヤーの手牌など)を表すものとして予約されている
export const TILE_ID_MAP = new Map<TileId, TileInstance>([
/* eslint-disable no-multi-spaces */
[1, { t: 'm1' }], [2, { t: 'm1' }], [3, { t: 'm1' }], [4, { t: 'm1' }],
@@ -288,18 +289,21 @@ type EnvForCalcYaku = {
export const YAKU_DEFINITIONS = [{
name: 'riichi',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.riichi;
},
}, {
name: 'tsumo',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.tsumoTile != null;
},
}, {
name: 'red',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return (
(state.handTiles.filter(t => t === 'chun').length >= 3) ||
@@ -313,6 +317,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'white',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return (
(state.handTiles.filter(t => t === 'haku').length >= 3) ||
@@ -326,6 +331,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'green',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return (
(state.handTiles.filter(t => t === 'hatsu').length >= 3) ||
@@ -339,6 +345,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'field-wind-e',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.fieldWind === 'e' && (
(state.handTiles.filter(t => t === 'e').length >= 3) ||
@@ -352,6 +359,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'field-wind-s',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.fieldWind === 's' && (
(state.handTiles.filter(t => t === 's').length >= 3) ||
@@ -365,6 +373,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'seat-wind-e',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.house === 'e' && (
(state.handTiles.filter(t => t === 'e').length >= 3) ||
@@ -378,6 +387,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'seat-wind-s',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.house === 's' && (
(state.handTiles.filter(t => t === 's').length >= 3) ||
@@ -391,6 +401,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'seat-wind-w',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.house === 'w' && (
(state.handTiles.filter(t => t === 'w').length >= 3) ||
@@ -404,6 +415,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'seat-wind-n',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
return state.house === 'n' && (
(state.handTiles.filter(t => t === 'n').length >= 3) ||
@@ -417,6 +429,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'tanyao',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
const yaochuTiles: TileType[] = ['m1', 'm9', 'p1', 'p9', 's1', 's9', 'e', 's', 'w', 'n', 'haku', 'hatsu', 'chun'];
return (
@@ -432,6 +445,7 @@ export const YAKU_DEFINITIONS = [{
}, {
name: 'pinfu',
fan: 1,
isYakuman: false,
calc: (state: EnvForCalcYaku) => {
// 面前じゃないとダメ
if (state.huros.length !== 0) return false;
@@ -494,6 +508,19 @@ export function calcOwnedDoraCount(handTiles: TileType[], huros: Huro[], doras:
return count;
}
export function calcRedDoraCount(handTiles: TileId[], huros: Huro[]): number {
let count = 0;
for (const t of handTiles) {
if (findTileByIdOrFail(t).red) count++;
}
for (const huro of huros) {
for (const t of huro.tiles) {
if (findTileByIdOrFail(t).red) count++;
}
}
return count;
}
export function calcTsumoHoraPointDeltas(house: House, fans: number): Record<House, number> {
const isParent = house === 'e';
@@ -534,13 +561,18 @@ export function isTile(tile: string): tile is TileType {
return TILE_TYPES.includes(tile as TileType);
}
export function sortTiles(tiles: TileType[]): TileType[] {
tiles.sort((a, b) => {
export function sortTiles(tiles: TileId[]): TileId[] {
return tiles.toSorted((a, b) => {
return a - b;
});
}
export function sortTileTypes(tiles: TileType[]): TileType[] {
return tiles.toSorted((a, b) => {
const aIndex = TILE_TYPES.indexOf(a);
const bIndex = TILE_TYPES.indexOf(b);
return aIndex - bIndex;
});
return tiles;
}
export function nextHouse(house: House): House {

View File

@@ -67,58 +67,59 @@ export type MasterState = {
};
turn: House | null;
nextTurnAfterAsking: House | null;
askings: {
ron: {
/**
* 牌を捨てた人
*/
callee: House;
ronAsking: {
/**
* 牌を捨てた人
*/
callee: House;
/**
* ロンする権利がある人
*/
callers: House[];
} | null;
/**
* ロンする権利がある人
*/
callers: House[];
} | null;
pon: {
/**
* 牌を捨てた人
*/
callee: House;
ponAsking: {
/**
* 牌を捨てた人
*/
callee: House;
/**
* ポンする権利がある人
*/
caller: House;
} | null;
/**
* ポンする権利がある人
*/
caller: House;
} | null;
cii: {
/**
* 牌を捨てた人
*/
callee: House;
ciiAsking: {
/**
* 牌を捨てた人
*/
callee: House;
/**
* チーする権利がある人(calleeの下家なのは自明だがプログラム簡略化のため)
*/
caller: House;
} | null;
/**
* チーする権利がある人(calleeの下家なのは自明だがプログラム簡略化のため)
*/
caller: House;
} | null;
kan: {
/**
* 牌を捨てた人
*/
callee: House;
kanAsking: {
/**
* 牌を捨てた人
*/
callee: House;
/**
* カンする権利がある人
*/
caller: House;
} | null;
/**
* カンする権利がある人
*/
caller: House;
} | null;
};
};
export class MasterGameEngine {
public state: MasterState;
private state: MasterState;
constructor(state: MasterState) {
this.state = state;
@@ -129,6 +130,10 @@ export class MasterGameEngine {
.map(id => Common.nextTileForDora($type(id)));
}
public get handTiles(): Record<House, TileId[]> {
return this.state.handTiles;
}
public get handTileTypes(): Record<House, TileType[]> {
return {
e: this.state.handTiles.e.map(id => $type(id)),
@@ -147,6 +152,30 @@ export class MasterGameEngine {
};
}
public get riichis(): Record<House, boolean> {
return this.state.riichis;
}
public get askings(): MasterState['askings'] {
return this.state.askings;
}
public get user1House(): House {
return this.state.user1House;
}
public get user2House(): House {
return this.state.user2House;
}
public get user3House(): House {
return this.state.user3House;
}
public get user4House(): House {
return this.state.user4House;
}
public static createInitialState(): MasterState {
const ikasama: TileId[] = [125, 129, 9, 56, 57, 61, 77, 81, 85, 133, 134, 135, 121, 122];
@@ -207,10 +236,12 @@ export class MasterGameEngine {
},
turn: 'e',
nextTurnAfterAsking: null,
ponAsking: null,
ciiAsking: null,
kanAsking: null,
ronAsking: null,
askings: {
ron: null,
pon: null,
cii: null,
kan: null,
},
};
}
@@ -280,8 +311,9 @@ export class MasterGameEngine {
ronTile: this.hoTileTypes[callee].at(-1)!,
riichi: this.state.riichis[house],
}));
const doraCount = Common.calcOwnedDoraCount(this.handTileTypes[house], this.state.huros[house], this.doras);
// TODO: 赤ドラ
const doraCount =
Common.calcOwnedDoraCount(this.handTileTypes[house], this.state.huros[house], this.doras) +
Common.calcRedDoraCount(this.state.handTiles[house], this.state.huros[house]);
const fans = yakus.map(yaku => yaku.fan).reduce((a, b) => a + b, 0) + doraCount;
const point = Common.fanToPoint(fans, house === 'e');
this.state.points[callee] -= point;
@@ -356,25 +388,25 @@ export class MasterGameEngine {
if (canRonHouses.length > 0 || canPonHouse != null || canCiiHouse != null) {
if (canRonHouses.length > 0) {
this.state.ronAsking = {
this.state.askings.ron = {
callee: house,
callers: canRonHouses,
};
}
if (canKanHouse != null) {
this.state.kanAsking = {
this.state.askings.kan = {
callee: house,
caller: canKanHouse,
};
}
if (canPonHouse != null) {
this.state.ponAsking = {
this.state.askings.pon = {
callee: house,
caller: canPonHouse,
};
}
if (canCiiHouse != null) {
this.state.ciiAsking = {
this.state.askings.cii = {
callee: house,
caller: canCiiHouse,
};
@@ -460,8 +492,9 @@ export class MasterGameEngine {
ronTile: null,
riichi: this.state.riichis[house],
}));
const doraCount = Common.calcOwnedDoraCount(this.handTileTypes[house], this.state.huros[house], this.doras);
// TODO: 赤ドラ
const doraCount =
Common.calcOwnedDoraCount(this.handTileTypes[house], this.state.huros[house], this.doras) +
Common.calcRedDoraCount(this.state.handTiles[house], this.state.huros[house]);
const fans = yakus.map(yaku => yaku.fan).reduce((a, b) => a + b, 0) + doraCount;
const pointDeltas = Common.calcTsumoHoraPointDeltas(house, fans);
this.state.points.e += pointDeltas.e;
@@ -484,17 +517,17 @@ export class MasterGameEngine {
kan: boolean;
ron: House[];
}) {
if (this.state.ponAsking == null && this.state.ciiAsking == null && this.state.kanAsking == null && this.state.ronAsking == null) throw new Error();
if (this.state.askings.pon == null && this.state.askings.cii == null && this.state.askings.kan == null && this.state.askings.ron == null) throw new Error();
const pon = this.state.ponAsking;
const cii = this.state.ciiAsking;
const kan = this.state.kanAsking;
const ron = this.state.ronAsking;
const pon = this.state.askings.pon;
const cii = this.state.askings.cii;
const kan = this.state.askings.kan;
const ron = this.state.askings.ron;
this.state.ponAsking = null;
this.state.ciiAsking = null;
this.state.kanAsking = null;
this.state.ronAsking = null;
this.state.askings.pon = null;
this.state.askings.cii = null;
this.state.askings.kan = null;
this.state.askings.ron = null;
if (ron != null && answers.ron.length > 0) {
this.ronHora(answers.ron, ron.callee);
@@ -657,10 +690,10 @@ export class MasterGameEngine {
tilesCount: this.state.tiles.length,
doraIndicateTiles: this.state.kingTiles.slice(0, this.state.activatedDorasCount),
handTiles: {
e: house === 'e' ? this.state.handTiles.e : this.state.handTiles.e.map(() => null),
s: house === 's' ? this.state.handTiles.s : this.state.handTiles.s.map(() => null),
w: house === 'w' ? this.state.handTiles.w : this.state.handTiles.w.map(() => null),
n: house === 'n' ? this.state.handTiles.n : this.state.handTiles.n.map(() => null),
e: house === 'e' ? this.state.handTiles.e : this.state.handTiles.e.map(() => 0),
s: house === 's' ? this.state.handTiles.s : this.state.handTiles.s.map(() => 0),
w: house === 'w' ? this.state.handTiles.w : this.state.handTiles.w.map(() => 0),
n: house === 'n' ? this.state.handTiles.n : this.state.handTiles.n.map(() => 0),
},
hoTiles: {
e: this.state.hoTiles.e,
@@ -706,4 +739,8 @@ export class MasterGameEngine {
public calcCrc32ForUser4(): number {
// TODO
}
public getState(): MasterState {
return structuredClone(this.state);
}
}

View File

@@ -33,10 +33,10 @@ export type PlayerState = {
* 副露した牌を含まない手牌
*/
handTiles: {
e: TileId[] | null[];
s: TileId[] | null[];
w: TileId[] | null[];
n: TileId[] | null[];
e: TileId[] | 0[];
s: TileId[] | 0[];
w: TileId[] | 0[];
n: TileId[] | 0[];
};
hoTiles: {
@@ -72,7 +72,7 @@ export type PlayerState = {
};
export type KyokuResult = {
yakus: { name: string; fan: number; }[];
yakus: { name: string; fan: number; isYakuman: boolean; }[];
doraCount: number;
pointDeltas: { e: number; s: number; w: number; n: number; };
};
@@ -84,13 +84,53 @@ export class PlayerGameEngine {
public static InvalidOperationError = class extends Error {};
private myUserNumber: 1 | 2 | 3 | 4;
public state: PlayerState;
private state: PlayerState;
constructor(myUserNumber: PlayerGameEngine['myUserNumber'], state: PlayerState) {
this.myUserNumber = myUserNumber;
this.state = state;
}
public get doras(): TileType[] {
return this.state.doraIndicateTiles.map(t => Common.nextTileForDora($type(t)));
}
public get points(): Record<House, number> {
return this.state.points;
}
public get handTiles(): Record<House, number[]> {
return this.state.handTiles;
}
public get hoTiles(): Record<House, number[]> {
return this.state.hoTiles;
}
public get huros(): Record<House, Huro[]> {
return this.state.huros;
}
public get tilesCount(): number {
return this.state.tilesCount;
}
public get canRon(): PlayerState['canRon'] {
return this.state.canRon;
}
public get canPon(): PlayerState['canPon'] {
return this.state.canPon;
}
public get canKan(): PlayerState['canKan'] {
return this.state.canKan;
}
public get canCii(): PlayerState['canCii'] {
return this.state.canCii;
}
public get myHouse(): House {
switch (this.myUserNumber) {
case 1: return this.state.user1House;
@@ -112,10 +152,6 @@ export class PlayerGameEngine {
return this.state.riichis[this.myHouse];
}
public get doras(): TileType[] {
return this.state.doraIndicateTiles.map(t => Common.nextTileForDora($type(t)));
}
public commit_tsumo(house: House, tile: TileId) {
console.log('commit_tsumo', this.state.turn, house, tile);
this.state.tilesCount--;
@@ -123,7 +159,7 @@ export class PlayerGameEngine {
if (house === this.myHouse) {
this.myHandTiles.push(tile);
} else {
this.state.handTiles[house].push(null);
this.state.handTiles[house].push(0);
}
}
@@ -148,9 +184,9 @@ export class PlayerGameEngine {
if (house === this.myHouse) {
} else {
const canRon = Common.getHoraSets(this.myHandTiles.concat(tile).map(id => $type(id))).length > 0;
const canPon = this.myHandTiles.filter(t => t === tile).length === 2;
const canKan = this.myHandTiles.filter(t => t === tile).length === 3;
const canCii = house === Common.prevHouse(this.myHouse) &&
const canPon = !this.isMeRiichi && this.myHandTiles.filter(t => t === tile).length === 2;
const canKan = !this.isMeRiichi && this.myHandTiles.filter(t => t === tile).length === 3;
const canCii = !this.isMeRiichi && house === Common.prevHouse(this.myHouse) &&
Common.SHUNTU_PATTERNS.some(pattern =>
pattern.includes($type(tile)) &&
pattern.filter(t => this.myHandTileTypes.includes(t)).length >= 2);
@@ -173,8 +209,9 @@ export class PlayerGameEngine {
ronTile: null,
riichi: this.state.riichis[house],
}));
const doraCount = Common.calcOwnedDoraCount(handTiles.map(id => $type(id)), this.state.huros[house], this.doras);
// TODO: 赤ドラ
const doraCount =
Common.calcOwnedDoraCount(handTiles.map(id => $type(id)), this.state.huros[house], this.doras) +
Common.calcRedDoraCount(handTiles, this.state.huros[house]);
const fans = yakus.map(yaku => yaku.fan).reduce((a, b) => a + b, 0) + doraCount;
const pointDeltas = Common.calcTsumoHoraPointDeltas(house, fans);
this.state.points.e += pointDeltas.e;
@@ -186,6 +223,7 @@ export class PlayerGameEngine {
yakus: yakus.map(yaku => ({
name: yaku.name,
fan: yaku.fan,
isYakuman: yaku.isYakuman,
})),
doraCount,
pointDeltas,
@@ -223,13 +261,14 @@ export class PlayerGameEngine {
ronTile: $type(this.state.hoTiles[callee].at(-1)!),
riichi: this.state.riichis[house],
}));
const doraCount = Common.calcOwnedDoraCount(handTiles[house].map(id => $type(id)), this.state.huros[house], this.doras);
// TODO: 赤ドラ
const doraCount =
Common.calcOwnedDoraCount(handTiles[house].map(id => $type(id)), this.state.huros[house], this.doras) +
Common.calcRedDoraCount(handTiles[house], this.state.huros[house]);
const fans = yakus.map(yaku => yaku.fan).reduce((a, b) => a + b, 0) + doraCount;
const point = Common.fanToPoint(fans, house === 'e');
this.state.points[callee] -= point;
this.state.points[house] += point;
resultMap[house].yakus = yakus.map(yaku => ({ name: yaku.name, fan: yaku.fan }));
resultMap[house].yakus = yakus.map(yaku => ({ name: yaku.name, fan: yaku.fan, isYakuman: yaku.isYakuman }));
resultMap[house].doraCount = doraCount;
resultMap[house].pointDeltas[callee] = -point;
resultMap[house].pointDeltas[house] = point;
@@ -297,6 +336,28 @@ export class PlayerGameEngine {
console.log('commit_kakan', this.state.turn, house, tiles);
}
/**
* チーします
* @param caller チーした人
* @param callee 牌を捨てた人
*/
public commit_cii(caller: House, callee: House, tiles: TileId[]) {
this.state.canCii = null;
this.state.hoTiles[callee].pop();
if (caller === this.myHouse) {
if (this.myHandTiles.includes(tiles[0])) this.myHandTiles.splice(this.myHandTiles.indexOf(tiles[0]), 1);
if (this.myHandTiles.includes(tiles[1])) this.myHandTiles.splice(this.myHandTiles.indexOf(tiles[1]), 1);
if (this.myHandTiles.includes(tiles[2])) this.myHandTiles.splice(this.myHandTiles.indexOf(tiles[2]), 1);
} else {
this.state.handTiles[caller].unshift();
this.state.handTiles[caller].unshift();
}
this.state.huros[caller].push({ type: 'cii', tiles: tiles, from: callee });
this.state.turn = caller;
}
public commit_nop() {
this.state.canRon = null;
this.state.canPon = null;
@@ -337,4 +398,8 @@ export class PlayerGameEngine {
public getKakanableTiles(): TileId[] {
return this.myHandTiles.filter(t => this.state.huros[this.myHouse].some(h => h.type === 'pon' && $type(t) === $type(h.tiles[0])));
}
public getState(): PlayerState {
return structuredClone(this.state);
}
}