Track mistakes per cell so corrections and undo/redo adjust the count

- A wrong entry now counts once; erasing, correcting, or undoing it decrements
  the counter instead of accumulating duplicates on every edit.
- Persist mistakeCells in the save and rebuild from existing grid data for
  saves created before this field existed.
This commit is contained in:
Brian Fertig 2026-11-26 13:27:54 -07:00
parent 0223a191fc
commit 14c17d2eb0
No known key found for this signature in database
3 changed files with 39 additions and 7 deletions

View File

@ -90,6 +90,7 @@ export default class TectonicGame extends Phaser.Scene {
this.elapsed = 0;
this.timerOn = false;
this.hintsLeft = 0; this.hintsUsed = 0; this.mistakes = 0;
this.mistakeCells = {}; // "r,c" → 1 for wrong entries still on the map
this.revealed = false;
this.moves = 0;
this.gameEnded = false;
@ -358,7 +359,18 @@ export default class TectonicGame extends Phaser.Scene {
this.history = s.history;
this.elapsed = s.elapsed;
this.hintsLeft = s.hintsLeft; this.hintsUsed = s.hintsUsed; this.mistakes = s.mistakes;
this.mistakeCells = {};
if (s.mistakeCells && typeof s.mistakeCells === 'object') {
for (const [k, v] of Object.entries(s.mistakeCells))
if (/^[0-7],[0-7]$/.test(k) && v === 1) this.mistakeCells[k] = 1;
}
this.revealed = s.revealed;
if (!Object.keys(this.mistakeCells).length && this.isChecking()) {
// Older save without mistakeCells: rebuild — every wrong entry was already counted.
for (let r = 0; r < N; r++) for (let c = 0; c < N; c++)
if (!this.givens[r][c] && this.grid[r][c] && this.grid[r][c] !== this.solution[r][c])
this.mistakeCells[`${r},${c}`] = 1;
}
this.moves = s.moves;
this.surveyNo = s.surveyNo || 1000 + Math.floor(Math.random() * 9000);
this.timerOn = this.moves > 0;
@ -807,7 +819,7 @@ export default class TectonicGame extends Phaser.Scene {
pushEdit(this.history, { r, c, from, to: v });
this.moves++;
this.timerOn = true;
this.applyCell(r, c, v, { anim: true, countMistake: true });
this.applyCell(r, c, v, { anim: true });
}
rejectValue(r, c, size) {
@ -828,7 +840,7 @@ export default class TectonicGame extends Phaser.Scene {
// Set a cell's value and run the feedback rules. Used by writes, undo/redo
// and hints.
applyCell(r, c, v, { anim = false, countMistake = false } = {}) {
applyCell(r, c, v, { anim = false } = {}) {
const from = this.grid[r][c];
this.grid[r][c] = v;
this.renderCellText(r, c);
@ -846,10 +858,11 @@ export default class TectonicGame extends Phaser.Scene {
if (!v || this.givens[r][c]) {
this.cellState[r][c] = 0;
this.setMistake(r, c, false);
} else if (this.isChecking()) {
const ok = v === this.solution[r][c];
this.cellState[r][c] = ok ? 1 : 2;
if (!ok && countMistake) this.mistakes++;
this.setMistake(r, c, !ok);
} else {
this.cellState[r][c] = 0;
}
@ -869,6 +882,19 @@ export default class TectonicGame extends Phaser.Scene {
this.saveGame();
}
// Mistakes are tracked per cell: a wrong entry counts once, and erasing or
// correcting it (new write, undo/redo or hint) takes the count back, so a
// fixed mistake returns the counter to what it was before.
setMistake(r, c, wrong) {
const key = `${r},${c}`;
if (wrong) {
if (this.mistakeCells[key] !== 1) { this.mistakeCells[key] = 1; this.mistakes++; }
} else if (this.mistakeCells[key] === 1) {
delete this.mistakeCells[key];
this.mistakes--;
}
}
allFilled() {
for (let r = 0; r < N; r++) for (let c = 0; c < N; c++) if (!this.grid[r][c]) return false;
return true;
@ -885,9 +911,8 @@ export default class TectonicGame extends Phaser.Scene {
if (this.givens[r][c]) continue;
const ok = this.grid[r][c] === this.solution[r][c];
this.cellState[r][c] = ok ? 1 : 2;
if (!ok) wrong++;
if (!ok) { wrong++; this.setMistake(r, c, true); }
}
this.mistakes += wrong;
for (const id of this.meta.cages.keys()) this.drawWash(id);
this.recomputeCageDone(!silent);
this.refreshStats();
@ -1078,6 +1103,7 @@ export default class TectonicGame extends Phaser.Scene {
grid: this.grid, solution: this.solution, regions: this.regions, givens: this.givens,
cellState: this.cellState, history: this.history, elapsed: this.elapsed,
hintsLeft: this.hintsLeft, hintsUsed: this.hintsUsed, mistakes: this.mistakes,
mistakeCells: this.mistakeCells,
revealed: this.revealed, moves: this.moves, surveyNo: this.surveyNo,
hinted: [...this.hinted],
}));

View File

@ -243,6 +243,7 @@ export function serialiseSave(s) {
hintsLeft: Number.isFinite(s.hintsLeft) ? s.hintsLeft : null,
hintsUsed: s.hintsUsed ?? 0,
mistakes: s.mistakes ?? 0,
mistakeCells: (s.mistakeCells && typeof s.mistakeCells === 'object') ? s.mistakeCells : {},
revealed: !!s.revealed,
moves: s.moves ?? 0,
surveyNo: s.surveyNo ?? 0,

View File

@ -453,6 +453,7 @@ section('Scene smoke run');
sc.doRedo();
sc.writeCell(bad.r, bad.c, p.solution[bad.r][bad.c]);
flush();
ok('fixing the mistake takes the count back', sc.mistakes === 0);
ok('fixing the last mistake wins', sc.gameEnded === true);
ok('win clears the save', globalThis.localStorage.getItem('tectonic-save') === null);
const stats = JSON.parse(globalThis.localStorage.getItem('solitaire-stats-tectonic') || '{}');
@ -475,11 +476,15 @@ section('Scene smoke run');
sc.armed = wrong;
sc.onCellPointer(t.r, t.c);
ok('easy: a wrong entry is marked and counted', sc.cellState[t.r][t.c] === 2 && sc.mistakes === 1);
sc.doUndo();
ok('undoing a mistake takes the count back', sc.grid[t.r][t.c] === 0 && sc.mistakes === 0);
sc.doRedo();
ok('redo brings the mistake back', sc.grid[t.r][t.c] === wrong && sc.mistakes === 1);
sc.onCellPointer(t.r, t.c);
ok('armed tag on the same value erases', sc.grid[t.r][t.c] === 0);
ok('armed tag on the same value erases', sc.grid[t.r][t.c] === 0 && sc.mistakes === 0);
sc.armed = p.solution[t.r][t.c];
sc.onCellPointer(t.r, t.c);
ok('easy: a right entry is marked right', sc.cellState[t.r][t.c] === 1 && sc.mistakes === 1);
ok('easy: a right entry is marked right', sc.cellState[t.r][t.c] === 1 && sc.mistakes === 0);
sc.onTagPointer(sc.armed);
ok('tapping the armed tag disarms it', sc.armed == null);