This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e5485f
commit 4c8646c
Showing
3 changed files
with
166 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { Coordinate, Tile } from '@/components/mahjong/game.ts' | ||
|
||
export class TileMap { | ||
private readonly tileMap: { | ||
[key: string]: Tile | ||
} | ||
|
||
constructor(tiles: Tile[]) { | ||
this.tileMap = {} | ||
for (const tile of tiles) { | ||
this.tileMap[tileToKey(tile)] = tile | ||
} | ||
} | ||
|
||
public get(coord: Coordinate): Tile | null { | ||
return this.tileMap[tileToKey(coord)] || null | ||
} | ||
|
||
public set(tile: Tile): void { | ||
this.tileMap[tileToKey(tile)] = tile | ||
} | ||
|
||
public delete(tile: Tile): void { | ||
delete this.tileMap[tileToKey(tile)] | ||
} | ||
|
||
public tiles(): Tile[] { | ||
return Object.values(this.tileMap) | ||
} | ||
|
||
public isOpen({ x, y, z }: Coordinate): boolean { | ||
let occupiedLeft = this.get({ x: x - 1, y, z }) !== null | ||
occupiedLeft = occupiedLeft || (this.get({ x: x - 1, y: y - 1, z }) !== null) | ||
occupiedLeft = occupiedLeft || (this.get({ x: x - 1, y: y + 1, z }) !== null) | ||
|
||
let occupiedRight = this.get({ x: x + 1, y, z }) !== null | ||
occupiedRight = occupiedRight || (this.get({ x: x + 1, y: y - 1, z }) !== null) | ||
occupiedRight = occupiedRight || (this.get({ x: x + 1, y: y + 1, z }) !== null) | ||
|
||
let occupiedTop = this.get({ x, y, z: z + 1 }) !== null | ||
occupiedTop = occupiedTop || (this.get({ x, y: y - 1, z: z + 1 }) !== null) | ||
occupiedTop = occupiedTop || (this.get({ x, y: y + 1, z: z + 1 }) !== null) | ||
|
||
if (occupiedTop) { | ||
return false | ||
} | ||
|
||
if (occupiedLeft && occupiedRight) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
public openExists(): boolean { | ||
for (const tile of this.tiles()) { | ||
if (this.isOpen(tile.coord)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
||
function tileToKey(tile: Tile | Coordinate): string { | ||
if ('x' in tile) { | ||
return `${tile.x},${tile.y},${tile.z}` | ||
} | ||
return `${tile.coord.x},${tile.coord.y},${tile.coord.z}` | ||
} |