Files
OpenFrontIO/src/core/Util.ts
T
2024-08-29 20:04:29 -07:00

44 lines
1.4 KiB
TypeScript

import {functional} from "typia";
import {Cell, Tile} from "./Game";
export function manhattanDist(c1: Cell, c2: Cell): number {
return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
}
export function within(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}
export function dist(dist: number): (root: Tile, tile: Tile) => boolean {
return (root: Tile, n: Tile) => manhattanDist(root.cell(), n.cell()) <= dist;
}
export function and(x: (root: Tile, tile: Tile) => boolean, y: (root: Tile, tile: Tile) => boolean): (root: Tile, tile: Tile) => boolean {
return (root: Tile, tile: Tile) => x(root, tile) && y(root, tile)
}
export function bfs(tile: Tile, filter: (root: Tile, tile: Tile) => boolean): Set<Tile> {
const seen = new Set<Tile>
const q: Tile[] = []
q.push(tile)
while (q.length > 0) {
const curr = q.pop()
seen.add(curr)
for (const n of curr.neighbors()) {
if (!seen.has(n) && filter(tile, n)) {
q.push(n)
}
}
}
return seen
}
export function simpleHash(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash);
}