Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugin2026/src/main/kotlin/sc/plugin2026/Board.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import sc.framework.deepCopy
import sc.plugin2026.util.PiranhaConstants
import kotlin.random.Random

val line = "-".repeat(PiranhaConstants.BOARD_LENGTH * 2 + 2)

/** Spielbrett für Piranhas mit [PiranhaConstants.BOARD_LENGTH]² Feldern. */
@XStreamAlias(value = "board")
class Board(
Expand All @@ -24,12 +22,14 @@ class Board(
}

fun prettyString(): String {
val line = "-".repeat(gameField.first().size * 2 + 2)
val map = StringBuilder(line)
gameField.forEach { row ->
map.append("\n|")
row.forEach { field ->
map.append(field.asLetters())
}
map.append("|")
}
map.append("\n").append(line)
return map.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ object GameRuleLogic {
* @return wie viele Felder weit der Zug sein sollte */
@JvmStatic
fun movementDistance(board: Board, move: Move): Int {
var count = 1
var count = 0
if(board.getOrNull(move.from)?.team != null) {
count++
}

var pos = move.from
while(true) {
pos += move.direction
Expand All @@ -24,6 +28,7 @@ object GameRuleLogic {
count++
}
}

pos = move.from
while(true) {
pos += move.direction.opposite
Expand All @@ -32,6 +37,7 @@ object GameRuleLogic {
count++
}
}

return count
}

Expand Down
36 changes: 36 additions & 0 deletions plugin2026/src/test/kotlin/sc/plugin2026/GameRuleLogicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,41 @@ class GameRuleLogicTest: FunSpec({
GameRuleLogic.checkMove(board, Move(fish, Direction.UP_LEFT)) shouldBe null
GameRuleLogic.possibleMovesFor(board, fish) shouldHaveSize 3
}
test("check movementDistance before and after moving") {
// Construct a tiny deterministic board (1x5) with fishes aligned horizontally.
val fields = arrayOf(arrayOf(
FieldState.EMPTY,
FieldState.TWO_S,
FieldState.ONE_S,
FieldState.EMPTY,
FieldState.ONE_S,
))
val state = GameState(board = Board(fields))
state.board.prettyString() shouldBe """
------------
| B1R1 R1|
------------
""".trimIndent()

val move = Move(Coordinates(4,0), Direction.LEFT)
// Sanity: move is valid and has distance 3
GameRuleLogic.movementDistance(state.board, move) shouldBe 3
GameRuleLogic.checkMove(state.board, move) shouldBe null
GameRuleLogic.targetCoordinates(state.board, move) shouldBe Coordinates(1, 0)
state.performMoveDirectly(move)

state.board.prettyString() shouldBe """
------------
| R1R1 |
------------
""".trimIndent()

val next = Move(Coordinates(1, 0), Direction.RIGHT)
GameRuleLogic.movementDistance(state.board, next) shouldBe 2
GameRuleLogic.checkMove(state.board, next) shouldBe null

// FIXME this test fails because the piranha that was eaten is gone
GameRuleLogic.movementDistance(state.board, state.lastMove!!) shouldBe 3
}
}
})
Loading