diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2022-03-27 23:06:05 +0200 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2022-03-27 23:06:05 +0200 |
| commit | 6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925 (patch) | |
| tree | 52a77c928e527125c752fdc2fec5d1809ea43818 /src/map.ts | |
| parent | ab750d05a2eb580067e51c26032ff24358834d0b (diff) | |
| download | boules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.tar.gz boules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.tar.zst boules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.zip | |
v0.1.0 - Game without ending
Diffstat (limited to 'src/map.ts')
| -rw-r--r-- | src/map.ts | 63 |
1 files changed, 62 insertions, 1 deletions
@@ -1,7 +1,68 @@ import Ball from "./Ball"; -import { MAP_HEIGHT, MAP_WIDTH } from "./consts"; +import { DELETE_FROM_BALLS, MAP_HEIGHT, MAP_WIDTH } from "./consts"; import { Graph } from "./pathfinder/Graph"; +export function findToRemove(balls: Ball[]): Ball[] { + const out = new Set<Ball>() + for (const ball of balls) { + const xNeighbours = new Set<Ball>(); + for (let x = ball.x; x < MAP_HEIGHT; x++) { + const b = balls.find(b => b.y === ball.y && b.x === x && b.color === ball.color); + if (!b) break; + xNeighbours.add(b) + } + for (let x = ball.x - 1; x >= 0; x--) { + const b = balls.find(b => b.y === ball.y && b.x === x && b.color === ball.color) + if (!b) break; + xNeighbours.add(b) + } + + const yNeighbours = new Set<Ball>(); + for (let y = ball.y; y < MAP_WIDTH; y++) { + const b = balls.find(b => b.x === ball.x && b.y === y && b.color === ball.color); + if (!b) break; + yNeighbours.add(b) + } + for (let y = ball.y - 1; y >= 0; y--) { + const b = balls.find(b => b.x === ball.x && b.y === y && b.color === ball.color) + if (!b) break; + yNeighbours.add(b) + } + + // same(Sign)XYNeigbours + const sxyNeighbours = new Set<Ball>(); + for (let x = ball.x, y = ball.y; x >= 0 && y >= 0; x--, y--) { + const b = balls.find(b => b.x === x && b.y === y && b.color === ball.color) + if (!b) break; + sxyNeighbours.add(b) + } + for (let x = ball.x + 1, y = ball.y + 1; x < MAP_HEIGHT && y < MAP_WIDTH; x--, y--) { + const b = balls.find(b => b.x === x && b.y === y && b.color === ball.color) + if (!b) break; + sxyNeighbours.add(b) + } + + // diffrent(Sign)XYNeigbours + const dxyNeighbours = new Set<Ball>(); + for (let x = ball.x, y = ball.y; x >= 0 && y < MAP_WIDTH; x--, y++) { + const b = balls.find(b => b.x === x && b.y === y && b.color === ball.color) + if (!b) break; + dxyNeighbours.add(b) + } + for (let x = ball.x + 1, y = ball.y - 1; x >= 0 && y >= 0; x++, y--) { + const b = balls.find(b => b.x === x && b.y === y && b.color === ball.color) + if (!b) break; + dxyNeighbours.add(b) + } + + const sets = [xNeighbours, yNeighbours, sxyNeighbours, dxyNeighbours] + for (const s of sets) { + if (s.size >= DELETE_FROM_BALLS) s.forEach(b => out.add(b)) + } + } + return [...out]; +} + export type Map = (Ball | null)[][]; export function genMap(balls: Array<Ball>): Map { const map = <Map>[]; |
