'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, fillColumn, Planet, Rng, GameCore } = require('./helpers.js'); const Offline = require('../src/offline.js'); describe('Offline Strijd progression (t1119)', () => { it('one opponent for one round, then N opponents for N-1 rounds', () => { assert.equal(Offline.roundsAtOpponentCount(1), 1); assert.equal(Offline.roundsAtOpponentCount(2), 1); assert.equal(Offline.roundsAtOpponentCount(3), 2); assert.equal(Offline.roundsAtOpponentCount(6), 5); const expect = { 1: { opponents: 1 }, 2: { opponents: 2 }, 3: { opponents: 3 }, 4: { opponents: 3 }, 5: { opponents: 4 }, 6: { opponents: 4 }, 7: { opponents: 4 }, 8: { opponents: 5 } }; for (const [round, exp] of Object.entries(expect)) { const s = Offline.scheduleForRound(Number(round)); assert.equal(s.opponents, exp.opponents, 'round ' + round + ' opponents=' + s.opponents); assert.equal(s.round, Number(round)); } }); it('skill starts at 5% and rises 1% per round, capped at 100%', () => { assert.equal(Offline.skillForRound(1), 0.05); assert.equal(Offline.skillForRound(2), 0.06); assert.equal(Offline.skillForRound(96), 1); assert.equal(Offline.skillForRound(97), 1); assert.equal(Offline.skillForRound(200), 1); assert.equal(Offline.scheduleForRound(1).skill, 0.05); assert.equal(Offline.scheduleForRound(10).skill, 0.14); }); }); describe('Bot move finder and roll (t1119)', () => { it('finds the shortest vertical move that creates a match', () => { const g = makeGame({ noSpawn: true }); // Column 0: [0, 0, 1, 0] — moving the 1 up (or the top 0 down) makes [0,0,0,1]. g.place(0, 0, 0); g.place(0, 1, 0); g.place(0, 2, 1); g.place(0, 3, 0); const move = Offline.findBotMove(g); assert.ok(move, 'expected a move'); assert.equal(move.col, 0); assert.equal(move.dist, 1); assert.ok( (move.fromY === 2 && move.toY === 3) || (move.fromY === 3 && move.toY === 2), 'from=' + move.fromY + ' to=' + move.toY ); assert.ok(Offline.executeBotMove(g, move)); const launched = g.resolveMatches(); assert.ok(launched.length >= 1, 'the move must create a match'); }); it('returns null when no vertical move creates a match', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 0); g.place(0, 1, 1); g.place(0, 2, 2); g.place(1, 0, 3); g.place(1, 1, 4); assert.equal(Offline.findBotMove(g), null); }); it('a roll at skill 0 never moves, skill 1 always tries', () => { const setup = () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 0); g.place(0, 1, 0); g.place(0, 2, 1); g.place(0, 3, 0); return g; }; const g0 = setup(); const r0 = Offline.tryBotRoll(g0, 0, () => 0); assert.equal(r0.success, false); assert.equal(r0.moved, false); assert.equal(g0.blocks[2].y, 2, 'nothing moved at skill 0 even with roll 0'); const g1 = setup(); const r1 = Offline.tryBotRoll(g1, 1, () => 0.999); assert.equal(r1.success, true); assert.equal(r1.moved, true); assert.ok(r1.move); // roll == skill is a failure (success only when roll < skill) const g2 = setup(); const r2 = Offline.tryBotRoll(g2, 0.05, () => 0.05); assert.equal(r2.success, false); assert.equal(r2.moved, false); }); }); describe('Offline battle flow (t1119)', () => { it('starts a round with the scheduled number of bots, each a random planet', () => { const rng = Rng.create(42); const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: rng, round: 5 }); assert.equal(battle.schedule.opponents, 4); assert.equal(battle.bots.length, 4); assert.equal(battle.schedule.skill, Offline.skillForRound(5)); for (const bot of battle.bots) { assert.ok(bot.planet && bot.planet.id); assert.ok(bot.core instanceof GameCore); assert.equal(bot.eliminated, false); } const ids = new Set(battle.bots.map((b) => b.planet.id)); // With 12 planets and 4 bots, seed 42 is very likely to vary; just // assert they are real catalogue planets. const catalogue = new Set(Planet.allPlanets().map((p) => p.id)); for (const id of ids) { assert.ok(catalogue.has(id), 'unknown planet ' + id); } }); it('a forced miss never delivers garbage; a forced hit does', () => { const player = makeGame({ noSpawn: true }); // Forced miss: rng always returns 0 for pick? miss if rng() < missChance. // Use a planet with 60% miss (power 0) and rng that always returns 0 // → always miss? 0 < 0.60 is true, so miss. Wait, I used rng() < missChance // in battle... _dispatchShots: miss = targets.length===0 || this.rng() < missChance // Yes 0 < 0.60 → miss. const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' }); let n = 0; const missRng = () => { n++; return 0; }; // always miss (0 < 0.60) const battle = Offline.createBattle({ playerPlanet: easy, rng: missRng, round: 1 }); // Give the player an outgoing shot without actually flying. player.outgoingShots.push({ id: 99, color: 1, col: 0, y: 13 }); battle.tick(0.01, player); assert.ok(battle.flightResults.length >= 1); assert.equal(battle.flightResults[0].miss, true); assert.equal(battle.pending.length, 0); // Forced hit: rng always 0.99, missChance 0.60, 0.99 < 0.60 is false. const hitRng = () => 0.99; const battle2 = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 }); const player2 = makeGame({ noSpawn: true }); player2.outgoingShots.push({ id: 7, color: 2, col: 1, y: 13 }); const botBlocksBefore = battle2.bots[0].core.blocks.length; battle2.tick(0.01, player2); assert.equal(battle2.flightResults[0].miss, false); assert.equal(battle2.pending.length, 1); // Deliver after the flight duration. battle2.tick(Offline.FLIGHT_DURATION + 0.05, player2); assert.equal(battle2.pending.length, 0); assert.ok(battle2.bots[0].core.blocks.length > botBlocksBefore, 'garbage was ingested by the bot'); }); it('winning a round means every bot is defeated, then advanceRound grows the field', () => { const rng = Rng.create(7); const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng, round: 1 }); assert.equal(battle.bots.length, 1); const player = makeGame({ noSpawn: true }); battle.bots[0].core.state = 'gameover'; battle.tick(0.05, player); assert.equal(battle.state, 'round_won'); assert.equal(battle.aliveBots().length, 0); battle.advanceRound(); assert.equal(battle.round, 2); assert.equal(battle.state, 'playing'); assert.equal(battle.bots.length, 2); assert.equal(battle.schedule.skill, 0.06); }); it('the player losing sets state to lost at the current round', () => { const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: Rng.create(1), round: 3 }); const player = makeGame({ noSpawn: true }); player.state = 'gameover'; battle.tick(0.05, player); assert.equal(battle.state, 'lost'); assert.equal(battle.round, 3); }); }); function setupTwoStep (g) { // No immediate 3-match. After col0: 0→1 ([1,0,2,0]) a second // adjacent swap on col2 (7 at y=1 → y=2) makes row 1 = 0,0,0. g.place(0, 0, 0); g.place(0, 1, 1); g.place(0, 2, 2); g.place(0, 3, 0); g.place(1, 0, 3); g.place(1, 1, 0); g.place(1, 2, 4); g.place(1, 3, 5); g.place(2, 0, 6); g.place(2, 1, 7); g.place(2, 2, 0); g.place(2, 3, 8); return g; } function inSameSegment (grid, height, move) { if (move.fromY === move.toY) { return false; } const lo = Math.min(move.fromY, move.toY); const hi = Math.max(move.fromY, move.toY); for (let y = lo; y <= hi; y++) { if (grid[move.col][y] == null) { return false; } } return lo >= 0 && hi < height; } describe('Bot lookahead planner (t1144)', () => { it('lookaheadChance is 0 through 10% skill, then linear to 1 at 40%', () => { assert.equal(Offline.lookaheadChance(0), 0); assert.equal(Offline.lookaheadChance(0.05), 0); assert.equal(Offline.lookaheadChance(0.10), 0); assert.ok(Math.abs(Offline.lookaheadChance(0.25) - 0.5) < 1e-12); assert.equal(Offline.lookaheadChance(0.40), 1); assert.equal(Offline.lookaheadChance(1), 1); }); it('finds a 1-step immediate match and prefers it over a setup', () => { const g = makeGame({ noSpawn: true }); // Immediate: col0 [0,0,1,0] — also leave a 2-step shape on the right // that must not be chosen while the 1-step exists. g.place(0, 0, 0); g.place(0, 1, 0); g.place(0, 2, 1); g.place(0, 3, 0); g.place(1, 0, 3); g.place(1, 1, 0); g.place(1, 2, 4); g.place(1, 3, 5); g.place(2, 0, 6); g.place(2, 1, 7); g.place(2, 2, 0); g.place(2, 3, 8); const move = Offline.findBotMove(g); assert.ok(move, 'expected an immediate move'); assert.equal(move.col, 0); assert.equal(move.dist, 1); const memory = { plan: null }; const r = Offline.tryBotRoll(g, 1, () => 0, memory); assert.equal(r.success, true); assert.equal(r.moved, true); assert.equal(r.planned, undefined); assert.equal(memory.plan, null); assert.equal(r.move.col, 0); assert.equal(r.move.dist, 1); const launched = g.resolveMatches(); assert.ok(launched.length >= 1, 'immediate move must create a match'); }); it('finds a 2-step setup when no immediate match exists', () => { const g = setupTwoStep(makeGame({ noSpawn: true })); assert.equal(Offline.findBotMove(g), null, 'fixture must have no 1-step'); const plan = Offline.findSetupPlan(g); assert.ok(plan && plan.steps && plan.steps.length === 2, 'expected a 2-step plan'); assert.equal(plan.steps[0].dist + plan.steps[1].dist, plan.total); assert.ok(plan.total >= 2); const firstLegal = Offline.listMovesFromCore(g).some((m) => m.col === plan.steps[0].col && m.fromY === plan.steps[0].fromY && m.toY === plan.steps[0].toY); assert.ok(firstLegal, 'first step must be a legal core move'); assert.ok(Offline.executeBotMove(g, plan.steps[0])); assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false, 'first step itself is a setup, not a match'); assert.ok(Offline.stepIsValid(g, plan.steps[1], true), 'second step must still be legal and create a match'); assert.ok(Offline.executeBotMove(g, plan.steps[1])); const launched = g.resolveMatches(); assert.ok(launched.length >= 1, 'second step must create a match'); }); it('performs the first step and follows the plan on the next successful roll', () => { const g = setupTwoStep(makeGame({ noSpawn: true })); const memory = { plan: null }; const r1 = Offline.tryBotRoll(g, 1, () => 0, memory); assert.equal(r1.success, true); assert.equal(r1.moved, true); assert.equal(r1.planned, true); assert.ok(memory.plan && memory.plan.steps.length === 2); assert.equal(memory.plan.index, 1); assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false); const r2 = Offline.tryBotRoll(g, 1, () => 0, memory); assert.equal(r2.success, true); assert.equal(r2.moved, true); assert.equal(r2.followed, true); assert.equal(memory.plan, null); const launched = g.resolveMatches(); assert.ok(launched.length >= 1, 'followed step must create a match'); }); it('follows a committed plan even below the lookahead threshold', () => { const g = setupTwoStep(makeGame({ noSpawn: true })); const memory = { plan: null }; assert.ok(Offline.tryBotRoll(g, 1, () => 0, memory).planned); // skill 5% is below the 10% lookahead floor, but the roll succeeds // (0 < 0.05) and the stored second step is still valid. assert.equal(Offline.lookaheadChance(0.05), 0); const r = Offline.tryBotRoll(g, 0.05, () => 0, memory); assert.equal(r.followed, true); assert.equal(r.moved, true); }); it('re-plans when the stored next step is no longer valid', () => { const g = setupTwoStep(makeGame({ noSpawn: true })); const memory = { plan: null }; const r1 = Offline.tryBotRoll(g, 1, () => 0, memory); assert.ok(r1.planned && memory.plan); const next = memory.plan.steps[1]; // After the first step, row 1 is a pair of 0s waiting for the // third 0 from the second step. Recolor the middle 0 so that // following the stored step no longer makes a match. const mid = g.blockAt(1, 1); assert.ok(mid); assert.equal(mid.color, 0); mid.color = 9; const r2 = Offline.tryBotRoll(g, 1, () => 0, memory); assert.notEqual(r2.followed, true); // Plan was dropped; a new plan may or may not exist after the // recolor, but the invalid step must not have been executed. if (r2.moved) { assert.ok( r2.move.col !== next.col || r2.move.fromY !== next.fromY || r2.move.toY !== next.toY || r2.planned === true, 'must not blindly follow an invalid step' ); } }); it('does not start a new 2-step plan at or below 10% skill', () => { const g = setupTwoStep(makeGame({ noSpawn: true })); const memory = { plan: null }; // Successful roll (0 < 0.10) but lookaheadChance is 0, so no // second rng() and no setup. const r = Offline.tryBotRoll(g, 0.10, () => 0, memory); assert.equal(r.success, true); assert.equal(r.moved, false); assert.equal(memory.plan, null); assert.equal(Offline.findBotMove(g), null); }); it('does not propose illegal swaps (gap, locked column, or off-segment)', () => { const g = makeGame({ noSpawn: true }); // Gapped column 0: segments [0,1] and [3,4] — a cross-gap swap // is illegal. Locked full column 1 is also illegal. g.place(0, 0, 0); g.place(0, 1, 1); g.place(0, 3, 0); g.place(0, 4, 2); fillColumn(g, 1, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]); g.checkLocks(); assert.equal(g.isColumnLocked(1), true); // Legal 2-step fixture on cols 2–4 (same shape, shifted). g.place(2, 0, 0); g.place(2, 1, 1); g.place(2, 2, 2); g.place(2, 3, 0); g.place(3, 0, 3); g.place(3, 1, 0); g.place(3, 2, 4); g.place(3, 3, 5); g.place(4, 0, 6); g.place(4, 1, 7); g.place(4, 2, 0); g.place(4, 3, 8); const grid = Offline.snapshotGrid(g); const coreMoves = Offline.listMovesFromCore(g); for (const m of coreMoves) { assert.notEqual(m.col, 1, 'locked column must have no moves: ' + JSON.stringify(m)); assert.ok(inSameSegment(grid, g.height, m), 'core move crossed a gap: ' + JSON.stringify(m)); if (m.col === 0) { const low = m.fromY <= 1 && m.toY <= 1; const high = m.fromY >= 3 && m.toY >= 3; assert.ok(low || high, 'col 0 move crossed the gap: ' + JSON.stringify(m)); } } assert.equal(Offline.findBotMove(g), null); const plan = Offline.findSetupPlan(g); assert.ok(plan, 'expected a legal 2-step on cols 2–4'); assert.notEqual(plan.steps[0].col, 1); assert.ok(inSameSegment(grid, g.height, plan.steps[0])); const firstLegal = coreMoves.some((m) => m.col === plan.steps[0].col && m.fromY === plan.steps[0].fromY && m.toY === plan.steps[0].toY); assert.ok(firstLegal, 'first plan step must be in listMovesFromCore'); assert.ok(Offline.executeBotMove(g, plan.steps[0]), 'first step must be executable'); const grid2 = Offline.snapshotGrid(g); assert.ok(inSameSegment(grid2, g.height, plan.steps[1]), 'second step crossed a gap: ' + JSON.stringify(plan.steps[1])); assert.ok(Offline.stepIsValid(g, plan.steps[1], true)); assert.ok(Offline.executeBotMove(g, plan.steps[1])); }); it('finishes a full 10×13 search quickly and within budget', () => { const g = makeGame({ noSpawn: true }); for (let c = 0; c < g.width; c++) { for (let r = 0; r < g.height; r++) { g.place(c, r, (c + r) % 3); } } const t0 = Date.now(); const plan = Offline.findSetupPlan(g); const ms = Date.now() - t0; assert.ok(ms < 200, 'planner took ' + ms + ' ms on a full board'); if (plan) { const grid = Offline.snapshotGrid(g); assert.ok(inSameSegment(grid, g.height, plan.steps[0])); assert.ok(Offline.executeBotMove(g, plan.steps[0])); } }); });