Skip to content

Commit a82d850

Browse files
committed
feat: revamp cloning abstraction for more flexibility
1 parent e71009e commit a82d850

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

plugin/src/main/kotlin/sc/plugin2023/Board.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sc.plugin2023
33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import sc.api.plugins.*
55
import sc.api.plugins.Coordinates
6+
import sc.framework.deepCopy
67
import kotlin.random.Random
78
import sc.plugin2023.util.PenguinConstants as Constants
89

@@ -13,7 +14,8 @@ import sc.plugin2023.util.PenguinConstants as Constants
1314
* @author soed
1415
*/
1516
@XStreamAlias(value = "board")
16-
class Board(override val gameField: MutableTwoDBoard<Field> = generateFields()): RectangularBoard<Field>(gameField) {
17+
class Board(override val gameField: MutableTwoDBoard<Field> = generateFields()):
18+
RectangularBoard<Field>(gameField), IBoard {
1719

1820
constructor(board: Board): this(board.gameField.deepCopy())
1921

@@ -62,7 +64,7 @@ class Board(override val gameField: MutableTwoDBoard<Field> = generateFields()):
6264

6365
fun getOrEmpty(key: Coordinates?) = key?.let { getOrNull(it) } ?: Field()
6466

65-
override val entries: Set<Map.Entry<Coordinates, Field>>
67+
override val entries: Set<Positioned<Field>>
6668
get() = filterFields { f, coordinates -> Positioned(coordinates, f) }.toSet()
6769

6870
override fun clone(): Board = Board(this)
@@ -91,7 +93,7 @@ class Board(override val gameField: MutableTwoDBoard<Field> = generateFields()):
9193
}
9294
}.let {
9395
it + it.reversedArray().map { list ->
94-
Array(Constants.BOARD_SIZE) { index -> list[Constants.BOARD_SIZE - index - 1].clone() }
96+
Array(Constants.BOARD_SIZE) { index -> list[Constants.BOARD_SIZE - index - 1].deepCopy() }
9597
}
9698
}
9799
}

plugin/src/main/kotlin/sc/plugin2023/Field.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class Field(val fish: Int = 0, val penguin: Team? = null) : IField<Field>,
1515
val isOccupied: Boolean
1616
get() = penguin != null
1717

18-
override fun clone(): Field = Field(fish, penguin)
18+
override fun clone(): Field = copy()
1919

2020
override fun toString(): String = penguin?.letter?.toString() ?: fish.toString()
2121
}

plugin/src/main/kotlin/sc/plugin2024/Board.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.thoughtworks.xstream.annotations.*
44
import org.slf4j.Logger
55
import org.slf4j.LoggerFactory
66
import sc.api.plugins.*
7+
import sc.framework.clone
78
import sc.plugin2024.util.BoardConverter
8-
import kotlin.math.abs
99
import kotlin.reflect.KClass
1010

1111
/**

plugin2026/src/main/kotlin/sc/plugin2026/Board.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import sc.api.plugins.ITeam
77
import sc.api.plugins.MutableTwoDBoard
88
import sc.api.plugins.RectangularBoard
99
import sc.api.plugins.Team
10-
import sc.api.plugins.deepCopy
10+
import sc.framework.deepCopy
1111
import sc.plugin2026.util.*
1212
import kotlin.random.Random
1313

plugin2026/src/main/kotlin/sc/plugin2026/FieldState.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package sc.plugin2026
22

33
import sc.api.plugins.IField
44
import sc.api.plugins.Team
5+
import sc.framework.DeepCloneable
56

6-
enum class FieldState(val size: Int): IField<FieldState> {
7+
enum class FieldState(val size: Int): IField<FieldState>, DeepCloneable<FieldState> {
78
ONE_S(1),
89
ONE_M(2),
910
ONE_L(3),
@@ -13,7 +14,7 @@ enum class FieldState(val size: Int): IField<FieldState> {
1314
OBSTRUCTED(0),
1415
EMPTY(0);
1516

16-
override fun copy(): FieldState = this
17+
override fun deepCopy(): FieldState = this
1718

1819
override val isEmpty: Boolean
1920
get() = this == EMPTY

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ package sc.api.plugins
22

33
interface IField<FIELD: IField<FIELD>> {
44
val isEmpty: Boolean
5-
fun copy(): FIELD
65
}
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/framework/PublicCloneable.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ package sc.framework
22

33
import java.io.Serializable
44

5-
interface PublicCloneable<T: Cloneable>: Cloneable, Serializable {
5+
interface PublicCloneable<T: Cloneable>: Cloneable, DeepCloneable<T> {
66
/** Eine tiefe Kopie des Objekts. */
77
public override fun clone(): T
8+
override fun deepCopy(): T = clone()
89
}
910

10-
inline fun <reified T: PublicCloneable<T>> List<T>.clone() =
11-
List(size) { this[it].clone() }
11+
interface DeepCloneable<T>: Serializable {
12+
/** Eine tiefe Kopie des Objekts. */
13+
fun deepCopy(): T
14+
}
15+
16+
inline fun <reified T: DeepCloneable<T>> List<T>.clone() =
17+
List(size) { this[it].deepCopy() }
1218

13-
inline fun <reified T: PublicCloneable<T>> List<List<T>>.deepCopy() =
14-
List(size) { row -> List(this[row].size) { column -> this[row][column].clone() } }
19+
inline fun <reified T: DeepCloneable<T>> List<List<T>>.deepCopy() =
20+
List(size) { row -> List(this[row].size) { column -> this[row][column].deepCopy() } }
1521

16-
inline fun <reified T: PublicCloneable<T>> Array<Array<T>>.deepCopy() =
17-
Array(size) { row -> Array(this[row].size) { column -> this[row][column].clone() } }
22+
inline fun <reified T: DeepCloneable<T>> Array<Array<T>>.deepCopy() =
23+
Array(size) { row -> Array(this[row].size) { column -> this[row][column].deepCopy() } }

0 commit comments

Comments
 (0)