feat: add solve() console command for auto-solving

Call solve() in browser console to auto-solve one move per second.
Call solve() again to stop. Stops automatically on game over.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 22:07:31 +07:00
co-authored by Claude Opus 4.6
parent 9de47575d8
commit 8212244302
@@ -21,6 +21,7 @@ export class GameScene extends Phaser.Scene {
private timerEvent: Phaser.Time.TimerEvent | null = null;
private hintHandler!: () => void;
private shuffleHandler!: () => void;
private solveInterval: number | null = null;
constructor() {
super({ key: "GameScene" });
@@ -49,7 +50,11 @@ export class GameScene extends Phaser.Scene {
}
this.stateManager.off("hint", this.hintHandler);
this.stateManager.off("shuffle", this.shuffleHandler);
this.stopSolve();
});
// Expose solve() on window for console use
(window as unknown as Record<string, unknown>).solve = () => this.startSolve();
}
private renderBoard(): void {
@@ -322,6 +327,56 @@ export class GameScene extends Phaser.Scene {
this.renderBoard();
}
private startSolve(): void {
if (this.solveInterval !== null) {
this.stopSolve();
console.log("Auto-solve stopped.");
return;
}
console.log("Auto-solve started. Call solve() again to stop.");
this.solveOneMove();
this.solveInterval = window.setInterval(() => {
if (this.isProcessing) return;
const state = this.stateManager.getState();
if (state.status !== "playing") {
this.stopSolve();
return;
}
this.solveOneMove();
}, 1000);
}
private stopSolve(): void {
if (this.solveInterval !== null) {
clearInterval(this.solveInterval);
this.solveInterval = null;
}
}
private solveOneMove(): void {
if (this.isProcessing) return;
const remaining = getRemainingTiles(this.board);
for (let i = 0; i < remaining.length; i++) {
for (let j = i + 1; j < remaining.length; j++) {
if (remaining[i].tile.emoji === remaining[j].tile.emoji) {
const path = findPath(this.board, remaining[i].pos, remaining[j].pos);
if (path) {
this.clearSelection();
this.selectedTile = remaining[i].pos;
this.highlightTile(remaining[i].pos);
this.isProcessing = true;
this.handleMatch(remaining[i].pos, remaining[j].pos, path);
return;
}
}
}
}
// No valid move found — shuffle
if (remaining.length > 0) {
this.autoShuffle();
}
}
shutdown(): void {
if (this.timerEvent) {
this.timerEvent.destroy();