Feature: Map size configuration

This commit is contained in:
sokol
2026-02-22 11:04:11 +03:00
parent f3be577a32
commit 3439d04a55
4 changed files with 391 additions and 44 deletions

View File

@@ -2,6 +2,95 @@ const { describe, it } = require('node:test');
const assert = require('node:assert');
const { HexMap, HexCell, CELL_TYPES, MAP_SIZE } = require('../public/map.js');
describe('HexMap - Dynamic Map Sizes', () => {
it('should create a 10x10 map', () => {
const map = new HexMap(10);
assert.strictEqual(map.size, 10);
assert.strictEqual(map.cells.size, 10 * 10);
});
it('should create a 15x15 map', () => {
const map = new HexMap(15);
assert.strictEqual(map.size, 15);
assert.strictEqual(map.cells.size, 15 * 15);
});
it('should create a 20x20 map', () => {
const map = new HexMap(20);
assert.strictEqual(map.size, 20);
assert.strictEqual(map.cells.size, 20 * 20);
});
it('should create a 25x25 map', () => {
const map = new HexMap(25);
assert.strictEqual(map.size, 25);
assert.strictEqual(map.cells.size, 25 * 25);
});
it('should generate cells with correct coordinates for all map sizes', () => {
const sizes = [10, 15, 20, 25];
for (const size of sizes) {
const map = new HexMap(size);
for (let q = 0; q < size; q++) {
for (let r = 0; r < size; r++) {
const cell = map.getCell(q, r);
assert.ok(cell, `Cell at ${q},${r} should exist for size ${size}`);
assert.strictEqual(cell.q, q);
assert.strictEqual(cell.r, r);
}
}
}
});
it('should have correct neighbor counts for different map sizes', () => {
const sizes = [10, 15, 20, 25];
for (const size of sizes) {
const map = new HexMap(size);
// Clear all blocks for predictable testing
map.cells.forEach(cell => {
if (cell.type === CELL_TYPES.BLOCKED) {
cell.type = CELL_TYPES.EMPTY;
}
});
// Center cell should have 6 neighbors
const centerQ = Math.floor(size / 2);
const centerR = Math.floor(size / 2);
const centerNeighbors = map.getNeighbors(centerQ, centerR);
assert.strictEqual(centerNeighbors.length, 6, `Center cell should have 6 neighbors for size ${size}`);
// Corner cell should have 2 neighbors
const cornerNeighbors = map.getNeighbors(0, 0);
assert.strictEqual(cornerNeighbors.length, 2, `Corner cell should have 2 neighbors for size ${size}`);
}
});
it('should calculate supply correctly for different map sizes', () => {
const sizes = [10, 15, 20, 25];
for (const size of sizes) {
const map = new HexMap(size);
// Clear any existing ownership
map.cells.forEach(cell => {
cell.type = CELL_TYPES.EMPTY;
});
// Create a connected territory of 5 cells
for (let i = 0; i < 5; i++) {
map.setOwner(i, 0, 1);
}
const supply = map.calculateSupply(1);
assert.strictEqual(supply, 5, `Supply should be 5 for connected territory in size ${size}`);
}
});
});
describe('HexCell', () => {
it('should create a cell with axial coordinates', () => {
const cell = new HexCell(3, 5);