Engine cleanups (closes #1 #2 #3 #4 #6 #7)#16
Merged
Conversation
…#1 #2 #3 #4 #6 #7) * #1 Off-by-one in #checkCol / #checkRow: `col > #cols` → `col >= #cols`. Valid columns are 0..cols-1; col === cols previously slipped through the guard and then crashed on this.#field[cols]. * #2 Negative bounds in #checkIndex / #checkCol / #checkRow: added `< 0` checks alongside the upper bounds. Number.isInteger(-1) is true and -1 > cols is false, so negatives also slipped through. * #3 #lose() now passes cell.reveal(true) so each cell reveals locally without triggering its zero-neighbour flood-fill. Result was correct before (already-revealed cells short-circuit) but it ran a recursive cascade per empty cell. * #4 Dropped dead `this.#isGameOver` arg passed to cell.reveal() in Minesweeper.reveal — the early-return at the top of reveal already guarantees #isGameOver is false here. * #6 Removed redundant `&& !this.#isMarked` from Cell.mark()'s second branch — the first branch already returns when #isMarked is true. * #7 Win check is now O(1). Cell.reveal() returns the count of newly revealed cells (summing transitive flood-fill reveals); Minesweeper tracks #revealedCount and wins when it equals cols*rows - mines. cell.isMine via the existing getter replaces the previous boolean return as the lose signal. Tests updated to match the number return.
Covers the regressions the engine-cleanup commit could re-introduce and exercises code paths previously untested: * bounds validation — col / row / index out of range (high and low) throw with the expected message. Locks in fixes #1 and #2. * win flow — 3x3 with 8 mines, first-click center is the only non-mine, so a single reveal triggers win. Exercises the new #revealedCount tracking from #7 without needing Math.random mocking. * lose flow — 3x3 with 7 mines, Math.random pinned to 0 so the lone other non-mine ends up at idx 8 and idx 0 is a mine. After the safe first-click center reveal, clicking idx 0 loses; snapshot exposes 6 mines + 1 explosion. Covers #3's reveal(true) path indirectly. Test count: 30 → 39. Coverage on minesweeper.ts: 86% → 96% statements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sweeps six of the small engine issues filed back in #1–#7. Issue #5 (first-click safety) already landed in earlier work, so it's skipped.
Summary
Minesweeper.#checkCol/#checkRowusedcol > #colsinstead ofcol >= #cols. Valid indices are0..cols-1;col === colsslipped through the guard and crashed on field access. Same off-by-one in#checkRow. Fix:>→>=.#checkIndex/#checkCol/#checkRowrejected negative numbers.Number.isInteger(-1)istrueand-1 > colsisfalse, sofield[-1]slipped through to a crash. Added explicit< 0checks.#lose()calledcell.reveal()with no arg, so each unrevealed empty cell triggered a zero-neighbour flood-fill cascade — already-revealed cells short-circuited, so the visible result was correct, but it ran thousands of redundant recursive calls per loss. Passtrueto skip the per-cell cascade.Minesweeper.reveal()passedthis.#isGameOvertocell.reveal()after an early-return onthis.#isGameOver, so the arg was alwaysfalse. Dropped.Cell.mark()'s second branch hadif (!#isRevealed && !#isMarked), but the first branch already returns when#isMarkedistrue. Dropped the tautological!#isMarked.Cell.reveal()now returns the count of newly-revealed cells (summing the transitive flood-fill reveals);Minesweepertracks#revealedCountand wins when it equalscols*rows - mines.cell.isMinevia the existing getter replaces the previous boolean return as the lose signal. Two test assertions updated to match the new number return.Test plan
npm run buildclean (no type errors)npm run lintcleannpm test— 30/30 passing (test assertions forCell.reveal's return type updated)Closes #1, closes #2, closes #3, closes #4, closes #6, closes #7.