@@ -4,7 +4,6 @@ import sc.api.plugins.Coordinates
4
4
import sc.api.plugins.Direction
5
5
import sc.api.plugins.Team
6
6
import sc.plugin2026.Board
7
- import sc.plugin2026.Field
8
7
import sc.plugin2026.FieldState
9
8
import sc.plugin2026.Move
10
9
import sc.plugin2026.PiranhaMoveMistake
@@ -86,58 +85,58 @@ object GameRuleLogic {
86
85
.map { direction -> Move (pos, direction)}
87
86
.filter { move -> checkMove(board, move) == null }
88
87
89
- private fun getDirectNeighbour (board : Board , f : Field , parentSet : Set <Field >): Set <Field > {
90
- val returnSet: MutableSet <Field > = java.util. HashSet ()
88
+ private fun getDirectNeighbour (f : Coordinates , parentSet : Set <Coordinates >): Set <Coordinates > {
89
+ val returnSet: MutableSet <Coordinates > = HashSet ()
91
90
for (i in - 1 .. 1 ) {
92
91
for (j in - 1 .. 1 ) {
93
92
val x = f.x + i
94
93
val y = f.y + j
95
94
if (x < 0 || x >= PiranhaConstants .BOARD_LENGTH || y < 0 || y >= PiranhaConstants .BOARD_LENGTH || (i == 0 && j == 0 )) continue
96
95
97
- val field : Field = board.getField (x, y)
98
- if (parentSet.contains(field )) {
99
- returnSet.add(field )
96
+ val coord = Coordinates (x, y)
97
+ if (parentSet.contains(coord )) {
98
+ returnSet.add(coord )
100
99
}
101
100
}
102
101
}
103
102
return returnSet
104
103
}
105
104
106
- private fun getSwarm (board : Board , found : MutableSet <Field >, swarm : MutableSet <Field >): MutableSet <Field > {
105
+ private fun getSwarm (found : MutableSet <Coordinates >, swarm : MutableSet <Coordinates >): MutableSet <Coordinates > {
107
106
if (swarm.isEmpty() && ! found.isEmpty()) {
108
107
val field = found.iterator().next()
109
108
swarm.add(field)
110
109
found.remove(field)
111
110
}
112
111
113
- var tmpSwarm: MutableSet <Field > = java.util. HashSet (swarm)
112
+ var tmpSwarm: MutableSet <Coordinates > = HashSet (swarm)
114
113
// O(swarm.size()) time
115
114
for (field in swarm) {
116
115
// Constant time for both calls (max of 8 neighbors)
117
- val neighbours = getDirectNeighbour(board, field, found)
116
+ val neighbours = getDirectNeighbour(field, found)
118
117
tmpSwarm.addAll(neighbours)
119
118
}
120
119
121
120
// O(found.size()*swarm.size()) time
122
121
// FIXME: Might be improved O(swarm.size()) should be possible
123
- if (swarm.size != tmpSwarm.size) tmpSwarm = getSwarm(board, found, tmpSwarm)
122
+ if (swarm.size != tmpSwarm.size) tmpSwarm = getSwarm(found, tmpSwarm)
124
123
125
124
swarm.addAll(tmpSwarm)
126
125
127
126
found.removeAll(swarm)
128
127
return swarm
129
128
}
130
129
131
- fun greatestSwarm (board : Board , fieldsToCheck : Set <Field >): Set <Field > {
130
+ @JvmStatic
131
+ fun greatestSwarm (fieldsToCheck : Set <Coordinates >): Set <Coordinates > {
132
132
// Make a copy, so there will be no conflict with direct calls.
133
- val occupiedFields: MutableSet <Field > = java.util. HashSet (fieldsToCheck)
134
- var greatestSwarm: Set <Field > = java.util. HashSet ()
133
+ val occupiedFields: MutableSet <Coordinates > = HashSet (fieldsToCheck)
134
+ var greatestSwarm: Set <Coordinates > = HashSet ()
135
135
var maxSize = - 1
136
136
137
137
// this is a maximum of MAX_FISH iterations, so it is a linear iteration altogether
138
138
while (! occupiedFields.isEmpty() && occupiedFields.size > maxSize) {
139
- val empty: MutableSet <Field > = java.util.HashSet ()
140
- val swarm: Set <Field > = getSwarm(board, occupiedFields, empty)
139
+ val swarm: Set <Coordinates > = getSwarm(occupiedFields, HashSet ())
141
140
if (maxSize < swarm.size) {
142
141
maxSize = swarm.size
143
142
greatestSwarm = swarm
@@ -146,22 +145,24 @@ object GameRuleLogic {
146
145
return greatestSwarm
147
146
}
148
147
149
- fun greatestSwarm (board : Board , player : Team ? ): Set <Field > {
150
- val occupiedFields = getOwnFields(board, player)
151
- return greatestSwarm(board, occupiedFields)
148
+ @JvmStatic
149
+ fun greatestSwarm (board : Board , team : Team ): Set <Coordinates > {
150
+ val occupiedFields = board.fieldsForTeam(team)
151
+ return greatestSwarm(occupiedFields.toHashSet())
152
152
}
153
153
154
- fun greatestSwarmSize ( board : Board , player : Team ? ): Int {
155
- return greatestSwarm (board, player).size
156
- }
154
+ @JvmStatic
155
+ fun greatestSwarmSize (board : Board , team : Team ): Int =
156
+ greatestSwarm(board, team).size
157
157
158
- fun greatestSwarmSize ( board : Board , set : Set < Field ?>): Int {
159
- return greatestSwarm(board, set).size
160
- }
158
+ @JvmStatic
159
+ fun greatestSwarmSize ( set : Set < Coordinates >): Int =
160
+ greatestSwarm(set).size
161
161
162
- fun isSwarmConnected (board : Board , player : Team ? ): Boolean {
163
- val fieldsWithFish = getOwnFields(board, player)
164
- val numGreatestSwarm: Int = greatestSwarmSize(board, fieldsWithFish)
162
+ @JvmStatic
163
+ fun isSwarmConnected (board : Board , team : Team ): Boolean {
164
+ val fieldsWithFish = board.fieldsForTeam(team)
165
+ val numGreatestSwarm: Int = greatestSwarmSize(fieldsWithFish.toHashSet())
165
166
return numGreatestSwarm == fieldsWithFish.size
166
167
}
167
168
}
0 commit comments