@@ -2,18 +2,19 @@ package sc.plugin2026
22
33import com.thoughtworks.xstream.annotations.XStreamAlias
44import sc.api.plugins.Coordinates
5+ import sc.api.plugins.IBoard
56import sc.api.plugins.ITeam
67import sc.api.plugins.MutableTwoDBoard
78import sc.api.plugins.RectangularBoard
89import sc.api.plugins.Team
10+ import sc.api.plugins.deepCopy
911import sc.plugin2026.util.*
10- import kotlin.math.floor
11-
12- typealias FieldS = FieldState
12+ import kotlin.random.Random
1313
1414/* * Spielbrett für Piranhas mit [PiranhaConstants.BOARD_LENGTH]² Feldern. */
1515@XStreamAlias(value = " board" )
16- class Board (gameField : MutableTwoDBoard <FieldS > = randomFields()): RectangularBoard<FieldS>(gameField) {
16+ class Board (gameField : MutableTwoDBoard <FieldState > = randomFields()):
17+ RectangularBoard <FieldState >(gameField), IBoard {
1718
1819 // TODO later
1920 // override fun toString() =
@@ -31,8 +32,8 @@ class Board(gameField: MutableTwoDBoard<FieldS> = randomFields()): RectangularBo
3132 // }
3233
3334 override fun clone (): Board =
34- Board (Array ( gameField.size) { column -> this .gameField[column].clone() } )
35-
35+ Board (gameField.deepCopy() )
36+
3637 fun getTeam (pos : Coordinates ): Team ? =
3738 this [pos].team
3839
@@ -41,41 +42,45 @@ class Board(gameField: MutableTwoDBoard<FieldS> = randomFields()): RectangularBo
4142
4243 companion object {
4344 /* * Erstellt ein zufälliges Spielbrett. */
44- private fun randomFields (): MutableTwoDBoard <FieldS > {
45- val fields = generateFields { x, y -> FieldS (x, y) }
45+ private fun randomFields (random : Random = Random .Default ): MutableTwoDBoard <FieldState > {
46+ val fields = Array (PiranhaConstants .BOARD_LENGTH ) {
47+ Array (PiranhaConstants .BOARD_LENGTH ) { FieldState .EMPTY }
48+ }
4649
4750 // Place Piranhas
4851 for (index in 1 until PiranhaConstants .BOARD_LENGTH - 1 ) {
49- fields[0 ][index].setPiranha(Team .ONE )
50- fields[PiranhaConstants .BOARD_LENGTH - 1 ][index].setPiranha(Team .ONE )
51- fields[index][0 ].setPiranha(Team .TWO )
52- fields[index][PiranhaConstants .BOARD_LENGTH - 1 ].setPiranha(Team .TWO )
52+ val size1 = random.nextInt(2 ) + 1
53+ fields[0 ][index] = FieldState .from(Team .ONE , size1)
54+ fields[index][0 ] = FieldState .from(Team .TWO , size1)
55+
56+ val size2 = random.nextInt(2 ) + 1
57+ fields[PiranhaConstants .BOARD_LENGTH - 1 ][index] = FieldState .from(Team .ONE , size2)
58+ fields[index][PiranhaConstants .BOARD_LENGTH - 1 ] = FieldState .from(Team .TWO , size2)
5359 }
5460
5561 // Place Obstacles
5662 // only consider fields in the middle of the board
57- var blockableFields: List <Field > = fields.slice(PiranhaConstants .OBSTACLES_START .. PiranhaConstants .OBSTACLES_END ).flatMap { it.slice(PiranhaConstants .OBSTACLES_START .. PiranhaConstants .OBSTACLES_END ) }
63+ val blockableWidth = PiranhaConstants .OBSTACLES_END - PiranhaConstants .OBSTACLES_START + 1
64+ val blockableSize = blockableWidth * blockableWidth
5865 // set fields with randomly selected coordinates to blocked
59- // coordinates may not lay on same horizontal, vertical or diagonal lines with other selected coordinates
60- for (i in 0 until PiranhaConstants .NUM_OBSTACLES ) {
61- val indexOfFieldToBlock = floor(Math .random() * blockableFields.size).toInt()
62- val selectedField = blockableFields[indexOfFieldToBlock]
63- selectedField.state = FieldState .OBSTRUCTED
64- blockableFields = blockableFields.filter { field ->
65- ! (field.x == selectedField.x || field.y == selectedField.y ||
66- field.x - field.y == selectedField.x - selectedField.y ||
67- field.x + field.y == selectedField.x + selectedField.y)
66+ val obstacles = ArrayList <Coordinates >(PiranhaConstants .NUM_OBSTACLES )
67+ while (obstacles.size < PiranhaConstants .NUM_OBSTACLES ) {
68+ val index = random.nextInt(blockableSize + 1 )
69+ val pos = Coordinates (
70+ PiranhaConstants .OBSTACLES_START + index.rem(blockableWidth),
71+ PiranhaConstants .OBSTACLES_START + index.div(blockableWidth)
72+ )
73+ // coordinates may not lay on same horizontal, vertical or diagonal lines with other selected coordinates
74+ if (obstacles.none {
75+ it.x == pos.x || it.y == pos.y ||
76+ it.x - it.y == pos.x - pos.y ||
77+ it.x + it.y == pos.x + pos.y
78+ }) {
79+ obstacles.add(pos)
80+ fields[pos.x][pos.y] = FieldState .OBSTRUCTED
6881 }
6982 }
7083 return fields
7184 }
72-
73- private fun generateFields (generator : (Int , Int ) -> FieldS ): Array <Array <FieldS >> {
74- return Array (PiranhaConstants .BOARD_LENGTH ) { x ->
75- Array (PiranhaConstants .BOARD_LENGTH ) { y ->
76- generator(x, y)
77- }
78- }
79- }
8085 }
8186}
0 commit comments