Open
Conversation
This commit adds functionality to determine if a puzzle configuration is solvable and ensures that only solvable puzzles are generated. Changes: - Added isSolvableFromNumbers() and isSolvable() functions to utils.ts that implement the mathematical solvability check for sliding puzzles - Updated TileDescriptor type to include number, width, and height properties - Modified levels-factory.ts to only generate solvable puzzle configurations - Updated Game component to track solvability state and pass it to Menu - Added visual solvability indicator in Menu component showing whether the current puzzle can be solved - The indicator displays with a green checkmark for solvable puzzles and red cancel icon for unsolvable ones The solvability algorithm checks: - Count inversions in the tile arrangement - Determine the blank tile's row position from bottom - Apply the rule: (even row from bottom + odd inversions) OR (odd row from bottom + even inversions) = solvable
Since the level generator now ensures all puzzles are solvable, there's no need to display the solvability status to users. The indicator would always show "Yes" which doesn't provide useful information. Changes: - Removed solvable chip from Menu component - Removed unused CheckCircle and Cancel icon imports - Removed solvable prop from Menu component type - Removed solvable state from Game component - Removed isSolvable import from Game component
Added thorough test coverage for both isSolvableFromNumbers() and isSolvable() functions: - Tests for solved state detection - Tests for solvable configurations with various combinations of: - Odd/even inversions - Odd/even blank row positions from bottom - Tests for unsolvable configurations - Tests for blank tile in different positions - Tests for correct inversion counting excluding the blank tile - Tests using both number arrays and tile descriptor objects All test cases include detailed comments explaining: - The blank tile position and row calculation - The number of inversions and parity - The solvability rule being tested
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.
This commit adds functionality to determine if a puzzle configuration is solvable and ensures that only solvable puzzles are generated.
Changes:
The solvability algorithm checks: