does all the colors, lol

This commit is contained in:
Joachim Nielandt 2018-08-24 10:26:40 +02:00
parent 320619c9b5
commit ef0ecb9d36
3 changed files with 108 additions and 18 deletions

View File

@ -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; F, F_, F2, B, B_, B2, L, L_, L2, R, R_, R2, U, U_, U2, D, D_, D2;
companion object { companion object {
/**
* Make a random set of moves that makes sense.
*/
fun random(amount: Int): List<Move> { fun random(amount: Int): List<Move> {
val rgen = Random() val rgen = Random()
val result = mutableListOf<Move>() val result = mutableListOf<Move>()
for (i in 0 until amount) { while (result.size < amount) {
result.add(Move.values()[rgen.nextInt(Move.values().size)]) 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 return result
} }
@ -26,6 +34,11 @@ enum class Move {
return res 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<String>) { fun main(args: Array<String>) {
val edgeModel = EdgeModel()
// println(edgeModel)
// println("edgeModel.whiteCrossSolved() = ${edgeModel.whiteCrossSolved()}")
// val u2 = Move.U2 // val u2 = Move.U2
// val doMove = edgeModel.doMove(Move.U2) // val doMove = edgeModel.doMove(Move.U2)
@ -78,16 +88,38 @@ fun main(args: Array<String>) {
// make a beginning model, then start doing crazy shit // make a beginning model, then start doing crazy shit
val moves = Move.random(15) val moves = Move.random(20)
println("Scramble: $moves") println("Scramble: $moves")
val scrambledModel = EdgeModel(moves) val scrambledModel = EdgeModel(moves)
println(scrambledModel) println(scrambledModel)
val whiteCrossMoveCount = whiteCrossMoveCount(scrambledModel) doAllCrossMoveCounts(scrambledModel)
println("whiteCrossMoveCount: $whiteCrossMoveCount")
} }
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<Move>? {
val moveCounts = Array<List<Move>?>(6) { null }
for (moveCount in 1..8) { for (moveCount in 1..8) {
// build a counter of moveCount big // build a counter of moveCount big
val counter = Counter(moveCount, Move.values().size) val counter = Counter(moveCount, Move.values().size)
@ -98,15 +130,14 @@ fun whiteCrossMoveCount(edgeModel: EdgeModel): Int {
val moves = Move.combo(counter) val moves = Move.combo(counter)
// execute the moves // execute the moves
val afterMoves = edgeModel.doMoves(moves) val afterMoves = edgeModel.doMoves(moves)
// check whitecross! val crossSolved = afterMoves.crossSolved(color)
if(afterMoves.whiteCrossSolved()) { if (crossSolved) {
println("White cross found!") return@crossMoveCount moves
println("moves = $moves")
return@whiteCrossMoveCount moveCount
} }
} while (counter.increase()) } while (counter.increase())
} }
return Int.MAX_VALUE return null
} }
/** /**

View File

@ -180,9 +180,36 @@ class EdgeModel {
return this.doMoves(f.toList()) return this.doMoves(f.toList())
} }
fun whiteCrossSolved(): Boolean { fun crossSolved(color: Short): Boolean {
return model[WB] == WHITE && model[WG] == WHITE && model[WO] == WHITE && model[WR] == WHITE && return when(color) {
model[GW] == GREEN && model[OW] == ORANGE && model[RW] == RED && model[BW] == BLUE 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!")
}
}
} }
} }

View File

@ -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")
}
}