diff --git a/src/main/kotlin/be/nielandt/Move.kt b/src/main/kotlin/be/nielandt/Move.kt index 96d924b..549fbbf 100644 --- a/src/main/kotlin/be/nielandt/Move.kt +++ b/src/main/kotlin/be/nielandt/Move.kt @@ -48,7 +48,7 @@ fun decodeMove(i: Int): String { L2 -> "L2" R2 -> "R2" else -> { - println("i = ${i}") + println("i = $i") throw IllegalArgumentException() } } diff --git a/src/main/kotlin/be/nielandt/counter/VariableCounter.kt b/src/main/kotlin/be/nielandt/counter/VariableCounter.kt index 9363580..4275d0a 100644 --- a/src/main/kotlin/be/nielandt/counter/VariableCounter.kt +++ b/src/main/kotlin/be/nielandt/counter/VariableCounter.kt @@ -14,10 +14,10 @@ import java.util.* * - [0,2,0] * - ... */ -class VariableCounter(private val baseSizes: IntArray) : Iterator> { +class VariableCounter(internal val baseSizes: IntArray) : Iterator { // init the counter with 0's, we only have a maximum as our basesize - val counter = Array(baseSizes.size) { + val counter = IntArray(baseSizes.size) { when (it) { baseSizes.size - 1 -> -1 else -> 0 @@ -33,13 +33,13 @@ class VariableCounter(private val baseSizes: IntArray) : Iterator> { return false } - override fun next(): Array { + override fun next(): IntArray { for (i in this.counter.size - 1 downTo 0) { this.counter[i]++ if (this.counter[i] == baseSizes[i]) { this.counter[i] = 0 } else { - return counter.copyOf() + break } } return counter.copyOf() diff --git a/src/main/kotlin/be/nielandt/iterator/ValidMoveIterator.kt b/src/main/kotlin/be/nielandt/iterator/ValidMoveIterator.kt index 82ee613..d777724 100644 --- a/src/main/kotlin/be/nielandt/iterator/ValidMoveIterator.kt +++ b/src/main/kotlin/be/nielandt/iterator/ValidMoveIterator.kt @@ -1,16 +1,83 @@ package be.nielandt.iterator +import be.nielandt.counter.VariableCounter +import be.nielandt.decodeMove +import java.util.* + /** * Iterates over the different moves of the given specific classes. */ -class ValidMoveIterator(val classes: List): Iterator> { +class ValidMoveIterator(val classes: List) : Iterator { - override fun hasNext(): Boolean { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + internal var expansionCounter: VariableCounter + + /** + * At initialisation time, create the internal expansion counter. This one will count appropriately for each class type, + * depending on whether the class is alone or is a duplicate. + */ + init { + // create the variable counter: if a class is 'alone', it can iterate over all 6 values. otherwise, each part can iterate over 3 + val intArray = IntArray(classes.size) + var i = 0 + while (i < classes.size - 1) { + if (i < classes.size - 1 && classes[i] == classes[i + 1]) { + intArray[i] = 3 + intArray[i + 1] = 3 + i += 2 + } else { + intArray[i] = 6 + i++ + } + } + this.expansionCounter = VariableCounter(intArray) } - override fun next(): Array { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } + override fun hasNext(): Boolean = this.expansionCounter.hasNext() + override fun next(): IntArray { + val next = this.expansionCounter.next() + println("Expansioncounter next = ${Arrays.toString(next)}") + // translate this state into a list of moves + var i = 0 + while (i < next.size) { + // process the double situation + if (classes[i] == classes[i + 1]) { + // so, we're in the same state, the first counter will get the 'low' value, the second the 'high' value + // for class FB, that would be F and B respectively + // class 0 (FB) has to expand to 0,6,12 (F,F_F2) and 1,7,13 (B,B_,B2) respectively + // class 1 (UD) has to expand to 2,8,14 (U,U_U2) and 3,9,15 (D,D_,D2) respectively + // class 2 (LR) has to expand to 4,10,16 (L,L_L2) and 5,11,17 (R,R_,R2) respectively + // next will contain 0,1,3, as the classes are a pair + val c1 = (classes[i] * 2) + 6 * next[i] + val c2 = (classes[i] * 2) + 6 * next[i + 1] + next[i] = c1 + next[i + 1] = c2 + + // bump up the counter by two + i += 2 + } + // now the single situation + else { + // class 0 (FB) has to expand to 0,1,6,7,12,13 (F,B,F_,B_,F2,B2) + // class 1 (UD) has to expand to 2,3,8,9,14,15 (U,D,U_,D_,U2,D2) + // class 2 (LR) has to expand to 4,5,10,11,16,17 (L,R,L_,R_,L2,R2) + next[i] = (classes[i] * 2) + ((next[i] / 2) * 6) + (next[i] % 2) + i++ + } + } + return next + } +} + +fun main(args: Array) { + val validMoveIterator = ValidMoveIterator(listOf(0, 0, 2, 1, 2, 0, 1, 1)) + println("classes = ${validMoveIterator.classes}") + println("basesizes = ${Arrays.toString(validMoveIterator.expansionCounter.baseSizes)}") + var count = 0 + while (validMoveIterator.hasNext()) { + println("Arrays.toString(validMoveIterator.next()) = ${Arrays.toString(validMoveIterator.next())}") + println("validMoveIterator = ${validMoveIterator.next().map { decodeMove(it) }}") + count++ + } + println("count = $count") } \ No newline at end of file