@@ -89,13 +89,15 @@ object GameRuleLogic {
8989 .map { direction -> Move (pos, direction)}
9090 .filter { move -> checkMove(board, move) == null }
9191
92- private fun getDirectNeighbour (f : Coordinates , parentSet : Set <Coordinates >): Set <Coordinates > {
93- val returnSet: MutableSet <Coordinates > = HashSet ( )
92+ private fun getDirectNeighbour (f : Coordinates , parentSet : Collection <Coordinates >): Collection <Coordinates > {
93+ val returnSet = ArrayList <Coordinates >( 8 )
9494 for (i in - 1 .. 1 ) {
9595 for (j in - 1 .. 1 ) {
9696 val x = f.x + i
9797 val y = f.y + j
98- if (x < 0 || x >= PiranhaConstants .BOARD_LENGTH || y < 0 || y >= PiranhaConstants .BOARD_LENGTH || (i == 0 && j == 0 )) continue
98+ if (x < 0 || x >= PiranhaConstants .BOARD_LENGTH ||
99+ y < 0 || y >= PiranhaConstants .BOARD_LENGTH ||
100+ (i == 0 && j == 0 )) continue
99101
100102 val coord = Coordinates (x, y)
101103 if (parentSet.contains(coord)) {
@@ -108,7 +110,7 @@ object GameRuleLogic {
108110
109111 /* * Called with a single fish in [swarm] and the [looseFishes] left,
110112 * recursively calling with neighbors added to [swarm] to find the whole swarm. */
111- private fun getSwarm (looseFishes : Set <Coordinates >, swarm : List <Coordinates >): List <Coordinates > {
113+ private fun getSwarm (looseFishes : Collection <Coordinates >, swarm : List <Coordinates >): List <Coordinates > {
112114 val swarmNeighbours =
113115 swarm.flatMap { getDirectNeighbour(it, looseFishes) }
114116
@@ -127,11 +129,15 @@ object GameRuleLogic {
127129 var maxSwarm: Map <Coordinates , Int >? = null
128130
129131 // this is a maximum of MAX_FISH iterations, so it is a linear iteration altogether
130- while (! fieldsLeft.isEmpty() && fieldsLeft.size > maxSize) {
131- val swarmCoords = getSwarm(fieldsToCheck.keys, listOf (fieldsLeft.removeLast()))
132+ while (! fieldsLeft.isEmpty() && fieldsLeft.size * 3 > maxSize) {
133+ val swarmStart = listOf (fieldsLeft.removeLast())
134+ // println("$swarmStart - $fieldsLeft")
135+ val swarmCoords = getSwarm(fieldsLeft, swarmStart)
136+
132137 fieldsLeft.removeAll(swarmCoords)
133138 val swarm = fieldsToCheck.filterKeys { swarmCoords.contains(it) }
134139 val swarmSize = swarm.values.sum()
140+ // println("$swarmCoords - $swarm - $swarmSize")
135141 if (maxSize < swarmSize) {
136142 maxSwarm = swarm
137143 maxSize = swarmSize
0 commit comments