using color enum now

This commit is contained in:
Joachim 2018-08-24 18:42:52 +02:00
parent 855bb1b918
commit 48a708ac41
4 changed files with 101 additions and 109 deletions

View File

@ -44,18 +44,12 @@ enum class Move {
} }
/** /**
* Six colors * Convert a color into a letter.
*/ */
const val WHITE: Short = 0
const val YELLOW: Short = 1
const val ORANGE: Short = 2
const val RED: Short = 3
const val GREEN: Short = 4
const val BLUE: Short = 5
val COLOR_LETTER = arrayOf("W", "Y", "O", "R", "G", "B") val COLOR_LETTER = arrayOf("W", "Y", "O", "R", "G", "B")
fun l(colorIndex: Short): String { fun l(c: Color): String {
return COLOR_LETTER[colorIndex.toInt()] return c.name.first().toString()
} }
fun main(args: Array<String>) { fun main(args: Array<String>) {
@ -97,33 +91,19 @@ fun main(args: Array<String>) {
// doAllCrossMoveCounts(scrambledModel) // doAllCrossMoveCounts(scrambledModel)
val allCrossMoveCount = allCrossMoveCount(scrambledModel) val allCrossMoveCount = allCrossMoveCount(scrambledModel)
allCrossMoveCount.forEachIndexed { index, list -> allCrossMoveCount.forEach { color, moves ->
println("cross for color: ${colorName(index.toShort())} in ${list?.size}: ${list?.joinToString(" ")}") println("cross for color: ${color} in ${moves.size}: ${moves.joinToString(" ")}")
}
}
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) { fun doAllCrossMoveCounts(edgeModel: EdgeModel) {
for (i in 0 until 6) { for (c: Color in Color.values()) {
val crossMoveCount = crossMoveCount(edgeModel, i.toShort()) val crossMoveCount = crossMoveCount(edgeModel, c)
println("${colorName(i.toShort())} in ${crossMoveCount?.size}: ${crossMoveCount?.joinToString()}") println("${c} in ${crossMoveCount?.size}: ${crossMoveCount?.joinToString()}")
} }
} }
fun crossMoveCount(edgeModel: EdgeModel, color: Short): List<Move>? { fun crossMoveCount(edgeModel: EdgeModel, color: Color): List<Move>? {
val moveCounts = Array<List<Move>?>(6) { null } val moveCounts = Array<List<Move>?>(6) { null }
for (moveCount in 1..8) { for (moveCount in 1..8) {
@ -146,9 +126,9 @@ fun crossMoveCount(edgeModel: EdgeModel, color: Short): List<Move>? {
return null return null
} }
fun allCrossMoveCount(edgeModel: EdgeModel): Array<List<Move>?> { fun allCrossMoveCount(edgeModel: EdgeModel): Map<Color, List<Move>> {
val start = Instant.now() val start = Instant.now()
val moveCounts = Array<List<Move>?>(6) { null } val moveCounts = mutableMapOf<Color, List<Move>>()
for (moveCount in 1..8) { for (moveCount in 1..8) {
// build a counter of moveCount big // build a counter of moveCount big
@ -161,23 +141,23 @@ fun allCrossMoveCount(edgeModel: EdgeModel): Array<List<Move>?> {
// execute the moves // execute the moves
val afterMoves = edgeModel.doMoves(moves) val afterMoves = edgeModel.doMoves(moves)
// check crosses that have not been found yet // check crosses that have not been found yet
moveCounts.forEachIndexed { index, list -> Color.values().forEach { color ->
if (list == null) { if(!moveCounts.containsKey(color)) {
val crossSolved = afterMoves.crossSolved(index.toShort()) val crossSolved = afterMoves.crossSolved(color)
if (crossSolved) { if (crossSolved) {
moveCounts[index] = moves moveCounts[color] = moves
} }
} }
} }
if (moveCounts.all { it != null }) { if (moveCounts.keys.size == Color.values().size) {
println("Execution time: ${Duration.between(start, Instant.now()).toSeconds()}s") println("Execution time: ${Duration.between(start, Instant.now()).toMillis() / 1000}s")
return@allCrossMoveCount moveCounts return@allCrossMoveCount moveCounts
} }
} while (counter.increase()) } while (counter.increase())
} }
println("Execution time: ${Duration.between(start, Instant.now()).toSeconds()}s") println("Execution time: ${Duration.between(start, Instant.now()).toMillis() / 1000}s")
return moveCounts return moveCounts
} }

View File

@ -1,5 +1,6 @@
package be.nielandt package be.nielandt
/** /**
* 24 edge-faces (a single part of an edge), so 24 pieces that can have a color * 24 edge-faces (a single part of an edge), so 24 pieces that can have a color
- ---------- - ----------
@ -25,7 +26,7 @@ package be.nielandt
class EdgeModel { class EdgeModel {
val model: ShortArray val model: Array<Color>
private val F = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2) private val F = intArrayOf(13, 18, 7, 20, 3, 0, 1, 2)
private val B = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5) private val B = intArrayOf(8, 9, 10, 11, 16, 15, 22, 5)
@ -44,13 +45,14 @@ class EdgeModel {
throw RuntimeException("each index should occur exactly twice in the arrays") throw RuntimeException("each index should occur exactly twice in the arrays")
} }
model = shortArrayOf( model = arrayOf(
GREEN, GREEN, GREEN, GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN,
RED, RED, RED, RED, Color.RED, Color.RED, Color.RED, Color.RED,
BLUE, BLUE, BLUE, BLUE, Color.BLUE, Color.BLUE, Color.BLUE, Color.BLUE,
ORANGE, ORANGE, ORANGE, ORANGE, Color.ORANGE, Color.ORANGE, Color.ORANGE, Color.ORANGE,
WHITE, WHITE, WHITE, WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE,
YELLOW, YELLOW, YELLOW, YELLOW) Color.YELLOW, Color.YELLOW, Color.YELLOW, Color.YELLOW
)
} }
constructor(randomMoves: Int) { constructor(randomMoves: Int) {
@ -59,7 +61,7 @@ class EdgeModel {
this.model = doMoves.model this.model = doMoves.model
} }
constructor(model: ShortArray) { constructor(model: Array<Color>) {
this.model = model this.model = model
} }
@ -180,66 +182,33 @@ class EdgeModel {
return this.doMoves(f.toList()) return this.doMoves(f.toList())
} }
fun crossSolved(color: Short): Boolean { fun crossSolved(color: Color): Boolean {
return when(color) { return when (color) {
WHITE -> { Color.WHITE -> {
model[WB] == WHITE && model[WG] == WHITE && model[WO] == WHITE && model[WR] == WHITE && model[WB] == Color.WHITE && model[WG] == Color.WHITE && model[WO] == Color.WHITE && model[WR] == Color.WHITE &&
model[BW] == BLUE && model[GW] == GREEN && model[OW] == ORANGE && model[RW] == RED model[BW] == Color.BLUE && model[GW] == Color.GREEN && model[OW] == Color.ORANGE && model[RW] == Color.RED
} }
YELLOW -> { Color.YELLOW -> {
model[YB] == YELLOW && model[YG] == YELLOW && model[YO] == YELLOW && model[YR] == YELLOW && model[YB] == Color.YELLOW && model[YG] == Color.YELLOW && model[YO] == Color.YELLOW && model[YR] == Color.YELLOW &&
model[BY] == BLUE && model[GY] == GREEN && model[OY] == ORANGE && model[RY] == RED model[BY] == Color.BLUE && model[GY] == Color.GREEN && model[OY] == Color.ORANGE && model[RY] == Color.RED
} }
RED -> { Color.RED -> {
model[RW] == RED && model[RG] == RED && model[RY] == RED && model[RB] == RED && model[RW] == Color.RED && model[RG] == Color.RED && model[RY] == Color.RED && model[RB] == Color.RED &&
model[WR] == WHITE && model[GR] == GREEN && model[YR] == YELLOW && model[BR] == BLUE model[WR] == Color.WHITE && model[GR] == Color.GREEN && model[YR] == Color.YELLOW && model[BR] == Color.BLUE
} }
BLUE -> { Color.BLUE -> {
model[BW] == BLUE && model[BR] == BLUE && model[BY] == BLUE && model[BO] == BLUE && model[BW] == Color.BLUE && model[BR] == Color.BLUE && model[BY] == Color.BLUE && model[BO] == Color.BLUE &&
model[WB] == WHITE && model[RB] == RED && model[YB] == YELLOW && model[OB] == ORANGE model[WB] == Color.WHITE && model[RB] == Color.RED && model[YB] == Color.YELLOW && model[OB] == Color.ORANGE
} }
GREEN -> { Color.GREEN -> {
model[GW] == GREEN && model[GO] == GREEN && model[GY] == GREEN && model[GR] == GREEN && model[GW] == Color.GREEN && model[GO] == Color.GREEN && model[GY] == Color.GREEN && model[GR] == Color.GREEN &&
model[WG] == WHITE && model[OG] == ORANGE && model[YG] == YELLOW && model[RG] == RED model[WG] == Color.WHITE && model[OG] == Color.ORANGE && model[YG] == Color.YELLOW && model[RG] == Color.RED
} }
ORANGE -> { Color.ORANGE -> {
model[OW] == ORANGE && model[OB] == ORANGE && model[OY] == ORANGE && model[OG] == ORANGE && model[OW] == Color.ORANGE && model[OB] == Color.ORANGE && model[OY] == Color.ORANGE && model[OG] == Color.ORANGE &&
model[WO] == WHITE && model[BO] == BLUE && model[YO] == YELLOW && model[GO] == GREEN model[WO] == Color.WHITE && model[BO] == Color.BLUE && model[YO] == Color.YELLOW && model[GO] == Color.GREEN
}
else -> {
throw IllegalStateException("6 color for lyfe!")
} }
} }
} }
} }
// sticker index constants for edges
const val GO = 3
const val GW = 0
const val GR = 1
const val GY = 2
const val WG = 18
const val WO = 19
const val WB = 16
const val WR = 17
const val YG = 20
const val YO = 23
const val YB = 22
const val YR = 21
const val RG = 7
const val RW = 4
const val RB = 5
const val RY = 6
const val BR = 11
const val BW = 8
const val BO = 9
const val BY = 10
const val OB = 15
const val OW = 12
const val OG = 13
const val OY = 14

View File

@ -0,0 +1,44 @@
package be.nielandt
/**
* These are the indices in the EdgeModel, given a readable name.
* GO = green orange, meaning the green sticker of the green/orange cubie, on the green face
*/
const val GO = 3
const val GW = 0
const val GR = 1
const val GY = 2
const val WG = 18
const val WO = 19
const val WB = 16
const val WR = 17
const val YG = 20
const val YO = 23
const val YB = 22
const val YR = 21
const val RG = 7
const val RW = 4
const val RB = 5
const val RY = 6
const val BR = 11
const val BW = 8
const val BO = 9
const val BY = 10
const val OB = 15
const val OW = 12
const val OG = 13
const val OY = 14
/**
* Six colors
*/
enum class Color(index: Int) {
WHITE(0), YELLOW(1), ORANGE(2), RED(3), GREEN(4), BLUE(5)
}

View File

@ -1,8 +1,7 @@
package be.nielandt package be.nielandt
import org.junit.Test
import org.junit.Assert.* import org.junit.Assert.*
import org.junit.Test
class EdgeModelTest { class EdgeModelTest {
@ -11,8 +10,8 @@ class EdgeModelTest {
// try each move // try each move
Move.values().forEach { move -> Move.values().forEach { move ->
val doMove = EdgeModel().doMove(move) val doMove = EdgeModel().doMove(move)
val count = (0 until 6).map { color -> val count = Color.values().map { color ->
val crossSolved = doMove.crossSolved(color.toShort()) val crossSolved = doMove.crossSolved(color)
crossSolved crossSolved
}.count { it } }.count { it }
assertEquals(1, count) assertEquals(1, count)
@ -21,9 +20,9 @@ class EdgeModelTest {
@Test @Test
fun findAtLeastOneCombo() { fun findAtLeastOneCombo() {
val white = WHITE val white = Color.WHITE
val doMoves = EdgeModel().doMoves(Move.random(20)) val doMoves = EdgeModel().doMoves(Move.random(20))
val crossMoveCount = crossMoveCount(doMoves, WHITE) val crossMoveCount = crossMoveCount(doMoves, Color.WHITE)
assertNotNull(crossMoveCount) assertNotNull(crossMoveCount)
assertTrue(crossMoveCount!!.size<9) assertTrue(crossMoveCount!!.size<9)
println(doMoves) println(doMoves)