-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from teogor/feature/add-sudoku-board-encoding-…
…decoding Enable encoding and decoding Sudoku boards to/from strings
- Loading branch information
Showing
11 changed files
with
170 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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
22 changes: 22 additions & 0 deletions
22
sudoklify-common/src/main/kotlin/dev/teogor/sudoklify/common/InternalSudoklifyApi.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2024 Teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.sudoklify.common | ||
|
||
@RequiresOptIn(message = "This API is internal to Sudoklify library. Do NOT use it!") | ||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) | ||
annotation class InternalSudoklifyApi |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
sudoklify-ktx/src/main/kotlin/dev/teogor/sudoklify/ktx/BoardCellExtensions.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 Teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.sudoklify.ktx | ||
|
||
import dev.teogor.sudoklify.common.types.BoardCell | ||
|
||
fun Int.toBoardCell(): BoardCell { | ||
return when { | ||
this == 0 -> "-" | ||
|
||
else -> { | ||
var valueCopy = this | ||
buildString { | ||
while (valueCopy > 0) { | ||
val char = | ||
when (val digit = (valueCopy % 10)) { | ||
0 -> 'j' | ||
else -> ('a' + digit - 1) | ||
} | ||
append(char) | ||
valueCopy /= 10 | ||
} | ||
reverse() | ||
this[0] = this[0].uppercaseChar() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun BoardCell.toInt(): Int { | ||
return when { | ||
this == "-" -> 0 | ||
|
||
else -> | ||
map { char -> | ||
when { | ||
char.isUpperCase() -> { | ||
char - 'A' + 1 | ||
} | ||
|
||
else -> { | ||
char - 'a' + 1 | ||
} | ||
} | ||
}.fold(0) { acc, digit -> acc * 10 + digit } | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
sudoklify-ktx/src/main/kotlin/dev/teogor/sudoklify/ktx/SudokuBoardExtensions.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 Teogor (Teodor Grigor) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.teogor.sudoklify.ktx | ||
|
||
import dev.teogor.sudoklify.common.InternalSudoklifyApi | ||
import dev.teogor.sudoklify.common.types.SudokuType | ||
|
||
inline fun <T> List<List<T>>.mapToSudokuString(crossinline valueMapper: T.() -> Int): String { | ||
return flatMap { cells -> | ||
cells.map { cell -> | ||
valueMapper(cell).toBoardCell() | ||
} | ||
}.joinToString("") | ||
} | ||
|
||
@OptIn(InternalSudoklifyApi::class) | ||
inline fun <T> String.mapToSudokuBoard( | ||
sudokuType: SudokuType, | ||
crossinline valueMapper: Int.() -> T, | ||
): List<List<T>> { | ||
return getCells() | ||
.chunked(sudokuType.cells) | ||
.map { row -> row.map { valueMapper(it.toInt()) } } | ||
} | ||
|
||
@OptIn(InternalSudoklifyApi::class) | ||
inline fun <T> String.mapIndexedToSudokuBoard( | ||
sudokuType: SudokuType, | ||
crossinline valueMapper: (value: Int, row: Int, column: Int) -> T, | ||
): List<List<T>> { | ||
return getCells() | ||
.chunked(sudokuType.cells) | ||
.mapIndexed { row, rowElements -> | ||
rowElements.mapIndexed { column, value -> | ||
valueMapper(value.toInt(), row, column) | ||
} | ||
} | ||
} | ||
|
||
@InternalSudoklifyApi | ||
fun String.getCells(): ArrayList<String> { | ||
val regex = Regex("([A-I][a-z]+)|-|[A-I]") | ||
val matches = regex.findAll(this) | ||
val matchedTokens = ArrayList<String>() | ||
matches.forEach { matchedTokens.add(it.value) } | ||
return matchedTokens | ||
} |