From 314c31db3491cda6c62135d7031da8d32958d836 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 17 Jan 2024 20:14:35 +0900 Subject: [PATCH] wip --- packages/frontend/src/scripts/mahjong.ts | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 packages/frontend/src/scripts/mahjong.ts diff --git a/packages/frontend/src/scripts/mahjong.ts b/packages/frontend/src/scripts/mahjong.ts new file mode 100644 index 0000000000..b3704fba74 --- /dev/null +++ b/packages/frontend/src/scripts/mahjong.ts @@ -0,0 +1,83 @@ +export const TILE_TYPES = [ + 'bamboo1', + 'bamboo2', + 'bamboo3', + 'bamboo4', + 'bamboo5', + 'bamboo6', + 'bamboo7', + 'bamboo8', + 'bamboo9', + 'character1', + 'character2', + 'character3', + 'character4', + 'character5', + 'character6', + 'character7', + 'character8', + 'character9', + 'circle1', + 'circle2', + 'circle3', + 'circle4', + 'circle5', + 'circle6', + 'circle7', + 'circle8', + 'circle9', + 'wind-east', + 'wind-south', + 'wind-west', + 'wind-north', + 'dragon-red', + 'dragon-green', + 'dragon-white', +]; + +type Player = 'east' | 'south' | 'west' | 'north'; + +export class MahjongGameForBackend { + public tiles: (typeof TILE_TYPES[number])[] = []; + public 場: (typeof TILE_TYPES[number])[] = []; + public playerEastTiles: (typeof TILE_TYPES[number])[] = []; + public playerSouthTiles: (typeof TILE_TYPES[number])[] = []; + public playerWestTiles: (typeof TILE_TYPES[number])[] = []; + public playerNorthTiles: (typeof TILE_TYPES[number])[] = []; + public turn: Player = 'east'; + + constructor() { + this.tiles = TILE_TYPES.slice(); + this.shuffleTiles(); + } + + public shuffleTiles() { + this.tiles.sort(() => Math.random() - 0.5); + } + + public drawTile(): typeof TILE_TYPES[number] { + return this.tiles.pop()!; + } + + public operation_drop(player: Player, tile: typeof TILE_TYPES[number]) { + if (this.turn !== player) { + throw new Error('Not your turn'); + } + + switch (player) { + case 'east': + this.playerEastTiles.splice(this.playerEastTiles.indexOf(tile), 1); + break; + case 'south': + this.playerSouthTiles.splice(this.playerSouthTiles.indexOf(tile), 1); + break; + case 'west': + this.playerWestTiles.splice(this.playerWestTiles.indexOf(tile), 1); + break; + case 'north': + this.playerNorthTiles.splice(this.playerNorthTiles.indexOf(tile), 1); + break; + } + this.場.push(tile); + } +}