Skip to content

Add solvability check for 15-puzzle#107

Open
ovidiubute wants to merge 3 commits intomasterfrom
claude/add-solvability-check-Ucv9g
Open

Add solvability check for 15-puzzle#107
ovidiubute wants to merge 3 commits intomasterfrom
claude/add-solvability-check-Ucv9g

Conversation

@ovidiubute
Copy link
Member

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

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
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.

2 participants