Skip to content

Commit 84d76ef

Browse files
committed
feat(plugin24/Board): bounds calculation helper
1 parent 46f30dc commit 84d76ef

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ data class Board(
2626
val logger: Logger = LoggerFactory.getLogger(this::class.java)
2727
}
2828

29+
/** Size of the map. */
30+
val bounds
31+
get() = segments.fold(Pair(0 to 0, 0 to 0)) { acc, segment ->
32+
val center = segment.center
33+
val x = center.x / 2
34+
Pair(acc.first.first.coerceAtMost(x) to acc.first.second.coerceAtLeast(x),
35+
acc.second.first.coerceAtMost(center.r) to acc.second.second.coerceAtLeast(center.r))
36+
}
37+
38+
val rectangleSize: Coordinates
39+
get() = bounds.let { Coordinates(it.first.second - it.first.first + 5, it.second.second - it.second.first + 5) }
40+
2941
override fun clone(): Board = Board(this.segments.clone(), visibleSegments)
3042

3143
internal fun getNextDirection() =

plugin/src/test/kotlin/sc/plugin2024/BoardTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.kotest.matchers.ints.*
77
import io.kotest.matchers.nulls.*
88
import io.kotest.matchers.string.*
99
import io.kotest.matchers.types.*
10+
import sc.api.plugins.Coordinates
1011
import sc.api.plugins.CubeCoordinates
1112
import sc.api.plugins.CubeDirection
1213
import sc.api.plugins.Team
@@ -59,12 +60,17 @@ class BoardTest: FunSpec({
5960
}
6061
}
6162

63+
test("board bounds") {
64+
Board(listOf(Segment.empty())).rectangleSize shouldBe Coordinates(5, 5)
65+
Board(listOf(Segment.empty(CubeCoordinates(4,0)))).rectangleSize shouldBe Coordinates(9, 5)
66+
Board(listOf(Segment.empty(CubeCoordinates(4,4)))).rectangleSize shouldBe Coordinates(11, 9)
67+
}
68+
6269
val board = Board()
6370
test("segmentIndex") {
6471
board.segmentIndex(CubeCoordinates.ORIGIN) shouldBe 0
6572
board.segmentIndex(CubeCoordinates(4, 0, -4)) shouldBe 1
6673
board.segmentIndex(CubeCoordinates(0, -3, 3)) shouldBe -1
67-
6874
}
6975

7076
test("segmentDistance") {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.thoughtworks.xstream.annotations.XStreamAsAttribute
44
import sc.framework.PublicCloneable
55
import kotlin.math.absoluteValue
66
import kotlin.math.sign
7-
import kotlin.random.Random
87

98
/**
109
* Two-dimensional coordinates tracking each axis.
@@ -32,6 +31,9 @@ data class CubeCoordinates
3231
val coordinates: IntArray
3332
get() = intArrayOf(q, r, s)
3433

34+
val x: Int
35+
get() = q * 2 + r
36+
3537
operator fun times(count: Int): CubeCoordinates =
3638
CubeCoordinates(q * count, r * count)
3739

0 commit comments

Comments
 (0)