Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved accuracy and reliability of BoardCell conversions with comprehensive testing #54

Merged
merged 3 commits into from
Feb 21, 2024

Conversation

teogor
Copy link
Owner

@teogor teogor commented Feb 21, 2024

This pull request addresses an issue with the BoardCell encoding and decoding functions where digits containing 0 were not handled correctly.

Specifically, the fix addresses:
- toInt(): Ensures that the 'J' character is correctly decoded as 0.
- toBoardCell(): Handles cases where the integer is composed of digits containing 0 (e.g., 10, 20, etc.).

Additionally, this PR introduces unit tests to verify the corrected behavior and ensure future changes maintain the expected functionality.

Using the fixed functions:

Once you've implemented the fixes in toBoardCell() and toInt(), you can use them directly in your code like this:

// Convert an integer to its BoardCell representation:
val cellValue = 20
val cellRepresentation = cellValue.toBoardCell() // Expected output: "Bj"

// Convert a BoardCell string back to an integer:
val cellString = "Aj"
val intValue = cellString.toInt() // Expected output: 10

// Check if an integer matches its BoardCell representation:
val number = 10
if (number == cellString.toInt()) {
  println("Number $number matches its BoardCell representation: $cellString")
}

@teogor teogor added this to the 1.0.0-beta01 milestone Feb 21, 2024
@teogor teogor self-assigned this Feb 21, 2024
@teogor
Copy link
Owner Author

teogor commented Feb 21, 2024

Algorithm J-Encoding For Sudoku Cells

Encoding:

  1. Split: Convert the integer representing the cell value into a string.
  2. Map: Iterate through each character (digit) in the string:
    • For digits 1-9, assign the corresponding letter 'a' to 'i' (a = 1, b = 2, etc.).
    • For digit 0, assign the letter 'j'.
  3. Format: Uppercase the first character in the resulting string.

Decoding:

  1. Map: Process each character (letter) in the BoardCell string:
    • For letters 'a' to 'i', convert them to their corresponding digits (a = 1, b = 2, etc.).
    • For letter 'j', convert it to 0.
  2. Combine: Join the converted digits into a single integer, representing the decoded cell value.

Example:

Encoding:

  • Input: Integer 230
  • Steps:
    1. Convert to string: '2', '3', '0'
    2. Map digits: 'b', 'c', 'j'
    3. Uppercase first: 'B', 'c', 'j'
  • Output: String "Bcj"

Decoding:

  • Input: String "Bcj"
  • Steps:
    1. Split into characters: ## Algorithm Explanation: BoardCell Encoding and Decoding

Encoding:

  1. Split: Convert the integer representing the cell value into a string.
  2. Map: Iterate through each character (digit) in the string:
    • For digits 1-9, assign the corresponding letter 'a' to 'i' (a = 1, b = 2, etc.).
    • For digit 0, assign the letter 'j'.
  3. Format: Uppercase the first character in the resulting string.

Decoding:

  1. Map: Process each character (letter) in the BoardCell string:
    • For letters 'a' to 'i', convert them to their corresponding digits (a = 1, b = 2, etc.).
    • For letter 'j', convert it to 0.
  2. Combine: Join the converted digits into a single integer, representing the decoded cell value.

Example:

Encoding:

  • Input: Integer 230
  • Steps:
    1. Convert to string: '2', '3', '0'
    2. Map digits: 'b', 'c', 'j'
    3. Uppercase first: 'B', 'c', 'j'
  • Output: String "Bcj"

Decoding:

  • Input: String "Bcj"
  • Steps:
    1. Split into characters: 'B', 'c', 'j'
    2. Map letters: 2, 3, 0
    3. Join digits: 230
  • Output: Integer 230

View the J-Encoding algorithm documentation: source.teogor.dev/sudoklify/j-encoding-for-sudoku-cells

@teogor teogor force-pushed the bugfix/boardcell-encoding-decoding-tests branch from 4bc455e to 216a57e Compare February 21, 2024 03:05
@teogor teogor merged commit 36cd422 into main Feb 21, 2024
4 checks passed
@zeobot zeobot bot deleted the bugfix/boardcell-encoding-decoding-tests branch February 21, 2024 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

1 participant