From 6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Sun, 27 Mar 2022 23:06:05 +0200 Subject: v0.1.0 - Game without ending --- src/map.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'src/map.ts') diff --git a/src/map.ts b/src/map.ts index 44a970c..9fa85ef 100644 --- a/src/map.ts +++ b/src/map.ts @@ -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() + for (const ball of balls) { + const xNeighbours = new Set(); + 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(); + 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(); + 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(); + 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): Map { const map = []; -- cgit v1.3.1