From 9d4913d1aed2332fd02f85d7ebdd6b97a1a25bec Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 5 Sep 2018 13:41:20 +0800 Subject: [PATCH] add GrowCustom operation --- .../domain/entities/operations/Operation.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt index d4c767f..d08c1b5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/operations/Operation.kt @@ -24,7 +24,10 @@ private val operations = listOf( SixGrowSeven, SevenGrowSix, FiveGrowNine, - NineGrowFive + NineGrowFive, + (1 .. 9).map { + GrowCustom(it.toByte()) + } ) suspend fun availableConverts(numbers: MutableList, operations: List): List { @@ -237,4 +240,22 @@ object NineGrowFive : Operation() { } } -class GrowCustom() +private class GrowCustom(private val number: Byte) : Operation() { + override suspend fun canConvert(numbers: MutableList, changesHistory: List): Boolean { + return changesHistory.canGrowSimpleWay + && changesHistory.firstOrNull { it is GrowCustom && it.number == number } == null + } + + override suspend fun canInvert(numbers: MutableList, changesHistory: List): Boolean { + return changesHistory.firstOrNull { it is GrowCustom && it.number == number } != null + && numbers.contains(number) + } + + override suspend fun doConvert(numbers: MutableList, changesHistory: MutableList?) { + numbers.add(number) + } + + override suspend fun doInvert(numbers: MutableList, changesHistory: MutableList?) { + numbers.remove(number) + } +}