Skip to content

Refactor win detection to use a configurable winningValue - #15

Open
mmiermans wants to merge 1 commit into
exp/mozilla-basefrom
exp/mz-js-logic
Open

Refactor win detection to use a configurable winningValue#15
mmiermans wants to merge 1 commit into
exp/mozilla-basefrom
exp/mz-js-logic

Conversation

@mmiermans

Copy link
Copy Markdown
Owner

Replaces the hardcoded 2048 magic number in win detection with a single winningValue field on the GameManager, and rewrites hasWon() to reuse getMaxTile() instead of the previous loop that always returned false. Makes it trivial to support alternate win thresholds (e.g. 1024 / 4096) later.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
🤖 Claude has reviewed this PR — expand after forming your own opinion

Code Quality

Bug: off-by-one in move() win detection

move() uses > but hasWon() uses >=, making them inconsistent. With winningValue = 2048, the in-game check now requires a 4096 tile to trigger a win — the original code used === 2048.

The fix on line 356 should be:

      if (merged.value >= self.winningValue) self.won = true;

This also means hasWon() (called during setup() to restore state from a saved game) would correctly detect a 2048 win, but playing the game live would not — a subtle and hard-to-catch divergence.

Good fix on hasWon()

The old hasWon() was silently broken: the return true inside eachCell's callback returned from the callback function, not from hasWon() itself, so it always returned false. Rewriting it to delegate to getMaxTile() fixes that bug and is a nice DRY improvement.

Testing

No tests exist for this logic. Given that this PR fixes one latent bug (hasWon always returning false) and introduces one new bug (> vs >=), even a minimal test for the win-detection threshold would add confidence.

Security / Performance / Documentation

No concerns. The comment on winningValue is fine for a magic-number extraction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant