Skip to content

Commit

Permalink
Merge pull request #63 from teogor/enhancement/sudoku-symbol-conversion
Browse files Browse the repository at this point in the history
Enable Consistent Sudoku Symbol Representation with `convertToSudokuSymbol`
  • Loading branch information
teogor authored Mar 2, 2024
2 parents 15f8137 + d7ad823 commit 8b85daf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dev.teogor.sudoklify.demo.gen.impl

import dev.teogor.sudoklify.common.types.Board
import dev.teogor.sudoklify.common.types.Difficulty
import dev.teogor.sudoklify.common.types.SudokuType
import dev.teogor.sudoklify.core.io.toSudokuIntArray
Expand Down Expand Up @@ -97,8 +96,8 @@ fun main() {
}

private fun comparePuzzles(
puzzle: Board,
solution: Board,
puzzle: Array<Array<String>>,
solution: Array<Array<String>>,
): Boolean {
if (puzzle.size != solution.size || puzzle[0].size != solution[0].size) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package dev.teogor.sudoklify.core.io

import dev.teogor.sudoklify.common.InternalSudoklifyApi
import dev.teogor.sudoklify.common.types.SudokuString
import dev.teogor.sudoklify.common.types.SudokuType
import dev.teogor.sudoklify.core.util.toBoard

Expand Down Expand Up @@ -74,7 +73,7 @@ class SudokuParser(
*
* @throws [IllegalArgumentException] if the provided Sudoku puzzle string is invalid.
*/
fun SudokuString.toSudokuIntArray(sudokuType: SudokuType): Array<IntArray> {
fun String.toSudokuIntArray(sudokuType: SudokuType): Array<IntArray> {
val parser = SudokuParser(this, sudokuType)
return parser.toIntArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dev.teogor.sudoklify.core.tokenizer

import dev.teogor.sudoklify.common.types.Board
import dev.teogor.sudoklify.common.types.Layout
import dev.teogor.sudoklify.common.types.TokenMap

Expand Down Expand Up @@ -51,7 +50,7 @@ sealed class Tokenizer {
layout: Layout,
sequence: String,
tokenMap: TokenMap,
): Board
): Array<Array<String>>

companion object {
/**
Expand Down Expand Up @@ -87,7 +86,7 @@ sealed class Tokenizer {
layout: Layout,
sequence: String,
tokenMap: TokenMap,
): Board {
): Array<Array<String>> {
with(replaceTokens(sequence, tokenMap)) {
return layout.map { row ->
row.map { cell ->
Expand Down Expand Up @@ -119,7 +118,7 @@ sealed class Tokenizer {
layout: Layout,
sequence: String,
tokenMap: TokenMap,
): Board {
): Array<Array<String>> {
val tokens = extractTokens(sequence, tokenMap)
return layout.map { row ->
row.map { cell ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

package dev.teogor.sudoklify.core.util

import dev.teogor.sudoklify.common.types.Board
import dev.teogor.sudoklify.common.types.SudokuString
import dev.teogor.sudoklify.common.types.SudokuType

/**
* Converts a `Board` object to a Sudoku string representation.
*
* @return A `SudokuString` representing the board.
*/
fun Board.toSequenceString(): SudokuString =
fun Array<Array<String>>.toSequenceString(): String =
joinToString("") {
it.joinToString("")
}
Expand All @@ -40,7 +38,7 @@ fun Board.toSequenceString(): SudokuString =
* @return A `Board` representing the Sudoku puzzle.
*/
@Deprecated("Use the `toBoard(sudokuType: SudokuType)` function instead.")
fun SudokuString.toBoard(boxDigits: Int): Board {
fun String.toBoard(boxDigits: Int): Array<Array<String>> {
return chunked(boxDigits)
.map { chunk -> chunk.map { it.toString() }.toTypedArray() }
.toTypedArray()
Expand All @@ -55,7 +53,7 @@ fun SudokuString.toBoard(boxDigits: Int): Board {
*
* @return A list of lists of strings representing the Sudoku puzzle.
*/
fun SudokuString.toBoard(sudokuType: SudokuType): Board {
fun String.toBoard(sudokuType: SudokuType): Array<Array<String>> {
val regex = Regex("([A-I][a-z]+)|-|[A-I]")
val matches = regex.findAll(this)
val matchedTokens = ArrayList<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("DEPRECATION")

package dev.teogor.sudoklify.core.util

import dev.teogor.sudoklify.common.types.GameType
Expand Down
1 change: 1 addition & 0 deletions sudoklify-ktx/api/sudoklify-ktx.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public final class dev/teogor/sudoklify/ktx/BoardCellExtensionsKt {
public static final fun convertToSudokuSymbol (I)Ljava/lang/String;
public static final fun toInt (Ljava/lang/String;)I
public static final fun toJEncodedCell (I)Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ fun JEncodedCell.toInt(): Int {
}.fold(0) { acc, digit -> acc * 10 + digit }
}
}

/**
* Converts the integer to a Sudoku symbol or digit.
*
* @return The string representation of the integer in base 10, converted to uppercase.
*/
fun Int.convertToSudokuSymbol(): String {
return toString(10).uppercase()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("DEPRECATION")

package dev.teogor.sudoklify.ktx

import dev.teogor.sudoklify.common.types.PuzzleString
Expand Down

0 comments on commit 8b85daf

Please sign in to comment.