BlueGit

projects-timfalken-com meteors

Files

meteors / test / offline.test.js

offline.test.js

offline.test.js 15.6 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, fillColumn, Planet, Rng, GameCore } = require('./helpers.js');
6const Offline = require('../src/offline.js');
7
8describe('Offline Strijd progression (t1119)', () =>
9{
10 it('one opponent for one round, then N opponents for N-1 rounds', () =>
11 {
12 assert.equal(Offline.roundsAtOpponentCount(1), 1);
13 assert.equal(Offline.roundsAtOpponentCount(2), 1);
14 assert.equal(Offline.roundsAtOpponentCount(3), 2);
15 assert.equal(Offline.roundsAtOpponentCount(6), 5);
16
17 const expect = {
18 1: { opponents: 1 },
19 2: { opponents: 2 },
20 3: { opponents: 3 },
21 4: { opponents: 3 },
22 5: { opponents: 4 },
23 6: { opponents: 4 },
24 7: { opponents: 4 },
25 8: { opponents: 5 }
26 };
27 for (const [round, exp] of Object.entries(expect))
28 {
29 const s = Offline.scheduleForRound(Number(round));
30 assert.equal(s.opponents, exp.opponents, 'round ' + round + ' opponents=' + s.opponents);
31 assert.equal(s.round, Number(round));
32 }
33 });
34
35 it('skill starts at 5% and rises 1% per round, capped at 100%', () =>
36 {
37 assert.equal(Offline.skillForRound(1), 0.05);
38 assert.equal(Offline.skillForRound(2), 0.06);
39 assert.equal(Offline.skillForRound(96), 1);
40 assert.equal(Offline.skillForRound(97), 1);
41 assert.equal(Offline.skillForRound(200), 1);
42 assert.equal(Offline.scheduleForRound(1).skill, 0.05);
43 assert.equal(Offline.scheduleForRound(10).skill, 0.14);
44 });
45});
46
47describe('Bot move finder and roll (t1119)', () =>
48{
49 it('finds the shortest vertical move that creates a match', () =>
50 {
51 const g = makeGame({ noSpawn: true });
52 // Column 0: [0, 0, 1, 0] — moving the 1 up (or the top 0 down) makes [0,0,0,1].
53 g.place(0, 0, 0);
54 g.place(0, 1, 0);
55 g.place(0, 2, 1);
56 g.place(0, 3, 0);
57 const move = Offline.findBotMove(g);
58 assert.ok(move, 'expected a move');
59 assert.equal(move.col, 0);
60 assert.equal(move.dist, 1);
61 assert.ok(
62 (move.fromY === 2 && move.toY === 3) || (move.fromY === 3 && move.toY === 2),
63 'from=' + move.fromY + ' to=' + move.toY
64 );
65 assert.ok(Offline.executeBotMove(g, move));
66 const launched = g.resolveMatches();
67 assert.ok(launched.length >= 1, 'the move must create a match');
68 });
69
70 it('returns null when no vertical move creates a match', () =>
71 {
72 const g = makeGame({ noSpawn: true });
73 g.place(0, 0, 0);
74 g.place(0, 1, 1);
75 g.place(0, 2, 2);
76 g.place(1, 0, 3);
77 g.place(1, 1, 4);
78 assert.equal(Offline.findBotMove(g), null);
79 });
80
81 it('a roll at skill 0 never moves, skill 1 always tries', () =>
82 {
83 const setup = () =>
84 {
85 const g = makeGame({ noSpawn: true });
86 g.place(0, 0, 0);
87 g.place(0, 1, 0);
88 g.place(0, 2, 1);
89 g.place(0, 3, 0);
90 return g;
91 };
92 const g0 = setup();
93 const r0 = Offline.tryBotRoll(g0, 0, () => 0);
94 assert.equal(r0.success, false);
95 assert.equal(r0.moved, false);
96 assert.equal(g0.blocks[2].y, 2, 'nothing moved at skill 0 even with roll 0');
97
98 const g1 = setup();
99 const r1 = Offline.tryBotRoll(g1, 1, () => 0.999);
100 assert.equal(r1.success, true);
101 assert.equal(r1.moved, true);
102 assert.ok(r1.move);
103
104 // roll == skill is a failure (success only when roll < skill)
105 const g2 = setup();
106 const r2 = Offline.tryBotRoll(g2, 0.05, () => 0.05);
107 assert.equal(r2.success, false);
108 assert.equal(r2.moved, false);
109 });
110});
111
112describe('Offline battle flow (t1119)', () =>
113{
114 it('starts a round with the scheduled number of bots, each a random planet', () =>
115 {
116 const rng = Rng.create(42);
117 const battle = Offline.createBattle({
118 playerPlanet: Planet.defaultPlanet(),
119 rng: rng,
120 round: 5
121 });
122 assert.equal(battle.schedule.opponents, 4);
123 assert.equal(battle.bots.length, 4);
124 assert.equal(battle.schedule.skill, Offline.skillForRound(5));
125 for (const bot of battle.bots)
126 {
127 assert.ok(bot.planet && bot.planet.id);
128 assert.ok(bot.core instanceof GameCore);
129 assert.equal(bot.eliminated, false);
130 }
131 const ids = new Set(battle.bots.map((b) => b.planet.id));
132 // With 12 planets and 4 bots, seed 42 is very likely to vary; just
133 // assert they are real catalogue planets.
134 const catalogue = new Set(Planet.allPlanets().map((p) => p.id));
135 for (const id of ids)
136 { assert.ok(catalogue.has(id), 'unknown planet ' + id); }
137 });
138
139 it('a forced miss never delivers garbage; a forced hit does', () =>
140 {
141 const player = makeGame({ noSpawn: true });
142 // Forced miss: rng always returns 0 for pick? miss if rng() < missChance.
143 // Use a planet with 60% miss (power 0) and rng that always returns 0
144 // → always miss? 0 < 0.60 is true, so miss. Wait, I used rng() < missChance
145 // in battle... _dispatchShots: miss = targets.length===0 || this.rng() < missChance
146 // Yes 0 < 0.60 → miss.
147 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' });
148 let n = 0;
149 const missRng = () => { n++; return 0; }; // always miss (0 < 0.60)
150 const battle = Offline.createBattle({ playerPlanet: easy, rng: missRng, round: 1 });
151 // Give the player an outgoing shot without actually flying.
152 player.outgoingShots.push({ id: 99, color: 1, col: 0, y: 13 });
153 battle.tick(0.01, player);
154 assert.ok(battle.flightResults.length >= 1);
155 assert.equal(battle.flightResults[0].miss, true);
156 assert.equal(battle.pending.length, 0);
157
158 // Forced hit: rng always 0.99, missChance 0.60, 0.99 < 0.60 is false.
159 const hitRng = () => 0.99;
160 const battle2 = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 });
161 const player2 = makeGame({ noSpawn: true });
162 player2.outgoingShots.push({ id: 7, color: 2, col: 1, y: 13 });
163 const botBlocksBefore = battle2.bots[0].core.blocks.length;
164 battle2.tick(0.01, player2);
165 assert.equal(battle2.flightResults[0].miss, false);
166 assert.equal(battle2.pending.length, 1);
167 // Deliver after the flight duration.
168 battle2.tick(Offline.FLIGHT_DURATION + 0.05, player2);
169 assert.equal(battle2.pending.length, 0);
170 assert.ok(battle2.bots[0].core.blocks.length > botBlocksBefore,
171 'garbage was ingested by the bot');
172 });
173
174 it('winning a round means every bot is defeated, then advanceRound grows the field', () =>
175 {
176 const rng = Rng.create(7);
177 const battle = Offline.createBattle({
178 playerPlanet: Planet.defaultPlanet(),
179 rng,
180 round: 1
181 });
182 assert.equal(battle.bots.length, 1);
183 const player = makeGame({ noSpawn: true });
184 battle.bots[0].core.state = 'gameover';
185 battle.tick(0.05, player);
186 assert.equal(battle.state, 'round_won');
187 assert.equal(battle.aliveBots().length, 0);
188 battle.advanceRound();
189 assert.equal(battle.round, 2);
190 assert.equal(battle.state, 'playing');
191 assert.equal(battle.bots.length, 2);
192 assert.equal(battle.schedule.skill, 0.06);
193 });
194
195 it('the player losing sets state to lost at the current round', () =>
196 {
197 const battle = Offline.createBattle({
198 playerPlanet: Planet.defaultPlanet(),
199 rng: Rng.create(1),
200 round: 3
201 });
202 const player = makeGame({ noSpawn: true });
203 player.state = 'gameover';
204 battle.tick(0.05, player);
205 assert.equal(battle.state, 'lost');
206 assert.equal(battle.round, 3);
207 });
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});