added validmove iterator

This commit is contained in:
Joachim 2018-08-27 21:41:49 +02:00
parent 0c779c7a5b
commit ea45836250
3 changed files with 78 additions and 11 deletions

View File

@ -48,7 +48,7 @@ fun decodeMove(i: Int): String {
L2 -> "L2" L2 -> "L2"
R2 -> "R2" R2 -> "R2"
else -> { else -> {
println("i = ${i}") println("i = $i")
throw IllegalArgumentException() throw IllegalArgumentException()
} }
} }

View File

@ -14,10 +14,10 @@ import java.util.*
* - [0,2,0] * - [0,2,0]
* - ... * - ...
*/ */
class VariableCounter(private val baseSizes: IntArray) : Iterator<Array<Int>> { class VariableCounter(internal val baseSizes: IntArray) : Iterator<IntArray> {
// init the counter with 0's, we only have a maximum as our basesize // 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) { when (it) {
baseSizes.size - 1 -> -1 baseSizes.size - 1 -> -1
else -> 0 else -> 0
@ -33,13 +33,13 @@ class VariableCounter(private val baseSizes: IntArray) : Iterator<Array<Int>> {
return false return false
} }
override fun next(): Array<Int> { override fun next(): IntArray {
for (i in this.counter.size - 1 downTo 0) { for (i in this.counter.size - 1 downTo 0) {
this.counter[i]++ this.counter[i]++
if (this.counter[i] == baseSizes[i]) { if (this.counter[i] == baseSizes[i]) {
this.counter[i] = 0 this.counter[i] = 0
} else { } else {
return counter.copyOf() break
} }
} }
return counter.copyOf() return counter.copyOf()

View File

@ -1,16 +1,83 @@
package be.nielandt.iterator 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. * Iterates over the different moves of the given specific classes.
*/ */
class ValidMoveIterator(val classes: List<Int>): Iterator<Array<Int>> { class ValidMoveIterator(val classes: List<Int>) : Iterator<IntArray> {
override fun hasNext(): Boolean { internal var expansionCounter: VariableCounter
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
/**
* 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<Int> { override fun hasNext(): Boolean = this.expansionCounter.hasNext()
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
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<String>) {
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")
} }