From 5a4d8efd09b04396f6f6faa21d12d585d3b9c587 Mon Sep 17 00:00:00 2001 From: meikpiep Date: Wed, 2 Aug 2023 18:32:37 +0200 Subject: [PATCH] Extracts methods --- .../holokenmod/creation/dlx/MathDokuDLX.kt | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/holoken-core/src/main/kotlin/com/holokenmod/creation/dlx/MathDokuDLX.kt b/holoken-core/src/main/kotlin/com/holokenmod/creation/dlx/MathDokuDLX.kt index 9377ba66..0d7c3743 100644 --- a/holoken-core/src/main/kotlin/com/holokenmod/creation/dlx/MathDokuDLX.kt +++ b/holoken-core/src/main/kotlin/com/holokenmod/creation/dlx/MathDokuDLX.kt @@ -20,11 +20,11 @@ class MathDokuDLX( ) } - var dlx: DLX? = null + private var dlx: DLX? = null + private var constraints = emptyList() private var numberOfCages = 0 - private fun initializeDLX() { // Number of columns = number of constraints = // BOARD * BOARD (for columns) + @@ -58,13 +58,17 @@ class MathDokuDLX( numberOfCages = creators.size + extraRectingularCages - val constraints = constraintsFromCages(creators) + constraintsFromRectangular(creators) + constraints = constraintsFromCages(creators) + constraintsFromRectangular(creators) dlx = DLX( possibleDigits.size * (grid.gridSize.width + grid.gridSize.height) + numberOfCages, numberOfNodes ) + logConstraints(constraints) + } + + private fun logConstraints(constraints: List) { if (logger.isInfoEnabled) { val headerCellId = StringBuilder() val headerValue = StringBuilder() @@ -89,24 +93,25 @@ class MathDokuDLX( logger.info { headerValue.toString() } logger.info { headerCellId.toString() } - } - for ((currentCombination, constraint) in constraints.withIndex()) { - if (logger.isInfoEnabled) { + for (constraint in constraints) { val constraintInfo = StringBuilder() - constraint.forEach { constraintInfo.append(if (it) { "*" } else { "-" }.padEnd(4)) } + constraint.forEach { + constraintInfo.append( + if (it) { + "*" + } else { + "-" + }.padEnd(4) + ) + } logger.info { constraintInfo.toString() } } - - for (constraintIndex in constraint.indices) { - if (constraint[constraintIndex]) { - dlx!!.addNode(constraintIndex, currentCombination) - } - } } } + private fun cartesianProduct(values: List, numberOfCopies: Int): Set> { return UniqueIndexSetsOfGivenLength(values, numberOfCopies).calculateProduct() } @@ -219,6 +224,14 @@ class MathDokuDLX( fun solve(type: DLX.SolveType): Int { initializeDLX() + for ((currentCombination, constraint) in constraints.withIndex()) { + for (constraintIndex in constraint.indices) { + if (constraint[constraintIndex]) { + dlx!!.addNode(constraintIndex, currentCombination) + } + } + } + return dlx!!.solve(type) } }