-
-
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 #62 from teogor/enhancement/generate-grid-with-givens
Enhance SudokuPuzzle with Grid Generation from Given Cells
- Loading branch information
Showing
5 changed files
with
90 additions
and
25 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
37 changes: 37 additions & 0 deletions
37
sudoklify-ktx/src/main/kotlin/dev/teogor/sudoklify/ktx/SudokuPuzzleExtensions.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,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() | ||
} |
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
42 changes: 42 additions & 0 deletions
42
sudoklify-seeds/src/test/kotlin/dev/teogor/sudoklify/seeds/utils/SudokuPuzzleUtils.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,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 | ||
} |