From 66eebbaa38da0568de828eecd364617dced326a4 Mon Sep 17 00:00:00 2001 From: Joachim Date: Fri, 24 Aug 2018 21:14:54 +0200 Subject: [PATCH] cleaning stuff up --- src/main/kotlin/be/nielandt/Counter.kt | 56 +++++++++++++ src/main/kotlin/be/nielandt/CrossSolver.kt | 95 ++-------------------- src/main/kotlin/be/nielandt/Move.kt | 52 ++++++++++++ 3 files changed, 114 insertions(+), 89 deletions(-) create mode 100644 src/main/kotlin/be/nielandt/Counter.kt create mode 100644 src/main/kotlin/be/nielandt/Move.kt diff --git a/src/main/kotlin/be/nielandt/Counter.kt b/src/main/kotlin/be/nielandt/Counter.kt new file mode 100644 index 0000000..44c5c99 --- /dev/null +++ b/src/main/kotlin/be/nielandt/Counter.kt @@ -0,0 +1,56 @@ +package be.nielandt + +/** + * Counter for X digits of a given base. + */ +class Counter(size: Int, val base: Int = 10) { + + /** + * Empty counter, all 0 values for each digit. + */ + private var counter: Array = Array(size) { 0 } + + /** + * Increase the counter. + * + * @return true if the increase happened, false if we hit the ceiling. + */ + fun increase(): Boolean { + if (atMax()) { + return false + } + for (i in this.counter.size - 1 downTo 0) { + this.counter[i]++ + if (this.counter[i] == base) + this.counter[i] = 0 + else + break + } + return true + } + + /** + * How many digits does this counter contain? + */ + fun size(): Int { + return this.counter.size + } + + /** + * Get the digit at the given index. + */ + fun digit(index: Int): Int { + return counter[index] + } + + override fun toString(): String = this.counter.joinToString(".") + + /** + * Have we reached the maximum value? + */ + fun atMax(): Boolean { + return counter.all { + it == this.base - 1 + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/be/nielandt/CrossSolver.kt b/src/main/kotlin/be/nielandt/CrossSolver.kt index ee70dff..7065688 100644 --- a/src/main/kotlin/be/nielandt/CrossSolver.kt +++ b/src/main/kotlin/be/nielandt/CrossSolver.kt @@ -2,56 +2,6 @@ package be.nielandt import java.time.Duration import java.time.Instant -import java.util.* - -/** - * All the possible moves on the cube. - */ -enum class Move { - F, F_, F2, B, B_, B2, L, L_, L2, R, R_, R2, U, U_, U2, D, D_, D2; - - /** - * Static methods for the Move object. - */ - companion object { - /** - * Make a random set of moves that makes sense. - */ - fun random(amount: Int): List { - val rgen = Random() - val result = mutableListOf() - while (result.size < amount) { - val randomMove = Move.values()[rgen.nextInt(Move.values().size)] - // check if it makes sense? - if (result.isNotEmpty() && result.last() otherFace randomMove) - result.add(randomMove) - else if (result.isEmpty()) - result.add(randomMove) - } - return result - } - - /** - * Use the given counter, which contains digits that correspond with the amount of Moves, to generate a list of Moves. - */ - fun combo(counter: Counter): List { - val res = mutableListOf() - for (i in 0 until counter.size()) { - res.add(Move.values()[counter.digit(i)]) - } - return res - } - } - - /** - * Use this as `Move otherFace Move`, a quick way to check if the moves manipulate the same face or not. - * Example: `Move.F otherFace Move.U_ == false` - */ - infix fun otherFace(otherMove: Move): Boolean { - return this.toString().substring(0, 1) != otherMove.toString().substring(0, 1) - } - -} /** * Convert the color into a single digit for print purposes. @@ -111,6 +61,9 @@ fun doAllCrossMoveCounts(edgeModel: EdgeModel) { } } +/** + * For a single color, go 8 deep and try and find the minimal amount of moves to solve that cross. + */ fun crossMoveCount(edgeModel: EdgeModel, color: Color): List? { val moveCounts = Array?>(6) { null } @@ -134,6 +87,9 @@ fun crossMoveCount(edgeModel: EdgeModel, color: Color): List? { return null } +/** + * Solve the minimal cross for all colors. + */ fun allCrossMoveCount(edgeModel: EdgeModel): Map> { val start = Instant.now() val moveCounts = mutableMapOf>() @@ -169,42 +125,3 @@ fun allCrossMoveCount(edgeModel: EdgeModel): Map> { return moveCounts } -/** - * Counter for X digits of a given base. - */ -class Counter(size: Int, val base: Int = 10) { - - // start the counter at [0,0,...0] - private var counter: Array = Array(size) { 0 } - - fun increase(): Boolean { - if (atMax()) { - return false - } - for (i in this.counter.size - 1 downTo 0) { - this.counter[i]++ - if (this.counter[i] == base) - this.counter[i] = 0 - else - break - } - return true - } - - fun size(): Int { - return this.counter.size - } - - fun digit(index: Int): Int { - return counter[index] - } - - override fun toString(): String = this.counter.joinToString(".") - - fun atMax(): Boolean { - return counter.all { - it == this.base - 1 - } - } -} - diff --git a/src/main/kotlin/be/nielandt/Move.kt b/src/main/kotlin/be/nielandt/Move.kt new file mode 100644 index 0000000..40001a0 --- /dev/null +++ b/src/main/kotlin/be/nielandt/Move.kt @@ -0,0 +1,52 @@ +package be.nielandt + +import java.util.* + +/** + * All the possible moves on the cube. + */ +enum class Move { + F, F_, F2, B, B_, B2, L, L_, L2, R, R_, R2, U, U_, U2, D, D_, D2; + + /** + * Static methods for the Move object. + */ + companion object { + /** + * Make a random set of moves that makes sense. + */ + fun random(amount: Int): List { + val rgen = Random() + val result = mutableListOf() + while (result.size < amount) { + val randomMove = Move.values()[rgen.nextInt(Move.values().size)] + // check if it makes sense? + if (result.isNotEmpty() && result.last() otherFace randomMove) + result.add(randomMove) + else if (result.isEmpty()) + result.add(randomMove) + } + return result + } + + /** + * Use the given counter, which contains digits that correspond with the amount of Moves, to generate a list of Moves. + */ + fun combo(counter: Counter): List { + val res = mutableListOf() + for (i in 0 until counter.size()) { + res.add(Move.values()[counter.digit(i)]) + } + return res + } + } + + /** + * Use this as `Move otherFace Move`, a quick way to check if the moves manipulate the same face or not. + * Example: `Move.F otherFace Move.U_ == false` + */ + infix fun otherFace(otherMove: Move): Boolean { + return this.toString().substring(0, 1) != otherMove.toString().substring(0, 1) + } + +} \ No newline at end of file