Skip to content

Commit

Permalink
Merge pull request #61 from ILikeYourHat/simplify-sudoku-type-definit…
Browse files Browse the repository at this point in the history
…ions

Simplify sudoku types definition
  • Loading branch information
ILikeYourHat authored Dec 21, 2024
2 parents 5aee86b + 9f3a04c commit e020903
Show file tree
Hide file tree
Showing 28 changed files with 37 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FilledSudokuGenerator(
}

private fun setRandomValidValue(sudoku: Sudoku, field: Field) {
sudoku.type.allPossibleValues()
(1..sudoku.type.maxValue)
.shuffled(random)
.asSequence()
.onEach { field.set(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SudokuShuffler(
}

private fun Sudoku.swapNumbers() {
val previousValues = type.allPossibleValues()
val previousValues = 1..type.maxValue
val newValues = previousValues.shuffled(random)

allFields
Expand Down
22 changes: 17 additions & 5 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/model/Board.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package io.github.ilikeyourhat.kudoku.model

import io.github.ilikeyourhat.kudoku.model.matrix.ListMatrix
import io.github.ilikeyourhat.kudoku.model.matrix.Matrix
import io.github.ilikeyourhat.kudoku.model.matrix.MutableMatrix
import java.util.NoSuchElementException

data class Board(
private val fields: Matrix<Field?>
private val fields: MutableMatrix<Field?>
) {

constructor(sizeX: Int, sizeY: Int) : this(
ListMatrix<Field?>(
sizeX = sizeX,
sizeY = sizeY,
defaultValue = null
)
)

constructor(sizeX: Int, sizeY: Int, valueInitializer: (x: Int, y: Int) -> Field?) : this(
ListMatrix<Field?>(
sizeX = sizeX,
Expand Down Expand Up @@ -60,19 +68,23 @@ data class Board(
return Board(
sizeX = endX - startX,
sizeY = endY - startY,
valueInitializer = { x, y -> getOrNull(startX + x, startY + y) }
valueInitializer = { x, y -> initField(startX + x, startY + y) }
)
}

fun region(startX: Int, startY: Int, endX: Int, endY: Int): Region {
val fields = (startX..endX).flatMap { indexX ->
(startY..endY).mapNotNull { indexY ->
getOrNull(indexX, indexY)
(startY..endY).map { indexY ->
initField(indexX, indexY)
}
}
return Region(fields)
}

private fun initField(x: Int, y: Int): Field {
return fields[x, y] ?: Field(x, y).also { fields[x, y] = it }
}

override fun toString(): String {
val sb = StringBuilder()
for (y in 0 until fields.sizeY) {
Expand Down

This file was deleted.

29 changes: 14 additions & 15 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/model/Sudoku.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ data class Sudoku(
val board: Board
) {

val regions = type.divider().divide(board)
val regions: List<Region> = type.divider().divide(board)

constructor(type: SudokuType) : this(
type = type,
board = BoardCreator.createBoard(type)
board = Board(type.sizeX, type.sizeY),
)

constructor(type: SudokuType, values: List<Int>) : this(
type = type,
board = BoardCreator.createBoard(type)
.also {
val fields = it.fields()
require(fields.size == values.size) {
"Incorrect values count, expected ${fields.size}, but was ${values.size}"
}
values.forEachIndexed { index, value ->
require(value == 0 || type.allPossibleValues().contains(value)) {
"Value $value is not supported by type ${type.name}"
}
fields[index].set(value)
}
board = Board(type.sizeX, type.sizeY)
) {
val fields = board.fields()
require(fields.size == values.size) {
"Incorrect values count, expected ${fields.size}, but was ${values.size}"
}
values.forEachIndexed { index, value ->
require((0..type.maxValue).contains(value)) {
"Value $value is not supported by type ${type.name}"
}
)
fields[index].set(value)
}
}

override fun toString(): String {
return "${type.name} $board"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ interface SudokuType {
val sizeX: Int
val sizeY: Int
val maxValue: Int
fun template(): String
fun divider(): RegionDivider
fun allPossibleValues(): List<Int> = IntRange(1, maxValue).toList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RegionDivider {

fun allFields() = apply {
dividers.add { board ->
listOf(Region(board.fields()))
listOf(board.region(0, 0, board.sizeX() - 1, board.sizeY() - 1))
}
}

Expand Down Expand Up @@ -78,7 +78,8 @@ class RegionDivider {
}

fun divide(board: Board): List<Region> {
return dividers.flatMap { it.divide(board) }
return dividers
.flatMap { it.divide(board) }
.distinct()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data class SudokuHintGrid(
companion object {

fun create(sudoku: Sudoku): SudokuHintGrid {
val possibleValues = sudoku.type.allPossibleValues()
val possibleValues = 1..sudoku.type.maxValue

val hintMap = sudoku.allFields
.associate { it.position to possibleValues.toMutableSet() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,4 @@ object Butterfly12x12 : SudokuType {
.applySubSudoku(0, 3, Classic9x9)
.applySubSudoku(3, 3, Classic9x9)
}

override fun template() = """
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
""".trimIndent()
}
18 changes: 0 additions & 18 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/type/Classic12x12.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ object Classic12x12 : SudokuType {
override val sizeY = 12
override val maxValue = 12

override fun template() = """
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@ object Classic12x12Vertical : SudokuType {
override val sizeY = 12
override val maxValue = 12

override fun template() = """
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_ _,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
22 changes: 0 additions & 22 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/type/Classic16x16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,6 @@ object Classic16x16 : SudokuType {
override val sizeY = 16
override val maxValue = 16

override fun template() = """
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
_,_,_,_ _,_,_,_ _,_,_,_ _,_,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
32 changes: 0 additions & 32 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/type/Classic25x25.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,6 @@ object Classic25x25 : SudokuType {
override val sizeY = 25
override val maxValue = 25

override fun template() = """
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
_,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_ _,_,_,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ object Classic4x4 : SudokuType {
override val sizeY = 4
override val maxValue = 4

override fun template() = """
_,_ _,_
_,_ _,_
_,_ _,_
_,_ _,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
11 changes: 0 additions & 11 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/type/Classic6x6.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ object Classic6x6 : SudokuType {
override val sizeY = 6
override val maxValue = 6

override fun template() = """
_,_,_ _,_,_
_,_,_ _,_,_
_,_,_ _,_,_
_,_,_ _,_,_
_,_,_ _,_,_
_,_,_ _,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ object Classic6x6Vertical : SudokuType {
override val sizeY = 6
override val maxValue = 6

override fun template() = """
_,_ _,_ _,_
_,_ _,_ _,_
_,_ _,_ _,_
_,_ _,_ _,_
_,_ _,_ _,_
_,_ _,_ _,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
14 changes: 0 additions & 14 deletions src/main/kotlin/io/github/ilikeyourhat/kudoku/type/Classic9x9.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ object Classic9x9 : SudokuType {
override val sizeY = 9
override val maxValue = 9

override fun template() = """
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
_,_,_ _,_,_ _,_,_
""".trimIndent()

override fun divider(): RegionDivider {
return RegionDivider()
.divideByRows()
Expand Down
Loading

0 comments on commit e020903

Please sign in to comment.