UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

89 lines 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAdjacentGroups = void 0; var grid_hex_util_1 = require("./grid.hex.util"); function getPreviousAdjacentCoordinates(_a, hexGridSystem) { var x = _a.x, y = _a.y; var adjacentCoordinates = []; if (hexGridSystem === undefined) { if (x > 0) adjacentCoordinates.push({ x: x - 1, y: y }); if (y > 0) adjacentCoordinates.push({ x: x, y: y - 1 }); return adjacentCoordinates; } switch (hexGridSystem) { case grid_hex_util_1.HexGridSystem.Axial: if (x > 0) adjacentCoordinates.push({ x: x - 1, y: y }); if (y > 0) adjacentCoordinates.push({ x: x, y: y - 1 }, { x: x + 1, y: y - 1 }); return adjacentCoordinates; case grid_hex_util_1.HexGridSystem.OddQ: case grid_hex_util_1.HexGridSystem.EvenQ: if (x > 0) adjacentCoordinates.push({ x: x - 1, y: y }); if (y > 0) { if (hexGridSystem === (x % 2 ? grid_hex_util_1.HexGridSystem.OddQ : grid_hex_util_1.HexGridSystem.EvenQ)) { adjacentCoordinates.push({ x: x, y: y - 1 }); } else { if (x > 0) { adjacentCoordinates.push({ x: x - 1, y: y - 1 }); } adjacentCoordinates.push({ x: x, y: y - 1 }, { x: x + 1, y: y - 1 }); } } return adjacentCoordinates; default: throw new Error('Not implemented'); } } function createAdjacentGroups(map, options) { var _a; var _b, _c, _d; var isEmpty = (_b = options === null || options === void 0 ? void 0 : options.isEmpty) !== null && _b !== void 0 ? _b : (function (value) { return !value; }); var groups = []; for (var y = 0; y < map.length; y++) { groups.push([]); for (var x = 0; x < map[y].length; x++) { var value = map[y][x]; if (isEmpty(value)) { groups[y][x] = { values: [], coordinates: [] }; } else { var previousAdjacentCoordinates = getPreviousAdjacentCoordinates({ x: x, y: y }, options === null || options === void 0 ? void 0 : options.hexGridSystem); var adjacentGroups = []; for (var _i = 0, previousAdjacentCoordinates_1 = previousAdjacentCoordinates; _i < previousAdjacentCoordinates_1.length; _i++) { var _e = previousAdjacentCoordinates_1[_i], x_1 = _e.x, y_1 = _e.y; if (((_d = (_c = groups[y_1]) === null || _c === void 0 ? void 0 : _c[x_1]) === null || _d === void 0 ? void 0 : _d.values.length) && !adjacentGroups.includes(groups[y_1][x_1])) { adjacentGroups.push(groups[y_1][x_1]); } } if (!adjacentGroups.length) { groups[y][x] = { values: [value], coordinates: [{ x: x, y: y }] }; } else if (adjacentGroups.length === 1) { adjacentGroups[0].values.push(value); adjacentGroups[0].coordinates.push({ x: x, y: y }); groups[y][x] = adjacentGroups[0]; } else { var fusionGroup = { values: [value], coordinates: [{ x: x, y: y }] }; groups[y][x] = fusionGroup; for (var i = 0; i < adjacentGroups.length; i++) { (_a = fusionGroup.values).push.apply(_a, adjacentGroups[i].values); for (var _f = 0, _g = adjacentGroups[i].coordinates; _f < _g.length; _f++) { var _h = _g[_f], x_2 = _h.x, y_2 = _h.y; fusionGroup.coordinates.push({ x: x_2, y: y_2 }); groups[y_2][x_2] = fusionGroup; } } } } } } return groups; } exports.createAdjacentGroups = createAdjacentGroups; //# sourceMappingURL=adjacent-groups.util.js.map