feat: bots look ahead up to two swaps when no immediate match (t1144)
After a successful chance roll, a bot still takes an immediate match first. If none exists, it may search a 2-step setup (skill-scaled: none at ≤10%, always at ≥40%), play the first swap, and remember the rest. The next successful roll follows that step if it is still valid, otherwise the bot re-plans. Search is budgeted and pruned for a 10×13 field. Offline only — there are no server-side online bots.
@@ -309,11 +309,263 @@ function executeBotMove (core, move)
309
309
return true;
310
310
}
311
311
312
/**
313
* Max board evaluations (apply + hasMatch + undo) for a depth-2 search
314
* in one AI tick. A 10×13 field with typical stacks stays well under this.
315
*/
316
var PLAN_BUDGET = 4000;
317
/** Skill at or below this: never start a 2-step setup (early rounds). */
318
var LOOKAHEAD_START = 0.10;
319
/** Skill at or above this: always look for a 2-step setup when no immediate match. */
320
var LOOKAHEAD_FULL = 0.40;
321
322
/**
323
* Probability of searching a 2-step setup after a successful roll
324
* that found no immediate match. 0 through 10% skill (rondes 1–6),
325
* then linear up to 1 at 40% skill (ronde 36).
326
*/
327
function lookaheadChance (skill)
328
{
329
skill = Number(skill) || 0;
330
if (skill <= LOOKAHEAD_START)
331
{ return 0; }
332
if (skill >= LOOKAHEAD_FULL)
333
{ return 1; }
334
return (skill - LOOKAHEAD_START) / (LOOKAHEAD_FULL - LOOKAHEAD_START);
335
}
336
337
function copyMove (m)
338
{
339
return {
340
col: m.col,
341
fromY: m.fromY,
342
toY: m.toY,
343
dist: m.dist,
344
blockId: m.blockId
345
};
346
}
347
348
function movesEqual (a, b)
349
{
350
return a && b && a.col === b.col && a.fromY === b.fromY && a.toY === b.toY;
351
}
352
353
function betterImmediate (cand, best)
354
{
355
if (!best)
356
{ return true; }
357
if (cand.dist !== best.dist)
358
{ return cand.dist < best.dist; }
359
if (cand.col !== best.col)
360
{ return cand.col < best.col; }
361
if (cand.fromY !== best.fromY)
362
{ return cand.fromY < best.fromY; }
363
return cand.toY < best.toY;
364
}
365
366
function betterPlan (cand, best)
367
{
368
if (!best)
369
{ return true; }
370
if (cand.total !== best.total)
371
{ return cand.total < best.total; }
372
var a0 = cand.steps[0];
373
var b0 = best.steps[0];
374
if (a0.dist !== b0.dist)
375
{ return a0.dist < b0.dist; }
376
if (a0.col !== b0.col || a0.fromY !== b0.fromY || a0.toY !== b0.toY)
377
{ return betterImmediate(a0, b0); }
378
return betterImmediate(cand.steps[1], best.steps[1]);
379
}
380
381
/**
382
* Legal vertical swaps from a colour grid: contiguous non-null cells
383
* in a column (gaps break a segment, same as swapToward).
384
*/
385
function listMovesFromGrid (grid, width, height)
386
{
387
var moves = [];
388
for (var col = 0; col < width; col++)
389
{
390
var start = -1;
391
for (var r = 0; r <= height; r++)
392
{
393
var filled = r < height && grid[col][r] != null;
394
if (filled)
395
{
396
if (start < 0)
397
{ start = r; }
398
}
399
else if (start >= 0)
400
{
401
var end = r;
402
if (end - start >= 2)
403
{
404
for (var i = start; i < end; i++)
405
{
406
for (var j = start; j < end; j++)
407
{
408
if (i === j)
409
{ continue; }
410
moves.push({
411
col: col,
412
fromY: i,
413
toY: j,
414
dist: Math.abs(j - i),
415
blockId: null
416
});
417
}
418
}
419
}
420
start = -1;
421
}
422
}
423
}
424
return moves;
425
}
426
427
function listMovesFromCore (core)
428
{
429
var moves = [];
430
for (var col = 0; col < core.width; col++)
431
{
432
var segs = contiguousSegments(core, col);
433
for (var s = 0; s < segs.length; s++)
434
{
435
var seg = segs[s];
436
if (seg.length < 2)
437
{ continue; }
438
for (var i = 0; i < seg.length; i++)
439
{
440
var fromY = Math.round(seg[i].y);
441
for (var j = 0; j < seg.length; j++)
442
{
443
if (i === j)
444
{ continue; }
445
var toY = Math.round(seg[j].y);
446
moves.push({
447
col: col,
448
fromY: fromY,
449
toY: toY,
450
dist: Math.abs(toY - fromY),
451
blockId: seg[i].id
452
});
453
}
454
}
455
}
456
}
457
return moves;
458
}
459
460
/**
461
* A 2-step setup: first swap creates no match, second swap does.
462
* Prefers smaller total distance, then shorter first step, then the
463
* same column / fromY / toY ties as findBotMove. First moves come
464
* from the live core (legal segments only). Budget-capped.
465
*/
466
function findSetupPlan (core, budget)
467
{
468
if (!core)
469
{ return null; }
470
var width = core.width;
471
var height = core.height;
472
var grid = snapshotGrid(core);
473
if (hasMatch(grid, width, height))
474
{ return null; }
475
budget = budget == null ? PLAN_BUDGET : budget;
476
var firstMoves = listMovesFromCore(core);
477
firstMoves.sort(function (a, b)
478
{
479
if (a.dist !== b.dist)
480
{ return a.dist - b.dist; }
481
if (a.col !== b.col)
482
{ return a.col - b.col; }
483
if (a.fromY !== b.fromY)
484
{ return a.fromY - b.fromY; }
485
return a.toY - b.toY;
486
});
487
var best = null;
488
var evals = 0;
489
for (var i = 0; i < firstMoves.length; i++)
490
{
491
var first = firstMoves[i];
492
if (best && first.dist + 1 > best.total)
493
{ break; }
494
applyColumnMove(grid, first.col, first.fromY, first.toY);
495
if (hasMatch(grid, width, height))
496
{
497
applyColumnMove(grid, first.col, first.toY, first.fromY);
498
continue;
499
}
500
var seconds = listMovesFromGrid(grid, width, height);
501
for (var j = 0; j < seconds.length; j++)
502
{
503
var second = seconds[j];
504
var total = first.dist + second.dist;
505
if (best && total > best.total)
506
{ continue; }
507
if (evals++ >= budget)
508
{ break; }
509
applyColumnMove(grid, second.col, second.fromY, second.toY);
510
var ok = hasMatch(grid, width, height);
511
applyColumnMove(grid, second.col, second.toY, second.fromY);
512
if (!ok)
513
{ continue; }
514
var cand = {
515
steps: [copyMove(first), copyMove(second)],
516
total: total
517
};
518
if (betterPlan(cand, best))
519
{ best = cand; }
520
}
521
applyColumnMove(grid, first.col, first.toY, first.fromY);
522
if (evals >= budget)
523
{ break; }
524
// 1+1 is the shortest possible setup; firstMoves is sorted so
525
// the first such plan is already the tie-break winner.
526
if (best && best.total === 2)
527
{ break; }
528
}
529
return best;
530
}
531
532
/**
533
* True when `step` is still a legal vertical swap on this core, and
534
* (when mustMatch) would create a 3+ match.
535
*/
536
function stepIsValid (core, step, mustMatch)
537
{
538
if (!core || !step)
539
{ return false; }
540
var moves = listMovesFromCore(core);
541
var found = null;
542
for (var i = 0; i < moves.length; i++)
543
{
544
if (movesEqual(moves[i], step))
545
{
546
found = moves[i];
547
break;
548
}
549
}
550
if (!found)
551
{ return false; }
552
step.blockId = found.blockId;
553
if (!mustMatch)
554
{ return true; }
555
var grid = snapshotGrid(core);
556
applyColumnMove(grid, step.col, step.fromY, step.toY);
557
return hasMatch(grid, core.width, core.height);
558
}
559
312
560
/**
313
561
* One AI tick. `rng` is a () => [0,1) function. Success if roll < skill.
314
562
* At skill 0 the bot never moves; at skill 1 it always looks for a move.
563
* `memory` is an optional object that stores a 2-step plan across ticks
564
* (`memory.plan`). On a later successful roll the bot follows the
565
* remaining step if it is still legal and still makes a match; otherwise
566
* it re-plans.
315
567
*/
316
function tryBotRoll (core, skill, rng)
568
function tryBotRoll (core, skill, rng, memory)
317
569
{
318
570
rng = rng || Math.random;
319
571
var roll = rng();
@@ -321,13 +573,73 @@ function tryBotRoll (core, skill, rng)
321
573
{
322
574
return { rolled: true, success: false, moved: false, roll: roll, move: null };
323
575
}
576
var grid = snapshotGrid(core);
577
if (hasMatch(grid, core.width, core.height))
578
{
579
if (memory)
580
{ memory.plan = null; }
581
return { rolled: true, success: true, moved: false, roll: roll, move: null };
582
}
583
if (memory && memory.plan && memory.plan.steps
584
&& memory.plan.index < memory.plan.steps.length)
585
{
586
var step = memory.plan.steps[memory.plan.index];
587
var last = memory.plan.index === memory.plan.steps.length - 1;
588
if (stepIsValid(core, step, last))
589
{
590
var followed = executeBotMove(core, step);
591
if (followed)
592
{
593
memory.plan.index++;
594
if (memory.plan.index >= memory.plan.steps.length)
595
{ memory.plan = null; }
596
return {
597
rolled: true,
598
success: true,
599
moved: true,
600
roll: roll,
601
move: step,
602
followed: true
603
};
604
}
605
}
606
memory.plan = null;
607
}
324
608
var move = findBotMove(core);
325
if (!move)
609
if (move)
610
{
611
if (memory)
612
{ memory.plan = null; }
613
var ok = executeBotMove(core, move);
614
return { rolled: true, success: true, moved: !!ok, roll: roll, move: move };
615
}
616
var chance = lookaheadChance(skill);
617
if (chance <= 0 || !(rng() < chance))
326
618
{
327
619
return { rolled: true, success: true, moved: false, roll: roll, move: null };
328
620
}
329
var ok = executeBotMove(core, move);
330
return { rolled: true, success: true, moved: !!ok, roll: roll, move: move };
621
var plan = findSetupPlan(core);
622
if (!plan || !plan.steps || !plan.steps[0])
623
{
624
return { rolled: true, success: true, moved: false, roll: roll, move: null };
625
}
626
var first = plan.steps[0];
627
var started = executeBotMove(core, first);
628
if (started && memory)
629
{
630
memory.plan = { steps: plan.steps, index: 1, total: plan.total };
631
}
632
else if (memory)
633
{ memory.plan = null; }
634
return {
635
rolled: true,
636
success: true,
637
moved: !!started,
638
roll: roll,
639
move: first,
640
planned: true,
641
plan: plan
642
};
331
643
}
332
644
333
645
function pickPlanet (rng, P)
@@ -375,7 +687,8 @@ OfflineBattle.prototype._initBots = function ()
375
687
core: core,
376
688
eliminated: false,
377
689
score: 0,
378
aiAcc: 0
690
aiAcc: 0,
691
plan: null
379
692
});
380
693
}
381
694
};
@@ -524,7 +837,7 @@ OfflineBattle.prototype.tick = function (dt, playerCore)
524
837
{
525
838
bot.aiAcc -= BOT_INTERVAL;
526
839
if (bot.core.state === 'playing' && !bot.core.drag)
527
{ tryBotRoll(bot.core, skill, this.rng); }
840
{ tryBotRoll(bot.core, skill, this.rng, bot); }
528
841
}
529
842
}
530
843
@@ -588,8 +901,16 @@ var api = {
588
901
skillForRound: skillForRound,
589
902
scheduleForRound: scheduleForRound,
590
903
findBotMove: findBotMove,
904
findSetupPlan: findSetupPlan,
591
905
tryBotRoll: tryBotRoll,
592
906
executeBotMove: executeBotMove,
907
stepIsValid: stepIsValid,
908
lookaheadChance: lookaheadChance,
909
listMovesFromCore: listMovesFromCore,
910
listMovesFromGrid: listMovesFromGrid,
911
PLAN_BUDGET: PLAN_BUDGET,
912
LOOKAHEAD_START: LOOKAHEAD_START,
913
LOOKAHEAD_FULL: LOOKAHEAD_FULL,
593
914
snapshotGrid: snapshotGrid,
594
915
applyColumnMove: applyColumnMove,
595
916
hasMatch: hasMatch,
@@ -309,11 +309,263 @@ function executeBotMove (core, move)309
return true;309
return true;310
}310
}311
311
312
/**313
* Max board evaluations (apply + hasMatch + undo) for a depth-2 search314
* in one AI tick. A 10×13 field with typical stacks stays well under this.315
*/316
var PLAN_BUDGET = 4000;317
/** Skill at or below this: never start a 2-step setup (early rounds). */318
var LOOKAHEAD_START = 0.10;319
/** Skill at or above this: always look for a 2-step setup when no immediate match. */320
var LOOKAHEAD_FULL = 0.40;321
322
/**323
* Probability of searching a 2-step setup after a successful roll324
* that found no immediate match. 0 through 10% skill (rondes 1–6),325
* then linear up to 1 at 40% skill (ronde 36).326
*/327
function lookaheadChance (skill)328
{329
skill = Number(skill) || 0;330
if (skill <= LOOKAHEAD_START)331
{ return 0; }332
if (skill >= LOOKAHEAD_FULL)333
{ return 1; }334
return (skill - LOOKAHEAD_START) / (LOOKAHEAD_FULL - LOOKAHEAD_START);335
}336
337
function copyMove (m)338
{339
return {340
col: m.col,341
fromY: m.fromY,342
toY: m.toY,343
dist: m.dist,344
blockId: m.blockId345
};346
}347
348
function movesEqual (a, b)349
{350
return a && b && a.col === b.col && a.fromY === b.fromY && a.toY === b.toY;351
}352
353
function betterImmediate (cand, best)354
{355
if (!best)356
{ return true; }357
if (cand.dist !== best.dist)358
{ return cand.dist < best.dist; }359
if (cand.col !== best.col)360
{ return cand.col < best.col; }361
if (cand.fromY !== best.fromY)362
{ return cand.fromY < best.fromY; }363
return cand.toY < best.toY;364
}365
366
function betterPlan (cand, best)367
{368
if (!best)369
{ return true; }370
if (cand.total !== best.total)371
{ return cand.total < best.total; }372
var a0 = cand.steps[0];373
var b0 = best.steps[0];374
if (a0.dist !== b0.dist)375
{ return a0.dist < b0.dist; }376
if (a0.col !== b0.col || a0.fromY !== b0.fromY || a0.toY !== b0.toY)377
{ return betterImmediate(a0, b0); }378
return betterImmediate(cand.steps[1], best.steps[1]);379
}380
381
/**382
* Legal vertical swaps from a colour grid: contiguous non-null cells383
* in a column (gaps break a segment, same as swapToward).384
*/385
function listMovesFromGrid (grid, width, height)386
{387
var moves = [];388
for (var col = 0; col < width; col++)389
{390
var start = -1;391
for (var r = 0; r <= height; r++)392
{393
var filled = r < height && grid[col][r] != null;394
if (filled)395
{396
if (start < 0)397
{ start = r; }398
}399
else if (start >= 0)400
{401
var end = r;402
if (end - start >= 2)403
{404
for (var i = start; i < end; i++)405
{406
for (var j = start; j < end; j++)407
{408
if (i === j)409
{ continue; }410
moves.push({411
col: col,412
fromY: i,413
toY: j,414
dist: Math.abs(j - i),415
blockId: null416
});417
}418
}419
}420
start = -1;421
}422
}423
}424
return moves;425
}426
427
function listMovesFromCore (core)428
{429
var moves = [];430
for (var col = 0; col < core.width; col++)431
{432
var segs = contiguousSegments(core, col);433
for (var s = 0; s < segs.length; s++)434
{435
var seg = segs[s];436
if (seg.length < 2)437
{ continue; }438
for (var i = 0; i < seg.length; i++)439
{440
var fromY = Math.round(seg[i].y);441
for (var j = 0; j < seg.length; j++)442
{443
if (i === j)444
{ continue; }445
var toY = Math.round(seg[j].y);446
moves.push({447
col: col,448
fromY: fromY,449
toY: toY,450
dist: Math.abs(toY - fromY),451
blockId: seg[i].id452
});453
}454
}455
}456
}457
return moves;458
}459
460
/**461
* A 2-step setup: first swap creates no match, second swap does.462
* Prefers smaller total distance, then shorter first step, then the463
* same column / fromY / toY ties as findBotMove. First moves come464
* from the live core (legal segments only). Budget-capped.465
*/466
function findSetupPlan (core, budget)467
{468
if (!core)469
{ return null; }470
var width = core.width;471
var height = core.height;472
var grid = snapshotGrid(core);473
if (hasMatch(grid, width, height))474
{ return null; }475
budget = budget == null ? PLAN_BUDGET : budget;476
var firstMoves = listMovesFromCore(core);477
firstMoves.sort(function (a, b)478
{479
if (a.dist !== b.dist)480
{ return a.dist - b.dist; }481
if (a.col !== b.col)482
{ return a.col - b.col; }483
if (a.fromY !== b.fromY)484
{ return a.fromY - b.fromY; }485
return a.toY - b.toY;486
});487
var best = null;488
var evals = 0;489
for (var i = 0; i < firstMoves.length; i++)490
{491
var first = firstMoves[i];492
if (best && first.dist + 1 > best.total)493
{ break; }494
applyColumnMove(grid, first.col, first.fromY, first.toY);495
if (hasMatch(grid, width, height))496
{497
applyColumnMove(grid, first.col, first.toY, first.fromY);498
continue;499
}500
var seconds = listMovesFromGrid(grid, width, height);501
for (var j = 0; j < seconds.length; j++)502
{503
var second = seconds[j];504
var total = first.dist + second.dist;505
if (best && total > best.total)506
{ continue; }507
if (evals++ >= budget)508
{ break; }509
applyColumnMove(grid, second.col, second.fromY, second.toY);510
var ok = hasMatch(grid, width, height);511
applyColumnMove(grid, second.col, second.toY, second.fromY);512
if (!ok)513
{ continue; }514
var cand = {515
steps: [copyMove(first), copyMove(second)],516
total: total517
};518
if (betterPlan(cand, best))519
{ best = cand; }520
}521
applyColumnMove(grid, first.col, first.toY, first.fromY);522
if (evals >= budget)523
{ break; }524
// 1+1 is the shortest possible setup; firstMoves is sorted so525
// the first such plan is already the tie-break winner.526
if (best && best.total === 2)527
{ break; }528
}529
return best;530
}531
532
/**533
* True when `step` is still a legal vertical swap on this core, and534
* (when mustMatch) would create a 3+ match.535
*/536
function stepIsValid (core, step, mustMatch)537
{538
if (!core || !step)539
{ return false; }540
var moves = listMovesFromCore(core);541
var found = null;542
for (var i = 0; i < moves.length; i++)543
{544
if (movesEqual(moves[i], step))545
{546
found = moves[i];547
break;548
}549
}550
if (!found)551
{ return false; }552
step.blockId = found.blockId;553
if (!mustMatch)554
{ return true; }555
var grid = snapshotGrid(core);556
applyColumnMove(grid, step.col, step.fromY, step.toY);557
return hasMatch(grid, core.width, core.height);558
}559
312
/**560
/**313
* One AI tick. `rng` is a () => [0,1) function. Success if roll < skill.561
* One AI tick. `rng` is a () => [0,1) function. Success if roll < skill.314
* At skill 0 the bot never moves; at skill 1 it always looks for a move.562
* At skill 0 the bot never moves; at skill 1 it always looks for a move.563
* `memory` is an optional object that stores a 2-step plan across ticks564
* (`memory.plan`). On a later successful roll the bot follows the565
* remaining step if it is still legal and still makes a match; otherwise566
* it re-plans.315
*/567
*/316
function tryBotRoll (core, skill, rng)568
function tryBotRoll (core, skill, rng, memory)317
{569
{318
rng = rng || Math.random;570
rng = rng || Math.random;319
var roll = rng();571
var roll = rng();@@ -321,13 +573,73 @@ function tryBotRoll (core, skill, rng)321
{573
{322
return { rolled: true, success: false, moved: false, roll: roll, move: null };574
return { rolled: true, success: false, moved: false, roll: roll, move: null };323
}575
}576
var grid = snapshotGrid(core);577
if (hasMatch(grid, core.width, core.height))578
{579
if (memory)580
{ memory.plan = null; }581
return { rolled: true, success: true, moved: false, roll: roll, move: null };582
}583
if (memory && memory.plan && memory.plan.steps584
&& memory.plan.index < memory.plan.steps.length)585
{586
var step = memory.plan.steps[memory.plan.index];587
var last = memory.plan.index === memory.plan.steps.length - 1;588
if (stepIsValid(core, step, last))589
{590
var followed = executeBotMove(core, step);591
if (followed)592
{593
memory.plan.index++;594
if (memory.plan.index >= memory.plan.steps.length)595
{ memory.plan = null; }596
return {597
rolled: true,598
success: true,599
moved: true,600
roll: roll,601
move: step,602
followed: true603
};604
}605
}606
memory.plan = null;607
}324
var move = findBotMove(core);608
var move = findBotMove(core);325
if (!move)609
if (move)610
{611
if (memory)612
{ memory.plan = null; }613
var ok = executeBotMove(core, move);614
return { rolled: true, success: true, moved: !!ok, roll: roll, move: move };615
}616
var chance = lookaheadChance(skill);617
if (chance <= 0 || !(rng() < chance))326
{618
{327
return { rolled: true, success: true, moved: false, roll: roll, move: null };619
return { rolled: true, success: true, moved: false, roll: roll, move: null };328
}620
}329
var ok = executeBotMove(core, move);621
var plan = findSetupPlan(core);330
return { rolled: true, success: true, moved: !!ok, roll: roll, move: move };622
if (!plan || !plan.steps || !plan.steps[0])623
{624
return { rolled: true, success: true, moved: false, roll: roll, move: null };625
}626
var first = plan.steps[0];627
var started = executeBotMove(core, first);628
if (started && memory)629
{630
memory.plan = { steps: plan.steps, index: 1, total: plan.total };631
}632
else if (memory)633
{ memory.plan = null; }634
return {635
rolled: true,636
success: true,637
moved: !!started,638
roll: roll,639
move: first,640
planned: true,641
plan: plan642
};331
}643
}332
644
333
function pickPlanet (rng, P)645
function pickPlanet (rng, P)@@ -375,7 +687,8 @@ OfflineBattle.prototype._initBots = function ()375
core: core,687
core: core,376
eliminated: false,688
eliminated: false,377
score: 0,689
score: 0,378
aiAcc: 0690
aiAcc: 0,691
plan: null379
});692
});380
}693
}381
};694
};@@ -524,7 +837,7 @@ OfflineBattle.prototype.tick = function (dt, playerCore)524
{837
{525
bot.aiAcc -= BOT_INTERVAL;838
bot.aiAcc -= BOT_INTERVAL;526
if (bot.core.state === 'playing' && !bot.core.drag)839
if (bot.core.state === 'playing' && !bot.core.drag)527
{ tryBotRoll(bot.core, skill, this.rng); }840
{ tryBotRoll(bot.core, skill, this.rng, bot); }528
}841
}529
}842
}530
843
@@ -588,8 +901,16 @@ var api = {588
skillForRound: skillForRound,901
skillForRound: skillForRound,589
scheduleForRound: scheduleForRound,902
scheduleForRound: scheduleForRound,590
findBotMove: findBotMove,903
findBotMove: findBotMove,904
findSetupPlan: findSetupPlan,591
tryBotRoll: tryBotRoll,905
tryBotRoll: tryBotRoll,592
executeBotMove: executeBotMove,906
executeBotMove: executeBotMove,907
stepIsValid: stepIsValid,908
lookaheadChance: lookaheadChance,909
listMovesFromCore: listMovesFromCore,910
listMovesFromGrid: listMovesFromGrid,911
PLAN_BUDGET: PLAN_BUDGET,912
LOOKAHEAD_START: LOOKAHEAD_START,913
LOOKAHEAD_FULL: LOOKAHEAD_FULL,593
snapshotGrid: snapshotGrid,914
snapshotGrid: snapshotGrid,594
applyColumnMove: applyColumnMove,915
applyColumnMove: applyColumnMove,595
hasMatch: hasMatch,916
hasMatch: hasMatch,