BlueGit

projects-timfalken-com meteors

test: planner finds 1- and 2-step setups and never proposes illegal swaps (t1144)

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
d15d1779e9c2e6781a840b75363a17604c689b49
Parent
b01e8e1a
1 file changed +258 −1
test/offline.test.js +258 −1
@@ -2,7 +2,7 @@
2 2
3 3 const { describe, it } = require('node:test');
4 4 const assert = require('node:assert/strict');
5 const { makeGame, Planet, Rng, GameCore } = require('./helpers.js');
5 const { makeGame, fillColumn, Planet, Rng, GameCore } = require('./helpers.js');
6 6 const Offline = require('../src/offline.js');
7 7
8 8 describe('Offline Strijd progression (t1119)', () =>
@@ -206,3 +206,260 @@ describe('Offline battle flow (t1119)', () =>
206 206 assert.equal(battle.round, 3);
207 207 });
208 208 });
209
210 function setupTwoStep (g)
211 {
212 // No immediate 3-match. After col0: 0→1 ([1,0,2,0]) a second
213 // adjacent swap on col2 (7 at y=1 → y=2) makes row 1 = 0,0,0.
214 g.place(0, 0, 0);
215 g.place(0, 1, 1);
216 g.place(0, 2, 2);
217 g.place(0, 3, 0);
218 g.place(1, 0, 3);
219 g.place(1, 1, 0);
220 g.place(1, 2, 4);
221 g.place(1, 3, 5);
222 g.place(2, 0, 6);
223 g.place(2, 1, 7);
224 g.place(2, 2, 0);
225 g.place(2, 3, 8);
226 return g;
227 }
228
229 function inSameSegment (grid, height, move)
230 {
231 if (move.fromY === move.toY)
232 { return false; }
233 const lo = Math.min(move.fromY, move.toY);
234 const hi = Math.max(move.fromY, move.toY);
235 for (let y = lo; y <= hi; y++)
236 {
237 if (grid[move.col][y] == null)
238 { return false; }
239 }
240 return lo >= 0 && hi < height;
241 }
242
243 describe('Bot lookahead planner (t1144)', () =>
244 {
245 it('lookaheadChance is 0 through 10% skill, then linear to 1 at 40%', () =>
246 {
247 assert.equal(Offline.lookaheadChance(0), 0);
248 assert.equal(Offline.lookaheadChance(0.05), 0);
249 assert.equal(Offline.lookaheadChance(0.10), 0);
250 assert.ok(Math.abs(Offline.lookaheadChance(0.25) - 0.5) < 1e-12);
251 assert.equal(Offline.lookaheadChance(0.40), 1);
252 assert.equal(Offline.lookaheadChance(1), 1);
253 });
254
255 it('finds a 1-step immediate match and prefers it over a setup', () =>
256 {
257 const g = makeGame({ noSpawn: true });
258 // Immediate: col0 [0,0,1,0] — also leave a 2-step shape on the right
259 // that must not be chosen while the 1-step exists.
260 g.place(0, 0, 0);
261 g.place(0, 1, 0);
262 g.place(0, 2, 1);
263 g.place(0, 3, 0);
264 g.place(1, 0, 3);
265 g.place(1, 1, 0);
266 g.place(1, 2, 4);
267 g.place(1, 3, 5);
268 g.place(2, 0, 6);
269 g.place(2, 1, 7);
270 g.place(2, 2, 0);
271 g.place(2, 3, 8);
272 const move = Offline.findBotMove(g);
273 assert.ok(move, 'expected an immediate move');
274 assert.equal(move.col, 0);
275 assert.equal(move.dist, 1);
276 const memory = { plan: null };
277 const r = Offline.tryBotRoll(g, 1, () => 0, memory);
278 assert.equal(r.success, true);
279 assert.equal(r.moved, true);
280 assert.equal(r.planned, undefined);
281 assert.equal(memory.plan, null);
282 assert.equal(r.move.col, 0);
283 assert.equal(r.move.dist, 1);
284 const launched = g.resolveMatches();
285 assert.ok(launched.length >= 1, 'immediate move must create a match');
286 });
287
288 it('finds a 2-step setup when no immediate match exists', () =>
289 {
290 const g = setupTwoStep(makeGame({ noSpawn: true }));
291 assert.equal(Offline.findBotMove(g), null, 'fixture must have no 1-step');
292 const plan = Offline.findSetupPlan(g);
293 assert.ok(plan && plan.steps && plan.steps.length === 2, 'expected a 2-step plan');
294 assert.equal(plan.steps[0].dist + plan.steps[1].dist, plan.total);
295 assert.ok(plan.total >= 2);
296 const firstLegal = Offline.listMovesFromCore(g).some((m) =>
297 m.col === plan.steps[0].col
298 && m.fromY === plan.steps[0].fromY
299 && m.toY === plan.steps[0].toY);
300 assert.ok(firstLegal, 'first step must be a legal core move');
301 assert.ok(Offline.executeBotMove(g, plan.steps[0]));
302 assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false,
303 'first step itself is a setup, not a match');
304 assert.ok(Offline.stepIsValid(g, plan.steps[1], true),
305 'second step must still be legal and create a match');
306 assert.ok(Offline.executeBotMove(g, plan.steps[1]));
307 const launched = g.resolveMatches();
308 assert.ok(launched.length >= 1, 'second step must create a match');
309 });
310
311 it('performs the first step and follows the plan on the next successful roll', () =>
312 {
313 const g = setupTwoStep(makeGame({ noSpawn: true }));
314 const memory = { plan: null };
315 const r1 = Offline.tryBotRoll(g, 1, () => 0, memory);
316 assert.equal(r1.success, true);
317 assert.equal(r1.moved, true);
318 assert.equal(r1.planned, true);
319 assert.ok(memory.plan && memory.plan.steps.length === 2);
320 assert.equal(memory.plan.index, 1);
321 assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false);
322
323 const r2 = Offline.tryBotRoll(g, 1, () => 0, memory);
324 assert.equal(r2.success, true);
325 assert.equal(r2.moved, true);
326 assert.equal(r2.followed, true);
327 assert.equal(memory.plan, null);
328 const launched = g.resolveMatches();
329 assert.ok(launched.length >= 1, 'followed step must create a match');
330 });
331
332 it('follows a committed plan even below the lookahead threshold', () =>
333 {
334 const g = setupTwoStep(makeGame({ noSpawn: true }));
335 const memory = { plan: null };
336 assert.ok(Offline.tryBotRoll(g, 1, () => 0, memory).planned);
337 // skill 5% is below the 10% lookahead floor, but the roll succeeds
338 // (0 < 0.05) and the stored second step is still valid.
339 assert.equal(Offline.lookaheadChance(0.05), 0);
340 const r = Offline.tryBotRoll(g, 0.05, () => 0, memory);
341 assert.equal(r.followed, true);
342 assert.equal(r.moved, true);
343 });
344
345 it('re-plans when the stored next step is no longer valid', () =>
346 {
347 const g = setupTwoStep(makeGame({ noSpawn: true }));
348 const memory = { plan: null };
349 const r1 = Offline.tryBotRoll(g, 1, () => 0, memory);
350 assert.ok(r1.planned && memory.plan);
351 const next = memory.plan.steps[1];
352 // After the first step, row 1 is a pair of 0s waiting for the
353 // third 0 from the second step. Recolor the middle 0 so that
354 // following the stored step no longer makes a match.
355 const mid = g.blockAt(1, 1);
356 assert.ok(mid);
357 assert.equal(mid.color, 0);
358 mid.color = 9;
359 const r2 = Offline.tryBotRoll(g, 1, () => 0, memory);
360 assert.notEqual(r2.followed, true);
361 // Plan was dropped; a new plan may or may not exist after the
362 // recolor, but the invalid step must not have been executed.
363 if (r2.moved)
364 {
365 assert.ok(
366 r2.move.col !== next.col
367 || r2.move.fromY !== next.fromY
368 || r2.move.toY !== next.toY
369 || r2.planned === true,
370 'must not blindly follow an invalid step'
371 );
372 }
373 });
374
375 it('does not start a new 2-step plan at or below 10% skill', () =>
376 {
377 const g = setupTwoStep(makeGame({ noSpawn: true }));
378 const memory = { plan: null };
379 // Successful roll (0 < 0.10) but lookaheadChance is 0, so no
380 // second rng() and no setup.
381 const r = Offline.tryBotRoll(g, 0.10, () => 0, memory);
382 assert.equal(r.success, true);
383 assert.equal(r.moved, false);
384 assert.equal(memory.plan, null);
385 assert.equal(Offline.findBotMove(g), null);
386 });
387
388 it('does not propose illegal swaps (gap, locked column, or off-segment)', () =>
389 {
390 const g = makeGame({ noSpawn: true });
391 // Gapped column 0: segments [0,1] and [3,4] — a cross-gap swap
392 // is illegal. Locked full column 1 is also illegal.
393 g.place(0, 0, 0);
394 g.place(0, 1, 1);
395 g.place(0, 3, 0);
396 g.place(0, 4, 2);
397 fillColumn(g, 1, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);
398 g.checkLocks();
399 assert.equal(g.isColumnLocked(1), true);
400 // Legal 2-step fixture on cols 2–4 (same shape, shifted).
401 g.place(2, 0, 0);
402 g.place(2, 1, 1);
403 g.place(2, 2, 2);
404 g.place(2, 3, 0);
405 g.place(3, 0, 3);
406 g.place(3, 1, 0);
407 g.place(3, 2, 4);
408 g.place(3, 3, 5);
409 g.place(4, 0, 6);
410 g.place(4, 1, 7);
411 g.place(4, 2, 0);
412 g.place(4, 3, 8);
413
414 const grid = Offline.snapshotGrid(g);
415 const coreMoves = Offline.listMovesFromCore(g);
416 for (const m of coreMoves)
417 {
418 assert.notEqual(m.col, 1, 'locked column must have no moves: ' + JSON.stringify(m));
419 assert.ok(inSameSegment(grid, g.height, m), 'core move crossed a gap: ' + JSON.stringify(m));
420 if (m.col === 0)
421 {
422 const low = m.fromY <= 1 && m.toY <= 1;
423 const high = m.fromY >= 3 && m.toY >= 3;
424 assert.ok(low || high, 'col 0 move crossed the gap: ' + JSON.stringify(m));
425 }
426 }
427
428 assert.equal(Offline.findBotMove(g), null);
429 const plan = Offline.findSetupPlan(g);
430 assert.ok(plan, 'expected a legal 2-step on cols 2–4');
431 assert.notEqual(plan.steps[0].col, 1);
432 assert.ok(inSameSegment(grid, g.height, plan.steps[0]));
433 const firstLegal = coreMoves.some((m) =>
434 m.col === plan.steps[0].col
435 && m.fromY === plan.steps[0].fromY
436 && m.toY === plan.steps[0].toY);
437 assert.ok(firstLegal, 'first plan step must be in listMovesFromCore');
438 assert.ok(Offline.executeBotMove(g, plan.steps[0]), 'first step must be executable');
439 const grid2 = Offline.snapshotGrid(g);
440 assert.ok(inSameSegment(grid2, g.height, plan.steps[1]),
441 'second step crossed a gap: ' + JSON.stringify(plan.steps[1]));
442 assert.ok(Offline.stepIsValid(g, plan.steps[1], true));
443 assert.ok(Offline.executeBotMove(g, plan.steps[1]));
444 });
445
446 it('finishes a full 10×13 search quickly and within budget', () =>
447 {
448 const g = makeGame({ noSpawn: true });
449 for (let c = 0; c < g.width; c++)
450 {
451 for (let r = 0; r < g.height; r++)
452 { g.place(c, r, (c + r) % 3); }
453 }
454 const t0 = Date.now();
455 const plan = Offline.findSetupPlan(g);
456 const ms = Date.now() - t0;
457 assert.ok(ms < 200, 'planner took ' + ms + ' ms on a full board');
458 if (plan)
459 {
460 const grid = Offline.snapshotGrid(g);
461 assert.ok(inSameSegment(grid, g.height, plan.steps[0]));
462 assert.ok(Offline.executeBotMove(g, plan.steps[0]));
463 }
464 });
465 });
@@ -2,7 +2,7 @@
2
2
3const { describe, it } = require('node:test');
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
4const assert = require('node:assert/strict');
5const { makeGame, Planet, Rng, GameCore } = require('./helpers.js');
5const { makeGame, fillColumn, Planet, Rng, GameCore } = require('./helpers.js');
6const Offline = require('../src/offline.js');
6const Offline = require('../src/offline.js');
7
7
8describe('Offline Strijd progression (t1119)', () =>
8describe('Offline Strijd progression (t1119)', () =>
@@ -206,3 +206,260 @@ describe('Offline battle flow (t1119)', () =>
206 assert.equal(battle.round, 3);
206 assert.equal(battle.round, 3);
207 });
207 });
208});
208});
209
210function setupTwoStep (g)
211{
212 // No immediate 3-match. After col0: 0→1 ([1,0,2,0]) a second
213 // adjacent swap on col2 (7 at y=1 → y=2) makes row 1 = 0,0,0.
214 g.place(0, 0, 0);
215 g.place(0, 1, 1);
216 g.place(0, 2, 2);
217 g.place(0, 3, 0);
218 g.place(1, 0, 3);
219 g.place(1, 1, 0);
220 g.place(1, 2, 4);
221 g.place(1, 3, 5);
222 g.place(2, 0, 6);
223 g.place(2, 1, 7);
224 g.place(2, 2, 0);
225 g.place(2, 3, 8);
226 return g;
227}
228
229function inSameSegment (grid, height, move)
230{
231 if (move.fromY === move.toY)
232 { return false; }
233 const lo = Math.min(move.fromY, move.toY);
234 const hi = Math.max(move.fromY, move.toY);
235 for (let y = lo; y <= hi; y++)
236 {
237 if (grid[move.col][y] == null)
238 { return false; }
239 }
240 return lo >= 0 && hi < height;
241}
242
243describe('Bot lookahead planner (t1144)', () =>
244{
245 it('lookaheadChance is 0 through 10% skill, then linear to 1 at 40%', () =>
246 {
247 assert.equal(Offline.lookaheadChance(0), 0);
248 assert.equal(Offline.lookaheadChance(0.05), 0);
249 assert.equal(Offline.lookaheadChance(0.10), 0);
250 assert.ok(Math.abs(Offline.lookaheadChance(0.25) - 0.5) < 1e-12);
251 assert.equal(Offline.lookaheadChance(0.40), 1);
252 assert.equal(Offline.lookaheadChance(1), 1);
253 });
254
255 it('finds a 1-step immediate match and prefers it over a setup', () =>
256 {
257 const g = makeGame({ noSpawn: true });
258 // Immediate: col0 [0,0,1,0] — also leave a 2-step shape on the right
259 // that must not be chosen while the 1-step exists.
260 g.place(0, 0, 0);
261 g.place(0, 1, 0);
262 g.place(0, 2, 1);
263 g.place(0, 3, 0);
264 g.place(1, 0, 3);
265 g.place(1, 1, 0);
266 g.place(1, 2, 4);
267 g.place(1, 3, 5);
268 g.place(2, 0, 6);
269 g.place(2, 1, 7);
270 g.place(2, 2, 0);
271 g.place(2, 3, 8);
272 const move = Offline.findBotMove(g);
273 assert.ok(move, 'expected an immediate move');
274 assert.equal(move.col, 0);
275 assert.equal(move.dist, 1);
276 const memory = { plan: null };
277 const r = Offline.tryBotRoll(g, 1, () => 0, memory);
278 assert.equal(r.success, true);
279 assert.equal(r.moved, true);
280 assert.equal(r.planned, undefined);
281 assert.equal(memory.plan, null);
282 assert.equal(r.move.col, 0);
283 assert.equal(r.move.dist, 1);
284 const launched = g.resolveMatches();
285 assert.ok(launched.length >= 1, 'immediate move must create a match');
286 });
287
288 it('finds a 2-step setup when no immediate match exists', () =>
289 {
290 const g = setupTwoStep(makeGame({ noSpawn: true }));
291 assert.equal(Offline.findBotMove(g), null, 'fixture must have no 1-step');
292 const plan = Offline.findSetupPlan(g);
293 assert.ok(plan && plan.steps && plan.steps.length === 2, 'expected a 2-step plan');
294 assert.equal(plan.steps[0].dist + plan.steps[1].dist, plan.total);
295 assert.ok(plan.total >= 2);
296 const firstLegal = Offline.listMovesFromCore(g).some((m) =>
297 m.col === plan.steps[0].col
298 && m.fromY === plan.steps[0].fromY
299 && m.toY === plan.steps[0].toY);
300 assert.ok(firstLegal, 'first step must be a legal core move');
301 assert.ok(Offline.executeBotMove(g, plan.steps[0]));
302 assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false,
303 'first step itself is a setup, not a match');
304 assert.ok(Offline.stepIsValid(g, plan.steps[1], true),
305 'second step must still be legal and create a match');
306 assert.ok(Offline.executeBotMove(g, plan.steps[1]));
307 const launched = g.resolveMatches();
308 assert.ok(launched.length >= 1, 'second step must create a match');
309 });
310
311 it('performs the first step and follows the plan on the next successful roll', () =>
312 {
313 const g = setupTwoStep(makeGame({ noSpawn: true }));
314 const memory = { plan: null };
315 const r1 = Offline.tryBotRoll(g, 1, () => 0, memory);
316 assert.equal(r1.success, true);
317 assert.equal(r1.moved, true);
318 assert.equal(r1.planned, true);
319 assert.ok(memory.plan && memory.plan.steps.length === 2);
320 assert.equal(memory.plan.index, 1);
321 assert.equal(Offline.hasMatch(Offline.snapshotGrid(g), g.width, g.height), false);
322
323 const r2 = Offline.tryBotRoll(g, 1, () => 0, memory);
324 assert.equal(r2.success, true);
325 assert.equal(r2.moved, true);
326 assert.equal(r2.followed, true);
327 assert.equal(memory.plan, null);
328 const launched = g.resolveMatches();
329 assert.ok(launched.length >= 1, 'followed step must create a match');
330 });
331
332 it('follows a committed plan even below the lookahead threshold', () =>
333 {
334 const g = setupTwoStep(makeGame({ noSpawn: true }));
335 const memory = { plan: null };
336 assert.ok(Offline.tryBotRoll(g, 1, () => 0, memory).planned);
337 // skill 5% is below the 10% lookahead floor, but the roll succeeds
338 // (0 < 0.05) and the stored second step is still valid.
339 assert.equal(Offline.lookaheadChance(0.05), 0);
340 const r = Offline.tryBotRoll(g, 0.05, () => 0, memory);
341 assert.equal(r.followed, true);
342 assert.equal(r.moved, true);
343 });
344
345 it('re-plans when the stored next step is no longer valid', () =>
346 {
347 const g = setupTwoStep(makeGame({ noSpawn: true }));
348 const memory = { plan: null };
349 const r1 = Offline.tryBotRoll(g, 1, () => 0, memory);
350 assert.ok(r1.planned && memory.plan);
351 const next = memory.plan.steps[1];
352 // After the first step, row 1 is a pair of 0s waiting for the
353 // third 0 from the second step. Recolor the middle 0 so that
354 // following the stored step no longer makes a match.
355 const mid = g.blockAt(1, 1);
356 assert.ok(mid);
357 assert.equal(mid.color, 0);
358 mid.color = 9;
359 const r2 = Offline.tryBotRoll(g, 1, () => 0, memory);
360 assert.notEqual(r2.followed, true);
361 // Plan was dropped; a new plan may or may not exist after the
362 // recolor, but the invalid step must not have been executed.
363 if (r2.moved)
364 {
365 assert.ok(
366 r2.move.col !== next.col
367 || r2.move.fromY !== next.fromY
368 || r2.move.toY !== next.toY
369 || r2.planned === true,
370 'must not blindly follow an invalid step'
371 );
372 }
373 });
374
375 it('does not start a new 2-step plan at or below 10% skill', () =>
376 {
377 const g = setupTwoStep(makeGame({ noSpawn: true }));
378 const memory = { plan: null };
379 // Successful roll (0 < 0.10) but lookaheadChance is 0, so no
380 // second rng() and no setup.
381 const r = Offline.tryBotRoll(g, 0.10, () => 0, memory);
382 assert.equal(r.success, true);
383 assert.equal(r.moved, false);
384 assert.equal(memory.plan, null);
385 assert.equal(Offline.findBotMove(g), null);
386 });
387
388 it('does not propose illegal swaps (gap, locked column, or off-segment)', () =>
389 {
390 const g = makeGame({ noSpawn: true });
391 // Gapped column 0: segments [0,1] and [3,4] — a cross-gap swap
392 // is illegal. Locked full column 1 is also illegal.
393 g.place(0, 0, 0);
394 g.place(0, 1, 1);
395 g.place(0, 3, 0);
396 g.place(0, 4, 2);
397 fillColumn(g, 1, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);
398 g.checkLocks();
399 assert.equal(g.isColumnLocked(1), true);
400 // Legal 2-step fixture on cols 2–4 (same shape, shifted).
401 g.place(2, 0, 0);
402 g.place(2, 1, 1);
403 g.place(2, 2, 2);
404 g.place(2, 3, 0);
405 g.place(3, 0, 3);
406 g.place(3, 1, 0);
407 g.place(3, 2, 4);
408 g.place(3, 3, 5);
409 g.place(4, 0, 6);
410 g.place(4, 1, 7);
411 g.place(4, 2, 0);
412 g.place(4, 3, 8);
413
414 const grid = Offline.snapshotGrid(g);
415 const coreMoves = Offline.listMovesFromCore(g);
416 for (const m of coreMoves)
417 {
418 assert.notEqual(m.col, 1, 'locked column must have no moves: ' + JSON.stringify(m));
419 assert.ok(inSameSegment(grid, g.height, m), 'core move crossed a gap: ' + JSON.stringify(m));
420 if (m.col === 0)
421 {
422 const low = m.fromY <= 1 && m.toY <= 1;
423 const high = m.fromY >= 3 && m.toY >= 3;
424 assert.ok(low || high, 'col 0 move crossed the gap: ' + JSON.stringify(m));
425 }
426 }
427
428 assert.equal(Offline.findBotMove(g), null);
429 const plan = Offline.findSetupPlan(g);
430 assert.ok(plan, 'expected a legal 2-step on cols 2–4');
431 assert.notEqual(plan.steps[0].col, 1);
432 assert.ok(inSameSegment(grid, g.height, plan.steps[0]));
433 const firstLegal = coreMoves.some((m) =>
434 m.col === plan.steps[0].col
435 && m.fromY === plan.steps[0].fromY
436 && m.toY === plan.steps[0].toY);
437 assert.ok(firstLegal, 'first plan step must be in listMovesFromCore');
438 assert.ok(Offline.executeBotMove(g, plan.steps[0]), 'first step must be executable');
439 const grid2 = Offline.snapshotGrid(g);
440 assert.ok(inSameSegment(grid2, g.height, plan.steps[1]),
441 'second step crossed a gap: ' + JSON.stringify(plan.steps[1]));
442 assert.ok(Offline.stepIsValid(g, plan.steps[1], true));
443 assert.ok(Offline.executeBotMove(g, plan.steps[1]));
444 });
445
446 it('finishes a full 10×13 search quickly and within budget', () =>
447 {
448 const g = makeGame({ noSpawn: true });
449 for (let c = 0; c < g.width; c++)
450 {
451 for (let r = 0; r < g.height; r++)
452 { g.place(c, r, (c + r) % 3); }
453 }
454 const t0 = Date.now();
455 const plan = Offline.findSetupPlan(g);
456 const ms = Date.now() - t0;
457 assert.ok(ms < 200, 'planner took ' + ms + ' ms on a full board');
458 if (plan)
459 {
460 const grid = Offline.snapshotGrid(g);
461 assert.ok(inSameSegment(grid, g.height, plan.steps[0]));
462 assert.ok(Offline.executeBotMove(g, plan.steps[0]));
463 }
464 });
465});