This commit is contained in:
syuilo 2024-01-29 17:15:09 +09:00
parent 668bf9a226
commit d427d24ca4
7 changed files with 252 additions and 91 deletions

View file

@ -172,7 +172,7 @@ type EnvForCalcYaku = {
riichi: boolean;
};
const YAKU_DEFINITIONS = [{
export const YAKU_DEFINITIONS = [{
name: 'riichi',
fan: 1,
calc: (state: EnvForCalcYaku) => {

View file

@ -4,7 +4,7 @@
*/
import CRC32 from 'crc-32';
import { Tile, House, Huro, TILE_TYPES } from './common.js';
import { Tile, House, Huro, TILE_TYPES, YAKU_DEFINITIONS } from './common.js';
import * as Utils from './utils.js';
import { PlayerState } from './engine.player.js';
@ -60,48 +60,48 @@ export type MasterState = {
/**
*
*/
source: House;
callee: House;
/**
*
*/
targets: House[];
callers: House[];
} | null;
ponAsking: {
/**
*
*/
source: House;
callee: House;
/**
*
*/
target: House;
caller: House;
} | null;
ciiAsking: {
/**
*
*/
source: House;
callee: House;
/**
* (sourceの下家なのは自明だがプログラム簡略化のため)
* (calleeの下家なのは自明だがプログラム簡略化のため)
*/
target: House;
caller: House;
} | null;
kanAsking: {
/**
*
*/
source: House;
callee: House;
/**
*
*/
target: House;
caller: House;
} | null;
};
@ -113,10 +113,18 @@ export class MasterGameEngine {
}
public static createInitialState(): MasterState {
const ikasama: Tile[] = ['haku', 'm2', 'm3', 'p5', 'p6', 'p7', 's2', 's3', 's4', 'chun', 'chun', 'chun', 'n', 'n'];
const tiles = [...TILE_TYPES.slice(), ...TILE_TYPES.slice(), ...TILE_TYPES.slice(), ...TILE_TYPES.slice()];
tiles.sort(() => Math.random() - 0.5);
const eHandTiles = tiles.splice(0, 14);
for (const tile of ikasama) {
const index = tiles.indexOf(tile);
tiles.splice(index, 1);
}
//const eHandTiles = tiles.splice(0, 14);
const eHandTiles = ikasama;
const sHandTiles = tiles.splice(0, 13);
const wHandTiles = tiles.splice(0, 13);
const nHandTiles = tiles.splice(0, 13);
@ -205,11 +213,30 @@ export class MasterGameEngine {
}
private endKyoku() {
console.log('endKyoku');
const newState = MasterGameEngine.createInitialState();
newState.kyoku = this.state.kyoku + 1;
newState.points = this.state.points;
}
private ron(callers: House[], callee: House) {
for (const house of callers) {
const yakus = YAKU_DEFINITIONS.filter(yaku => yaku.calc({
house: house,
handTiles: this.state.handTiles[house],
huros: this.state.huros[house],
tsumoTile: null,
ronTile: this.state.hoTiles[callee].at(-1)!,
riichi: this.state.riichis[house],
}));
console.log('yakus', yakus);
}
this.endKyoku();
// TODO: 役情報を返す
}
public commit_dahai(house: House, tile: Tile, riichi = false) {
if (this.state.turn !== house) throw new Error('Not your turn');
@ -280,26 +307,26 @@ export class MasterGameEngine {
if (canRonHouses.length > 0 || canPonHouse != null) {
if (canRonHouses.length > 0) {
this.state.ronAsking = {
source: house,
targets: canRonHouses,
callee: house,
callers: canRonHouses,
};
}
if (canKanHouse != null) {
this.state.kanAsking = {
source: house,
target: canKanHouse,
callee: house,
caller: canKanHouse,
};
}
if (canPonHouse != null) {
this.state.ponAsking = {
source: house,
target: canPonHouse,
callee: house,
caller: canPonHouse,
};
}
if (canCiiHouse != null) {
this.state.ciiAsking = {
source: house,
target: canCiiHouse,
callee: house,
caller: canCiiHouse,
};
}
this.state.turn = null;
@ -354,58 +381,57 @@ export class MasterGameEngine {
};
if (this.state.ronAsking != null && answers.ron.length > 0) {
// TODO
this.endKyoku();
this.ron(answers.ron, this.state.ronAsking.callee);
return {
type: 'endKyoku',
type: 'ronned',
};
}
if (this.state.kanAsking != null && answers.kan) {
const source = this.state.kanAsking.source;
const target = this.state.kanAsking.target;
const caller = this.state.kanAsking.caller;
const callee = this.state.kanAsking.callee;
const tile = this.state.hoTiles[source].pop()!;
this.state.huros[target].push({ type: 'minkan', tile, from: source });
const tile = this.state.hoTiles[callee].pop()!;
this.state.huros[caller].push({ type: 'minkan', tile, from: callee });
clearAsking();
this.state.turn = target;
this.state.turn = caller;
// TODO
return;
}
if (this.state.ponAsking != null && answers.pon) {
const source = this.state.ponAsking.source;
const target = this.state.ponAsking.target;
const caller = this.state.ponAsking.caller;
const callee = this.state.ponAsking.callee;
const tile = this.state.hoTiles[source].pop()!;
this.state.handTiles[target].splice(this.state.handTiles[target].indexOf(tile), 1);
this.state.handTiles[target].splice(this.state.handTiles[target].indexOf(tile), 1);
this.state.huros[target].push({ type: 'pon', tile, from: source });
const tile = this.state.hoTiles[callee].pop()!;
this.state.handTiles[caller].splice(this.state.handTiles[caller].indexOf(tile), 1);
this.state.handTiles[caller].splice(this.state.handTiles[caller].indexOf(tile), 1);
this.state.huros[caller].push({ type: 'pon', tile, from: callee });
clearAsking();
this.state.turn = target;
this.state.turn = caller;
return {
type: 'ponned',
source,
target,
caller,
callee,
tile,
};
}
if (this.state.ciiAsking != null && answers.cii) {
const source = this.state.ciiAsking.source;
const target = this.state.ciiAsking.target;
const caller = this.state.ciiAsking.caller;
const callee = this.state.ciiAsking.callee;
const tile = this.state.hoTiles[source].pop()!;
this.state.huros[target].push({ type: 'cii', tile, from: source });
const tile = this.state.hoTiles[callee].pop()!;
this.state.huros[caller].push({ type: 'cii', tile, from: callee });
clearAsking();
this.state.turn = target;
this.state.turn = caller;
return {
type: 'ciied',
source,
target,
caller,
callee,
tile,
};
}

View file

@ -4,7 +4,7 @@
*/
import CRC32 from 'crc-32';
import { Tile, House, Huro, TILE_TYPES } from './common.js';
import { Tile, House, Huro, TILE_TYPES, YAKU_DEFINITIONS } from './common.js';
import * as Utils from './utils.js';
export type PlayerState = {
@ -96,6 +96,7 @@ export class PlayerGameEngine {
public commit_tsumo(house: House, tile: Tile) {
console.log('commit_tsumo', this.state.turn, house, tile);
this.state.tilesCount--;
this.state.turn = house;
if (house === this.myHouse) {
this.myHandTiles.push(tile);
@ -141,51 +142,73 @@ export class PlayerGameEngine {
public commit_hora(house: House) {
console.log('commit_hora', this.state.turn, house);
if (this.state.turn !== house) throw new PlayerGameEngine.InvalidOperationError();
// TODO: ツモした人の手牌情報を貰う必要がある
}
/**
*
* @param source
* @param target
* @param callers
* @param callee
*/
public commit_ron(source: House, target: House) {
public commit_ron(callers: House[], callee: House) {
console.log('commit_ron', this.state.turn, callers, callee);
this.state.canRonSource = null;
const lastTile = this.state.hoTiles[source].pop();
if (lastTile == null) throw new PlayerGameEngine.InvalidOperationError();
if (target === this.myHouse) {
this.myHandTiles.push(lastTile);
} else {
this.state.handTiles[target].push(null);
// TODO: ロンした人の手牌情報を貰う必要がある
for (const house of callers) {
const yakus = YAKU_DEFINITIONS.filter(yaku => yaku.calc({
house: house,
handTiles: this.state.handTiles[house],
huros: this.state.huros[house],
tsumoTile: null,
ronTile: this.state.hoTiles[callee].at(-1)!,
riichi: this.state.riichis[house],
}));
console.log('yakus', yakus);
}
this.state.turn = null;
}
/**
*
* @param source
* @param target
* @param caller
* @param callee
*/
public commit_pon(source: House, target: House) {
public commit_pon(caller: House, callee: House) {
this.state.canPonSource = null;
const lastTile = this.state.hoTiles[source].pop();
const lastTile = this.state.hoTiles[callee].pop();
if (lastTile == null) throw new PlayerGameEngine.InvalidOperationError();
if (target === this.myHouse) {
if (caller === this.myHouse) {
this.myHandTiles.splice(this.myHandTiles.indexOf(lastTile), 1);
this.myHandTiles.splice(this.myHandTiles.indexOf(lastTile), 1);
} else {
this.state.handTiles[target].unshift();
this.state.handTiles[target].unshift();
this.state.handTiles[caller].unshift();
this.state.handTiles[caller].unshift();
}
this.state.huros[target].push({ type: 'pon', tile: lastTile, from: source });
this.state.huros[caller].push({ type: 'pon', tile: lastTile, from: callee });
this.state.turn = target;
this.state.turn = caller;
}
public commit_nop() {
this.state.canRonSource = null;
this.state.canPonSource = null;
}
public get isMenzen(): boolean {
const calls = ['pon', 'cii', 'minkan'];
return this.state.huros[this.myHouse].filter(h => calls.includes(h.type)).length === 0;
}
public canRiichi(): boolean {
if (this.state.turn !== this.myHouse) return false;
if (this.state.riichis[this.myHouse]) return false;
if (this.state.points[this.myHouse] < 1000) return false;
if (!this.isMenzen) return false;
if (Utils.getTilesForRiichi(this.myHandTiles).length === 0) return false;
return true;
}
}