From 3614a7d74646b1ced9a61e22a3745fddad65a76f Mon Sep 17 00:00:00 2001 From: Joachim Date: Fri, 24 Aug 2018 00:34:40 +0200 Subject: [PATCH] First commit --- .gitignore | 3 + pom.xml | 68 ++++++++ src/main/kotlin/be/nielandt/CrossSolver.kt | 85 ++++++++++ src/main/kotlin/be/nielandt/EdgeModel.kt | 175 +++++++++++++++++++++ src/main/kotlin/be/nielandt/Hello.kt | 6 + src/test/kotlin/be/nielandt/HelloTest.kt | 8 + 6 files changed, 345 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/kotlin/be/nielandt/CrossSolver.kt create mode 100644 src/main/kotlin/be/nielandt/EdgeModel.kt create mode 100644 src/main/kotlin/be/nielandt/Hello.kt create mode 100644 src/test/kotlin/be/nielandt/HelloTest.kt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1558649 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target/ +*.iml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4e6540e --- /dev/null +++ b/pom.xml @@ -0,0 +1,68 @@ + + + + 4.0.0 + + be.nielandt + crubecross + 1.0-SNAPSHOT + jar + + be.nielandt crubecross + + + UTF-8 + 1.2.61 + 4.12 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + test + + + junit + junit + ${junit.version} + test + + + + + src/main/kotlin + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + + + diff --git a/src/main/kotlin/be/nielandt/CrossSolver.kt b/src/main/kotlin/be/nielandt/CrossSolver.kt new file mode 100644 index 0000000..b8f9828 --- /dev/null +++ b/src/main/kotlin/be/nielandt/CrossSolver.kt @@ -0,0 +1,85 @@ +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; + + companion object { + 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)]) + } + return result + } + } +} + +/** + * Six colors + */ +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") + +fun l(colorIndex: Short): String { + return COLOR_LETTER[colorIndex.toInt()] +} + +fun main(args: Array) { + val edgeModel = EdgeModel() + println(edgeModel) + println("edgeModel.whiteCrossSolved() = ${edgeModel.whiteCrossSolved()}") + + val doMove = edgeModel.doMove(Move.D) + println(doMove) + println("doMove.whiteCrossSolved() = ${doMove.whiteCrossSolved()}") + + val message = EdgeModel().doMoves(Move.R, Move.U, Move.R_) + println(message) + println("message.whiteCrossSolved() = ${message.whiteCrossSolved()}") + + val doMoves = EdgeModel().doMoves(Move.random(15)) + println("random 15 moves = ${doMoves}") + println("doMoves.whiteCrossSolved() = ${doMoves.whiteCrossSolved()}") + +} + +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 diff --git a/src/main/kotlin/be/nielandt/EdgeModel.kt b/src/main/kotlin/be/nielandt/EdgeModel.kt new file mode 100644 index 0000000..3417432 --- /dev/null +++ b/src/main/kotlin/be/nielandt/EdgeModel.kt @@ -0,0 +1,175 @@ +package be.nielandt + +/** + * 24 edge-faces (a single part of an edge), so 24 pieces that can have a color +- ---------- +- | 16 | +- |19 W 17| +- | 18 | +------------------------------------- +| 12 | 00 | 04 | 08 | +|15 O 13|03 G 01|07 R 05|11 B 09| +| 14 | 02 | 06 | 10 | +------------------------------------- +- | 20 | +- |23 Y 21| +- | 22 | +- ---------- + * white on top, green in front + * + * + * + * + */ + + +class EdgeModel { + + val model: ShortArray + + 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 L = intArrayOf(3, 23, 9, 19, 12, 13, 14, 15) + private val U = intArrayOf(16, 17, 18, 19, 0, 12, 8, 4) + private val D = intArrayOf(2, 6, 10, 14, 20, 21, 22, 23) + private val R = intArrayOf(4, 5, 6, 7, 1, 17, 11, 21) + + constructor() { + // do a sanity check + val entries = mutableListOf(F, B, L, U, D, R).flatMap { it.asList() }.groupBy { it }.entries + println("entries = ${entries}") + if (entries.any { + it.value.size != 2 + }) { + throw RuntimeException("each index should occur exactly twice in the arrays") + } + + model = shortArrayOf( + GREEN, GREEN, GREEN, GREEN, + RED, RED, RED, RED, + BLUE, BLUE, BLUE, BLUE, + ORANGE, ORANGE, ORANGE, ORANGE, + WHITE, WHITE, WHITE, WHITE, + YELLOW, YELLOW, YELLOW, YELLOW) + } + + constructor(model: ShortArray) { + this.model = model + } + + /** + * Do a single move and calculate the resulting edge model. + */ + fun doMove(move: Move): EdgeModel { + val copyOf = this.model.copyOf() + // execute the move + + /** + * Do a non prime move. Go from 0 to 1 to 2 to 3, 4 to 5 to 6 to 7. + */ + fun nonPrime(s: IntArray) { + copyOf[s[1]] = model[s[0]] + copyOf[s[2]] = model[s[1]] + copyOf[s[3]] = model[s[2]] + copyOf[s[0]] = model[s[3]] + + copyOf[s[5]] = model[s[4]] + copyOf[s[6]] = model[s[5]] + copyOf[s[7]] = model[s[6]] + copyOf[s[4]] = model[s[7]] + } + + /** + * Do a prime move. Go from 3 to 2 to 1 to 0, 7 to 6 to 5 to 4. + */ + fun prime(s: IntArray) { + copyOf[s[0]] = model[s[1]] + copyOf[s[1]] = model[s[2]] + copyOf[s[2]] = model[s[3]] + copyOf[s[3]] = model[s[0]] + + copyOf[s[4]] = model[s[5]] + copyOf[s[5]] = model[s[6]] + copyOf[s[6]] = model[s[7]] + copyOf[s[7]] = model[s[4]] + } + + /** + * Do a double move, jump one + */ + fun double(s: IntArray) { + copyOf[s[0]] = model[s[2]] + copyOf[s[2]] = model[s[0]] + + copyOf[s[1]] = model[s[3]] + copyOf[s[3]] = model[s[1]] + + copyOf[s[4]] = model[s[6]] + copyOf[s[6]] = model[s[4]] + + copyOf[s[5]] = model[s[7]] + copyOf[s[7]] = model[s[5]] + } + + when (move) { + Move.F -> nonPrime(F) + Move.F_ -> prime(F) + Move.F2 -> double(F) + Move.B -> nonPrime(B) + Move.B_ -> prime(B) + Move.B2 -> double(B) + Move.L -> nonPrime(L) + Move.L_ -> prime(L) + Move.L2 -> double(L) + Move.R -> nonPrime(R) + Move.R_ -> prime(R) + Move.R2 -> double(R) + Move.U -> nonPrime(U) + Move.U_ -> prime(U) + Move.U2 -> double(U) + Move.D -> nonPrime(D) + Move.D_ -> prime(D) + Move.D2 -> double(D) + } + return EdgeModel(copyOf) + } + + /** + * Print out the edge model. + */ + override fun toString(): String { + val trimMargin = """ + | --------- + | | ${l(model[16])} | + | | ${l(model[19])} W ${l(model[17])} | + | | ${l(model[18])} | + |--------------------------------- + || ${l(model[12])} | ${l(model[0])} | ${l(model[4])} | ${l(model[8])} | + || ${l(model[15])} O ${l(model[13])} | ${l(model[3])} G ${l(model[1])} | ${l(model[7])} R ${l(model[5])} | ${l(model[11])} B ${l(model[9])} | + || ${l(model[14])} | ${l(model[2])} | ${l(model[6])} | ${l(model[10])} | + |--------------------------------- + | | ${l(model[20])} | + | | ${l(model[23])} Y ${l(model[21])} | + | | ${l(model[22])} | + | --------- + """.trimMargin() + return trimMargin + } + + fun doMoves(f: Collection): EdgeModel { + var edgeModel = this + f.forEach { + edgeModel = edgeModel.doMove(it) + } + return edgeModel + } + + fun doMoves(vararg f: Move): 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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/be/nielandt/Hello.kt b/src/main/kotlin/be/nielandt/Hello.kt new file mode 100644 index 0000000..16d61bf --- /dev/null +++ b/src/main/kotlin/be/nielandt/Hello.kt @@ -0,0 +1,6 @@ +package be.nielandt + +fun main(args: Array) { + println("Hello, World") +} + diff --git a/src/test/kotlin/be/nielandt/HelloTest.kt b/src/test/kotlin/be/nielandt/HelloTest.kt new file mode 100644 index 0000000..32e84ff --- /dev/null +++ b/src/test/kotlin/be/nielandt/HelloTest.kt @@ -0,0 +1,8 @@ +package be.nielandt + +import org.junit.Test +import kotlin.test.assertEquals + +class HelloTest { + +}