This commit is contained in:
InsanusMokrassar 2018-09-07 22:50:11 +08:00
parent 420401f1bf
commit 25e24db57c

View File

@ -1,13 +1,6 @@
package com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations package com.github.insanusmokrassar.PsychomatrixBase.domain.entities.operations
private const val oneAsByte: Byte = 1 private const val zeroAsByte: Byte = 0
private const val twoAsByte: Byte = 2
private const val fourAsByte: Byte = 4
private const val fiveAsByte: Byte = 5
private const val sixAsByte: Byte = 6
private const val sevenAsByte: Byte = 7
private const val eightAsByte: Byte = 8
private const val nineAsByte: Byte = 9
val List<Operation>.canGrowSimpleWay: Boolean val List<Operation>.canGrowSimpleWay: Boolean
get() = contains(SixGrowSeven) get() = contains(SixGrowSeven)
@ -25,7 +18,7 @@ private val operations: List<Operation> = listOf(
FiveGrowNine, FiveGrowNine,
NineGrowFive, NineGrowFive,
*(1 .. 9).map { *(1 .. 9).map {
GrowCustom(it.toByte()) GrowCustom(it)
}.toTypedArray() }.toTypedArray()
) )
@ -60,35 +53,35 @@ sealed class Operation {
object TwoGrowFour : Operation() { object TwoGrowFour : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == twoAsByte } / 2 > changesHistory.count { it == FourGrowTwo } return numbers[2] / 2 > changesHistory.count { it == FourGrowTwo }
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(fourAsByte) && changesHistory.contains(this) return numbers[4] != zeroAsByte && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
remove(twoAsByte) numbers[2]--
remove(twoAsByte) numbers[2]--
add(fourAsByte) numbers[4]++
} }
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
add(twoAsByte) numbers[2]++
add(twoAsByte) numbers[2]++
remove(fourAsByte) numbers[4]--
} }
} }
} }
object FourGrowTwo : Operation() { object FourGrowTwo : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == fourAsByte } > changesHistory.count { it == TwoGrowFour } return numbers[4] > changesHistory.count { it == TwoGrowFour }
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == twoAsByte } > 1 && changesHistory.contains(this) return numbers[2] > 1 && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
@ -102,42 +95,43 @@ object FourGrowTwo : Operation() {
object EightGrowOne : Operation() { object EightGrowOne : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(eightAsByte) return numbers[8] > 0
&& (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory)) && (TwoGrowFour.canConvert(numbers, changesHistory) || FourGrowTwo.canConvert(numbers, changesHistory))
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == oneAsByte } > 1 && changesHistory.contains(this) return numbers[1] > 1 && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
if (count { it == twoAsByte} <= 1) { if (numbers[2] < 2) {
FourGrowTwo.convert(numbers, changesHistory) FourGrowTwo.convert(numbers, changesHistory)
} }
remove(twoAsByte) numbers[2]--
remove(twoAsByte) numbers[2]--
remove(eightAsByte) numbers[8]--
add(oneAsByte) numbers[1]++
add(oneAsByte) numbers[1]++
} }
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
FourGrowTwo.invert(numbers, changesHistory) numbers[1]--
add(eightAsByte) numbers[1]--
remove(oneAsByte) numbers[8]++
remove(oneAsByte) numbers[2]++
numbers[2]++
} }
} }
} }
object OneGrowEight : Operation() { object OneGrowEight : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.count { it == oneAsByte} > 1 return numbers[1] > 1
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(eightAsByte) && changesHistory.contains(this) return numbers[8] > 0 && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
@ -151,35 +145,41 @@ object OneGrowEight : Operation() {
object SixGrowSeven : Operation() { object SixGrowSeven : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sixAsByte) return numbers[6] > changesHistory.count { it == SevenGrowSix }
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sevenAsByte) && changesHistory.contains(this) return numbers[7] > if (changesHistory.canGrowSimpleWay) {
&& !changesHistory.containsSimpleGrows 1
} else {
0
} && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
remove(sixAsByte) numbers[6]--
add(sevenAsByte) numbers[7]++
} }
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.apply { numbers.apply {
add(sixAsByte) numbers[7]--
remove(sevenAsByte) numbers[6]++
} }
} }
} }
object SevenGrowSix : Operation() { object SevenGrowSix : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sevenAsByte) return numbers[7] - changesHistory.count { it == SixGrowSeven } > if (changesHistory.canGrowSimpleWay) {
&& !changesHistory.containsSimpleGrows 1
} else {
0
}
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return numbers.contains(sixAsByte) && changesHistory.contains(this) return numbers[6] > 0 && changesHistory.contains(this)
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
@ -195,9 +195,7 @@ object FiveGrowNine : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& ( && (
numbers.count { numbers[5] / 2 - (changesHistory.count { it == NineGrowFive })
it == fiveAsByte
} / 2 - (changesHistory.count { it == NineGrowFive })
) > changesHistory.count { it == this } ) > changesHistory.count { it == this }
} }
@ -206,18 +204,18 @@ object FiveGrowNine : Operation() {
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(nineAsByte) numbers[9]++
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(nineAsByte) numbers[9]--
} }
} }
object NineGrowFive : Operation() { object NineGrowFive : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& (numbers.count { it == nineAsByte } - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this } && (numbers[9] - changesHistory.count { it == FiveGrowNine }) > changesHistory.count { it == this }
} }
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
@ -225,17 +223,17 @@ object NineGrowFive : Operation() {
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(fiveAsByte) numbers[5]++
numbers.add(fiveAsByte) numbers[5]++
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(fiveAsByte) numbers[5]--
numbers.remove(fiveAsByte) numbers[5]--
} }
} }
class GrowCustom internal constructor(val number: Byte) : Operation() { class GrowCustom internal constructor(val number: Int) : Operation() {
override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canConvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.canGrowSimpleWay return changesHistory.canGrowSimpleWay
&& changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null
@ -243,14 +241,14 @@ class GrowCustom internal constructor(val number: Byte) : Operation() {
override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean { override fun canInvert(numbers: List<Byte>, changesHistory: List<Operation>): Boolean {
return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null
&& numbers.contains(number) && numbers[number] > 0
} }
override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doConvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.add(number) numbers[number]++
} }
override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) { override fun doInvert(numbers: MutableList<Byte>, changesHistory: MutableList<Operation>?) {
numbers.remove(number) numbers[number]--
} }
} }