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
Showing
7 changed files
with
290 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Tile as TileT } from './game' | ||
import { cn } from '@/lib/utils' | ||
import { useState } from 'react' | ||
import { FieldTemplate, TEMPLATE_1 } from './field-template' | ||
import { coordsEqual, Game } from './game' | ||
|
||
export function Mahjong() { | ||
const [tiles, setTiles] = useState<TileT[]>([]) | ||
const [selected, setSelected] = useState<TileT | null>(null) | ||
|
||
const [game, _] = useState(() => { | ||
const g = Game.random( | ||
FieldTemplate.decode(TEMPLATE_1), | ||
(a, b) => a === b, | ||
['A', 'B', 'C', 'D'], | ||
) | ||
g.onTilesChange = setTiles | ||
g.onSelectedTileChange = setSelected | ||
setTiles(g.tiles()) | ||
return g | ||
}) | ||
|
||
return ( | ||
<div className="relative"> | ||
{tiles.map((t, i) => ( | ||
<Tile | ||
key={i} | ||
tile={t} | ||
open={game.isTileOpen(t.coord)} | ||
selected={selected ? coordsEqual(t.coord, selected.coord) : false} | ||
onClick={() => game.selectAt(t.coord)} | ||
/> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
function Tile({ | ||
tile, | ||
open, | ||
selected, | ||
onClick, | ||
}: { | ||
tile: TileT | ||
open: boolean | ||
selected: boolean | ||
onClick?: () => void | ||
}) { | ||
return ( | ||
<div | ||
className={cn( | ||
'h-[40px] w-[20px] border bg-gray-300 text-green-900 translate-x-[calc(var(--x)*20px)] translate-y-[calc(var(--y)*20px)] absolute', | ||
!open && 'bg-slate-500 text-red-900', | ||
selected && 'bg-blue-500 text-white', | ||
)} | ||
style={{ | ||
'--x': tile.coord.x, | ||
'--y': tile.coord.y, | ||
'zIndex': tile.coord.z, | ||
} as React.CSSProperties} | ||
onClick={(e) => { | ||
e.preventDefault() | ||
onClick?.() | ||
}} | ||
> | ||
{tile.kind as string} | ||
</div> | ||
) | ||
} |
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 was deleted.
Oops, something went wrong.
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,126 @@ | ||
import type { FieldTemplate } from './field-template' | ||
|
||
export type Coordinate = { x: number, y: number, z: number } | ||
export type Coordinate2D = { x: number, y: number } | ||
export type Move = [Coordinate, Coordinate] | ||
export type Tile<K = unknown> = { | ||
kind: K | ||
/** Coordinate of the top half of the tile. */ | ||
coord: Coordinate | ||
} | ||
export type Comparator<K = unknown> = (kindA: K, kindB: K) => boolean | ||
|
||
export class Game<K = unknown> { | ||
private inGameTiles: Tile<K>[] | ||
private selectedTile: Tile<K> | null | ||
private comparator: Comparator<K> | ||
|
||
constructor( | ||
tiles: Tile<K>[], | ||
comparator: Comparator<K>, | ||
public onSelectedTileChange?: (tile: Tile<K> | null) => void, | ||
public onTilesChange?: (tiles: Tile<K>[]) => void, | ||
) { | ||
this.inGameTiles = tiles | ||
this.selectedTile = null | ||
this.comparator = comparator | ||
} | ||
|
||
public static random<K>( | ||
template: FieldTemplate, | ||
comparator: Comparator<K>, | ||
choices: K[], | ||
): Game<K> { | ||
const tiles = template.tileCoords.map(coord => ({ | ||
kind: choices[Math.floor(Math.random() * choices.length)], | ||
coord, | ||
})) | ||
|
||
return new Game(tiles, comparator) | ||
} | ||
|
||
public tiles(): Tile<K>[] { | ||
return this.inGameTiles | ||
} | ||
|
||
public selectAt(coord: Coordinate2D): void { | ||
const tile = this.tilesAt2D(coord)[0] | ||
if (!tile) | ||
return | ||
|
||
if (this.selectedTile) { | ||
if (coordsEqual(this.selectedTile.coord, tile.coord)) { | ||
this.selectedTile = null | ||
return | ||
} | ||
else if (this.comparator(this.selectedTile.kind, tile.kind) && this.isTileOpen(tile.coord)) { | ||
this.removeTile(this.selectedTile.coord) | ||
this.removeTile(tile.coord) | ||
this.selectedTile = null | ||
return | ||
} | ||
} | ||
|
||
if (this.isTileOpen(tile.coord)) { | ||
this.selectedTile = tile | ||
} | ||
|
||
this.onSelectedTileChange?.(this.selectedTile) | ||
} | ||
|
||
public removeTile(coord: Coordinate): void { | ||
this.inGameTiles = this.inGameTiles.filter(t => !coordsEqual(t.coord, coord)) | ||
this.onTilesChange?.(this.inGameTiles) | ||
} | ||
|
||
public tilesAt2D(coord: Coordinate2D): Tile<K>[] { | ||
return this.inGameTiles | ||
.filter(t => t.coord.x === coord.x && (t.coord.y === coord.y || t.coord.y + 1 === coord.y)) | ||
.sort((a, b) => b.coord.z - a.coord.z) | ||
} | ||
|
||
/** Returns a tile at field coordinate. */ | ||
public tileAt({ x, y, z }: Coordinate): Tile<K> | null { | ||
// @todo Make better than O(n) | ||
for (const tile of this.inGameTiles) { | ||
if (tile.coord.z === z && tile.coord.x === x && (tile.coord.y === y || tile.coord.y + 1 === y)) { | ||
return tile | ||
} | ||
} | ||
return null | ||
} | ||
|
||
/** Returns whether the tile at the given field coordinate is open. */ | ||
public isTileOpen({ x, y, z }: Coordinate): boolean { | ||
let occupiedLeft = false | ||
let occupiedRight = false | ||
|
||
// @todo Make better than O(n) | ||
for (const tile of this.inGameTiles) { | ||
if (tile.coord.z < z) | ||
continue | ||
|
||
if (tile.coord.z > z) { | ||
// If lies on top -> closed | ||
if (tile.coord.x === x && (Math.abs(tile.coord.y - y) <= 1)) { | ||
return false | ||
} | ||
} | ||
|
||
if (tile.coord.z === z) { | ||
if (tile.coord.x - 1 === x && (Math.abs(tile.coord.y - y) <= 1)) { | ||
occupiedLeft = true | ||
} | ||
else if (tile.coord.x + 1 === x && (Math.abs(tile.coord.y - y) <= 1)) { | ||
occupiedRight = true | ||
} | ||
} | ||
} | ||
|
||
return !occupiedLeft || !occupiedRight | ||
} | ||
} | ||
|
||
export function coordsEqual(a: Coordinate, b: Coordinate): boolean { | ||
return a.x === b.x && a.y === b.y && a.z === b.z | ||
} |
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,14 @@ | ||
import { Mahjong } from '@/components/mahjong/Mahjong' | ||
import { createFileRoute } from '@tanstack/react-router' | ||
|
||
export const Route = createFileRoute('/test')({ | ||
component: RouteComponent, | ||
}) | ||
|
||
function RouteComponent() { | ||
return ( | ||
<div className="flex h-screen items-center justify-center"> | ||
<Mahjong /> | ||
</div> | ||
) | ||
} |