Skip to content

Commit 255d8a2

Browse files
committed
feat(sdk): make IField enum-ready
1 parent a5dab71 commit 255d8a2

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55
import com.thoughtworks.xstream.annotations.XStreamImplicit
66
import sc.api.plugins.*
7+
import sc.framework.clone
78
import sc.framework.plugins.maxByNoEqual
89
import sc.plugin2025.GameRuleLogic.calculateCarrots
910
import sc.plugin2025.GameRuleLogic.calculateMoveableFields
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package sc.api.plugins
22

3-
import sc.framework.PublicCloneable
4-
5-
interface IField<FIELD: IField<FIELD>>: PublicCloneable<FIELD> {
3+
interface IField<FIELD: IField<FIELD>> {
64
val isEmpty: Boolean
7-
}
5+
fun copy(): FIELD
6+
}
7+
8+
inline fun <reified T: IField<T>> Array<Array<T>>.deepCopy(): Array<Array<T>> =
9+
Array(size) { row -> Array(this[row].size) { column -> this[row][column].copy() } }

sdk/src/main/server-api/sc/api/plugins/RectangularBoard.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sc.api.plugins
22

33
import com.thoughtworks.xstream.annotations.XStreamImplicit
4-
import sc.framework.PublicCloneable
54

65
/** Eine zweidimensionale Anordnung von Feldern, eine Liste von Zeilen. */
76
typealias TwoDBoard<FIELD> = Array<Array<FIELD>>
@@ -16,11 +15,7 @@ typealias MutableTwoDBoard<FIELD> = Array<Array<FIELD>>
1615
*/
1716
open class RectangularBoard<FIELD: IField<FIELD>>(
1817
@XStreamImplicit protected open val gameField: TwoDBoard<FIELD>
19-
): FieldMap<FIELD>(), IBoard, Collection<FIELD> {
20-
21-
constructor(other: RectangularBoard<FIELD>): this(other.gameField.clone())
22-
23-
override fun clone() = RectangularBoard(this)
18+
): FieldMap<FIELD>(), Collection<FIELD> {
2419

2520
override val size: Int
2621
get() = gameField.size * columnCount
@@ -113,13 +108,4 @@ open class RectangularBoard<FIELD: IField<FIELD>>(
113108
other is RectangularBoard<*> && gameField.contentDeepEquals(other.gameField)
114109

115110
override fun hashCode(): Int = gameField.contentDeepHashCode()
116-
}
117-
118-
inline fun <reified T: PublicCloneable<T>> Array<Array<T>>.deepCopy() =
119-
Array(size) { row -> Array(this[row].size) { column -> this[row][column].clone() } }
120-
121-
inline fun <reified T: PublicCloneable<T>> List<T>.clone() =
122-
List(size) { this[it].clone() }
123-
124-
inline fun <reified T: PublicCloneable<T>> List<List<T>>.deepCopy() =
125-
List(size) { row -> List(this[row].size) { column -> this[row][column].clone() } }
111+
}

sdk/src/main/server-api/sc/framework/PublicCloneable.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ import java.io.Serializable
55
interface PublicCloneable<T: Cloneable>: Cloneable, Serializable {
66
/** Eine tiefe Kopie des Objekts. */
77
public override fun clone(): T
8-
}
8+
}
9+
10+
inline fun <reified T: PublicCloneable<T>> List<T>.clone() =
11+
List(size) { this[it].clone() }
12+
13+
inline fun <reified T: PublicCloneable<T>> List<List<T>>.deepCopy() =
14+
List(size) { row -> List(this[row].size) { column -> this[row][column].clone() } }
15+
16+
inline fun <reified T: PublicCloneable<T>> Array<Array<T>>.deepCopy() =
17+
Array(size) { row -> Array(this[row].size) { column -> this[row][column].clone() } }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sc.api
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import io.kotest.matchers.shouldBe
5+
import sc.api.plugins.IField
6+
import sc.api.plugins.RectangularBoard
7+
8+
enum class Field: IField<Field> {
9+
EMPTY,
10+
ONE,
11+
TWO;
12+
13+
override val isEmpty: Boolean
14+
get() = this == EMPTY
15+
16+
override fun copy(): Field = this
17+
}
18+
19+
class RectangularBoardTest: FunSpec({
20+
test("deep copy") {
21+
val board = RectangularBoard(arrayOf(arrayOf(Field.EMPTY, Field.ONE, Field.TWO)))
22+
board.fieldsEmpty() shouldBe false
23+
//val clone = board.clone()
24+
//clone.shouldBe(board)
25+
//clone.shouldNotBeSameInstanceAs(board)
26+
board[1, 0] = Field.EMPTY
27+
//clone.shouldNotBe(board)
28+
board[2, 0] = Field.EMPTY
29+
board.fieldsEmpty() shouldBe true
30+
//clone.fieldsEmpty() shouldBe false
31+
}
32+
})

0 commit comments

Comments
 (0)