Skip to content

Commit c589118

Browse files
committed
fix(plugin): Board coordinate validation
1 parent fe40b2f commit c589118

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
4646

4747
override fun isValid(coordinates: Coordinates) =
4848
(coordinates.x + coordinates.y) % 2 == 0 &&
49+
coordinates.x >= 0 &&
4950
super.isValid(coordinates.copy(coordinates.x / 2))
5051

5152
/** Gibt das Feld an den gegebenen Koordinaten zurück. */

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
package sc.plugin2023
22

3-
import io.kotest.assertions.throwables.shouldThrow
43
import io.kotest.core.spec.style.FunSpec
54
import io.kotest.inspectors.forAll
6-
import io.kotest.matchers.booleans.shouldBeFalse
5+
import io.kotest.matchers.*
76
import io.kotest.matchers.collections.*
87
import io.kotest.matchers.ints.*
9-
import io.kotest.matchers.maps.shouldBeEmpty
108
import io.kotest.matchers.maps.shouldHaveSize
119
import io.kotest.matchers.nulls.*
12-
import io.kotest.matchers.shouldBe
1310
import io.kotest.matchers.string.*
11+
import sc.api.plugins.Coordinates
1412
import sc.api.plugins.Team
1513
import sc.helpers.shouldSerializeTo
1614
import sc.helpers.testXStream
17-
import sc.plugin2023.Move
18-
import sc.api.plugins.Coordinates
19-
import sc.api.plugins.TwoDBoard
20-
import sc.framework.plugins.Constants
21-
import sc.helpers.checkSerialization
2215
import sc.plugin2023.util.PluginConstants
23-
import sc.shared.MoveMistake
24-
import sc.shared.InvalidMoveException
2516

2617
class BoardTest: FunSpec({
2718
context("Board generation") {
@@ -32,9 +23,19 @@ class BoardTest: FunSpec({
3223
it.penguin.shouldBeNull()
3324
it.fish shouldBeInRange 1..4
3425
}
26+
3527
generatedBoard.getPenguins() shouldHaveSize 0
36-
generatedBoard[0 y 0] = Team.ONE
28+
generatedBoard[1 y 1] = Team.ONE
3729
generatedBoard.getPenguins() shouldHaveSize 1
30+
31+
arrayOf(-1 y 1, -2 y 0, -1 y 3, -1 y 0).forAll {
32+
generatedBoard.getOrNull(it).shouldBeNull()
33+
}
34+
(0 until PluginConstants.BOARD_SIZE).map { (it * 2) y 2 }.forAll {
35+
val field = generatedBoard[it]
36+
field.fish shouldBeInRange 1..4
37+
generatedBoard[it.x, it.y] shouldBe field
38+
}
3839
}
3940
test("clones well") {
4041
val board = makeBoard(0 y 0 to 1)
@@ -58,6 +59,7 @@ class BoardTest: FunSpec({
5859
}
5960
}
6061
context("Board calculates diffs") {
62+
// TODO
6163
//val board = makeBoard(0 y 0 to "r", 2 y 0 to "r")
6264
//test("empty for itself") {
6365
// board.diff(board).shouldBeEmpty()
@@ -99,6 +101,6 @@ class BoardTest: FunSpec({
99101
infix fun Int.y(other: Int) = Coordinates(this, other)
100102

101103
fun makeBoard(vararg list: Pair<Coordinates, Int>) =
102-
Board(List(PluginConstants.BOARD_SIZE) { MutableList(PluginConstants.BOARD_SIZE) { Field(1) } }).apply {
103-
list.forEach { set(it.first, Team.values().getOrNull(it.second)) }
104-
}
104+
Board(List(PluginConstants.BOARD_SIZE) { MutableList(PluginConstants.BOARD_SIZE) { Field(1) } }).apply {
105+
list.forEach { set(it.first, Team.values().getOrNull(it.second)) }
106+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ open class RectangularBoard<FIELD: IField<FIELD>>(
3131
coordinates.y in gameField.indices &&
3232
coordinates.x in gameField[coordinates.y].indices
3333

34-
/** Gibt das Feld an den gegebenen Koordinaten zurück. */
35-
protected open operator fun get(x: Int, y: Int) =
34+
/** Gibt das Feld an den gegebenen Koordinaten zurück.
35+
* Bevorzugt für interne Verwendung, da Fehler roh zurückgegeben werden. */
36+
open operator fun get(x: Int, y: Int) =
3637
gameField[y][x]
3738

3839
/** Gibt das Feld an den gegebenen Koordinaten zurück. */
40+
@Throws(IllegalArgumentException::class)
3941
override operator fun get(key: Coordinates) =
4042
try {
4143
get(key.x, key.y)

0 commit comments

Comments
 (0)