@@ -2,18 +2,19 @@ package sc.plugin2026
2
2
3
3
import com.thoughtworks.xstream.annotations.XStreamAlias
4
4
import sc.api.plugins.Coordinates
5
+ import sc.api.plugins.IBoard
5
6
import sc.api.plugins.ITeam
6
7
import sc.api.plugins.MutableTwoDBoard
7
8
import sc.api.plugins.RectangularBoard
8
9
import sc.api.plugins.Team
10
+ import sc.api.plugins.deepCopy
9
11
import sc.plugin2026.util.*
10
- import kotlin.math.floor
11
-
12
- typealias FieldS = FieldState
12
+ import kotlin.random.Random
13
13
14
14
/* * Spielbrett für Piranhas mit [PiranhaConstants.BOARD_LENGTH]² Feldern. */
15
15
@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 {
17
18
18
19
// TODO later
19
20
// override fun toString() =
@@ -31,8 +32,8 @@ class Board(gameField: MutableTwoDBoard<FieldS> = randomFields()): RectangularBo
31
32
// }
32
33
33
34
override fun clone (): Board =
34
- Board (Array ( gameField.size) { column -> this .gameField[column].clone() } )
35
-
35
+ Board (gameField.deepCopy() )
36
+
36
37
fun getTeam (pos : Coordinates ): Team ? =
37
38
this [pos].team
38
39
@@ -41,41 +42,45 @@ class Board(gameField: MutableTwoDBoard<FieldS> = randomFields()): RectangularBo
41
42
42
43
companion object {
43
44
/* * 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
+ }
46
49
47
50
// Place Piranhas
48
51
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)
53
59
}
54
60
55
61
// Place Obstacles
56
62
// 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
58
65
// 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
68
81
}
69
82
}
70
83
return fields
71
84
}
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
- }
80
85
}
81
86
}
0 commit comments