From ef0ecb9d3675aed583426375f2042d6ade8e8d3d Mon Sep 17 00:00:00 2001 From: Joachim Nielandt Date: Fri, 24 Aug 2018 10:26:40 +0200 Subject: [PATCH] does all the colors, lol --- src/main/kotlin/be/nielandt/CrossSolver.kt | 61 +++++++++++++++----- src/main/kotlin/be/nielandt/EdgeModel.kt | 33 ++++++++++- src/test/kotlin/be/nielandt/EdgeModelTest.kt | 32 ++++++++++ 3 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 src/test/kotlin/be/nielandt/EdgeModelTest.kt diff --git a/src/main/kotlin/be/nielandt/CrossSolver.kt b/src/main/kotlin/be/nielandt/CrossSolver.kt index ba6e868..59ede01 100644 --- a/src/main/kotlin/be/nielandt/CrossSolver.kt +++ b/src/main/kotlin/be/nielandt/CrossSolver.kt @@ -9,11 +9,19 @@ enum class Move { F, F_, F2, B, B_, B2, L, L_, L2, R, R_, R2, U, U_, U2, D, D_, D2; companion object { + /** + * Make a random set of moves that makes sense. + */ fun random(amount: Int): List { val rgen = Random() val result = mutableListOf() - for (i in 0 until amount) { - result.add(Move.values()[rgen.nextInt(Move.values().size)]) + 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 } @@ -26,6 +34,11 @@ enum class Move { return res } } + + infix fun otherFace(otherMove: Move): Boolean { + return this.toString().substring(0, 1) != otherMove.toString().substring(0, 1) + } + } /** @@ -44,9 +57,6 @@ fun l(colorIndex: Short): String { } fun main(args: Array) { - val edgeModel = EdgeModel() -// println(edgeModel) -// println("edgeModel.whiteCrossSolved() = ${edgeModel.whiteCrossSolved()}") // val u2 = Move.U2 // val doMove = edgeModel.doMove(Move.U2) @@ -78,16 +88,38 @@ fun main(args: Array) { // make a beginning model, then start doing crazy shit - val moves = Move.random(15) + val moves = Move.random(20) println("Scramble: $moves") val scrambledModel = EdgeModel(moves) println(scrambledModel) - val whiteCrossMoveCount = whiteCrossMoveCount(scrambledModel) - println("whiteCrossMoveCount: $whiteCrossMoveCount") + doAllCrossMoveCounts(scrambledModel) } -fun whiteCrossMoveCount(edgeModel: EdgeModel): Int { +fun colorName(color: Short): String { + return when (color) { + WHITE -> "white" + YELLOW -> "yellow" + RED -> "red" + BLUE -> "blue" + GREEN -> "green" + ORANGE -> "orange" + else -> { + "?" + } + } +} + +fun doAllCrossMoveCounts(edgeModel: EdgeModel) { + for(i in 0 until 6) { + val crossMoveCount = crossMoveCount(edgeModel, i.toShort()) + println("${colorName(i.toShort())} in ${crossMoveCount?.size}: ${crossMoveCount?.joinToString()}") + } +} + +fun crossMoveCount(edgeModel: EdgeModel, color: Short): List? { + val moveCounts = Array?>(6) { null } + for (moveCount in 1..8) { // build a counter of moveCount big val counter = Counter(moveCount, Move.values().size) @@ -98,15 +130,14 @@ fun whiteCrossMoveCount(edgeModel: EdgeModel): Int { val moves = Move.combo(counter) // execute the moves val afterMoves = edgeModel.doMoves(moves) - // check whitecross! - if(afterMoves.whiteCrossSolved()) { - println("White cross found!") - println("moves = $moves") - return@whiteCrossMoveCount moveCount + val crossSolved = afterMoves.crossSolved(color) + if (crossSolved) { + return@crossMoveCount moves } + } while (counter.increase()) } - return Int.MAX_VALUE + return null } /** diff --git a/src/main/kotlin/be/nielandt/EdgeModel.kt b/src/main/kotlin/be/nielandt/EdgeModel.kt index 5e7fce6..dfbb88c 100644 --- a/src/main/kotlin/be/nielandt/EdgeModel.kt +++ b/src/main/kotlin/be/nielandt/EdgeModel.kt @@ -180,9 +180,36 @@ class EdgeModel { return this.doMoves(f.toList()) } - fun whiteCrossSolved(): Boolean { - return model[WB] == WHITE && model[WG] == WHITE && model[WO] == WHITE && model[WR] == WHITE && - model[GW] == GREEN && model[OW] == ORANGE && model[RW] == RED && model[BW] == BLUE + fun crossSolved(color: Short): Boolean { + return when(color) { + WHITE -> { + model[WB] == WHITE && model[WG] == WHITE && model[WO] == WHITE && model[WR] == WHITE && + model[BW] == BLUE && model[GW] == GREEN && model[OW] == ORANGE && model[RW] == RED + } + YELLOW -> { + model[YB] == YELLOW && model[YG] == YELLOW && model[YO] == YELLOW && model[YR] == YELLOW && + model[BY] == BLUE && model[GY] == GREEN && model[OY] == ORANGE && model[RY] == RED + } + RED -> { + model[RW] == RED && model[RG] == RED && model[RY] == RED && model[RB] == RED && + model[WR] == WHITE && model[GR] == GREEN && model[YR] == YELLOW && model[BR] == BLUE + } + BLUE -> { + model[BW] == BLUE && model[BR] == BLUE && model[BY] == BLUE && model[BO] == BLUE && + model[WB] == WHITE && model[RB] == RED && model[YB] == YELLOW && model[OB] == ORANGE + } + GREEN -> { + model[GW] == GREEN && model[GO] == GREEN && model[GY] == GREEN && model[GR] == GREEN && + model[WG] == WHITE && model[OG] == ORANGE && model[YG] == YELLOW && model[RG] == RED + } + ORANGE -> { + model[OW] == ORANGE && model[OB] == ORANGE && model[OY] == ORANGE && model[OG] == ORANGE && + model[WO] == WHITE && model[BO] == BLUE && model[YO] == YELLOW && model[GO] == GREEN + } + else -> { + throw IllegalStateException("6 color for lyfe!") + } + } } } diff --git a/src/test/kotlin/be/nielandt/EdgeModelTest.kt b/src/test/kotlin/be/nielandt/EdgeModelTest.kt new file mode 100644 index 0000000..d0d525f --- /dev/null +++ b/src/test/kotlin/be/nielandt/EdgeModelTest.kt @@ -0,0 +1,32 @@ +package be.nielandt + +import org.junit.Test + +import org.junit.Assert.* + +class EdgeModelTest { + + @Test + fun singleMoveAlwaysOneSolvedCross() { + // try each move + Move.values().forEach { move -> + val doMove = EdgeModel().doMove(move) + val count = (0 until 6).map { color -> + val crossSolved = doMove.crossSolved(color.toShort()) + crossSolved + }.count { it } + assertEquals(1, count) + } + } + + @Test + fun findAtLeastOneCombo() { + val white = WHITE + val doMoves = EdgeModel().doMoves(Move.random(20)) + val crossMoveCount = crossMoveCount(doMoves, WHITE) + assertNotNull(crossMoveCount) + assertTrue(crossMoveCount!!.size<9) + println(doMoves) + println("crossMoveCount = $crossMoveCount") + } +} \ No newline at end of file