Skip to content

Commit

Permalink
Merge pull request #62 from teogor/enhancement/generate-grid-with-givens
Browse files Browse the repository at this point in the history
Enhance SudokuPuzzle with Grid Generation from Given Cells
  • Loading branch information
teogor authored Mar 2, 2024
2 parents b8b808e + baf1fbe commit 15f8137
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 25 deletions.
4 changes: 4 additions & 0 deletions sudoklify-ktx/api/sudoklify-ktx.api
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public final class dev/teogor/sudoklify/ktx/SudokuBoardExtensionsKt {
public static final fun mapToSudokuString (Ljava/util/List;Lkotlin/jvm/functions/Function1;)Ljava/lang/String;
}

public final class dev/teogor/sudoklify/ktx/SudokuPuzzleExtensionsKt {
public static final fun generateGridWithGivens (Ldev/teogor/sudoklify/common/model/SudokuPuzzle;)Ljava/util/List;
}

public final class dev/teogor/sudoklify/ktx/SudokuTypeExtensionsKt {
public static final fun areCellsInSameBox (Ldev/teogor/sudoklify/common/types/SudokuType;II)Z
public static final fun areCellsInSameBox (Ldev/teogor/sudoklify/common/types/SudokuType;IIII)Z
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.model.SudokuPuzzle

/**
* Generates a grid based on the Sudoku puzzle with the given cells filled in.
*
* @return A list of lists representing the generated grid, where each inner list
* represents a row and each element in the inner list represents a cell value.
*/
fun SudokuPuzzle.generateGridWithGivens(): List<List<Int>> {
val gridSize = sudokuType.uniqueDigitsCount
val grid = MutableList(gridSize) { MutableList(gridSize) { 0 } }

// Map givens to the grid
for (given in givens) {
grid[given.row][given.col] = given.value
}

return grid.toList()
}
1 change: 1 addition & 0 deletions sudoklify-seeds/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ winds {

dependencies {
implementation(project(":sudoklify-core"))
implementation(project(":sudoklify-ktx"))
testImplementation(libs.junit.jupiter)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
package dev.teogor.sudoklify.seeds

import dev.teogor.sudoklify.common.model.SudokuBlueprint
import dev.teogor.sudoklify.common.types.Board
import dev.teogor.sudoklify.core.generation.createPuzzle
import dev.teogor.sudoklify.core.generation.difficulty
import dev.teogor.sudoklify.core.generation.generateSudoku
import dev.teogor.sudoklify.core.generation.seed
import dev.teogor.sudoklify.core.generation.seeds
import dev.teogor.sudoklify.core.generation.sudokuParamsBuilder
import dev.teogor.sudoklify.core.generation.sudokuType
import dev.teogor.sudoklify.core.util.toBoard
import dev.teogor.sudoklify.core.util.toSequenceString
import dev.teogor.sudoklify.ktx.createSeed
import dev.teogor.sudoklify.seeds.utils.comparePuzzles
import dev.teogor.sudoklify.seeds.utils.toSequenceString
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ArgumentsSource


class SudokuSeedsTest {
@Test
fun combinedSeeds_generatesSolutionMatchingPuzzle() {
Expand Down Expand Up @@ -62,7 +61,7 @@ class SudokuSeedsTest {
sudokuType { blueprint.sudokuType }
difficulty { blueprint.difficulty }
}
val sudoku = sudokuParams.generateSudoku()
val sudoku = sudokuParams.createPuzzle()

assertEquals(
blueprint.sudokuType,
Expand All @@ -74,9 +73,10 @@ class SudokuSeedsTest {
sudoku.difficulty,
"Difficulty level mismatch",
)

assertEquals(
blueprint.puzzle,
sudoku.puzzle.toSequenceString(),
toSequenceString(sudoku),
"Puzzle generation error",
)
assertEquals(
Expand All @@ -85,23 +85,4 @@ class SudokuSeedsTest {
"Solution generation error",
)
}

private fun comparePuzzles(puzzle: Board, solution: Board): Boolean {
if (puzzle.size != solution.size || puzzle[0].size != solution[0].size) {
return false
}

for (row in puzzle.indices) {
for (col in 0..<puzzle[row].size) {
val puzzleValue = puzzle[row][col]
val solvedValue = solution[row][col]

if (puzzleValue != "-" && puzzleValue != solvedValue) {
return false
}
}
}

return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.teogor.sudoklify.seeds.utils

import dev.teogor.sudoklify.common.model.SudokuPuzzle
import dev.teogor.sudoklify.ktx.generateGridWithGivens

fun List<List<Int>>.toSequenceString(): String {
return flatten().joinToString("")
}

fun toSequenceString(sudokuPuzzle: SudokuPuzzle): String {
return sudokuPuzzle.generateGridWithGivens().map {
it.map {
if (it == 0) {
"-"
} else {
it.toString()
}
}
}.flatten().joinToString(separator = "")
}

fun comparePuzzles(
puzzle: Array<Array<String>>,
solution: Array<Array<String>>,
): Boolean {
if (puzzle.size != solution.size || puzzle[0].size != solution[0].size) {
return false
}

for (row in puzzle.indices) {
for (col in 0..<puzzle[row].size) {
val puzzleValue = puzzle[row][col]
val solvedValue = solution[row][col]

if (puzzleValue != "-" && puzzleValue != solvedValue) {
return false
}
}
}

return true
}

0 comments on commit 15f8137

Please sign in to comment.