Skip to content

Commit 1e163c2

Browse files
committed
feat(plugin): implement double ambers
1 parent 9aa6651 commit 1e163c2

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

plugin/src/main/sc/plugin2022/Board.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,34 @@ data class Board(
2222

2323
/** Moves a piece according to [Move] after verification.
2424
* @throws InvalidMoveException if something is wrong with the Move.
25-
* @return the moved [Piece], null if it turned into an amber. */
25+
* @return number of ambers if any */
2626
@Throws(InvalidMoveException::class)
27-
fun movePiece(move: Move): Piece? =
27+
fun movePiece(move: Move): Int =
2828
(piecePositions[move.start] ?: throw InvalidMoveException(MoveMistake.START_EMPTY, move)).let { piece ->
29-
if(!move.destination.isValid)
29+
if (!move.destination.isValid)
3030
throw InvalidMoveException(MoveMistake.OUT_OF_BOUNDS, move)
3131
if (move.delta !in piece.possibleMoves)
3232
throw InvalidMoveException(MoveMistake.INVALID_MOVEMENT, move)
3333
piecePositions[move.destination]?.let {
34-
if(it.team == piece.team)
34+
if (it.team == piece.team)
3535
throw InvalidMoveException(MoveMistake.DESTINATION_BLOCKED, move)
3636
piece.capture(it)
3737
}
3838
piecePositions.remove(move.start)
39-
if (piece.isAmber || (piece.type.isLight && move.destination.y == piece.team.opponent().startLine)) {
40-
// TODO how to indicate double amber?
41-
piecePositions.remove(move.destination)
42-
null
43-
} else {
44-
piecePositions[move.destination] = piece
45-
piece
46-
}
39+
piecePositions[move.destination] = piece
40+
checkAmber(move.destination)
4741
}
4842

43+
/** Checks whether the piece at [position] should be turned into an amber
44+
* and if yes, removes it.
45+
* @return number of ambers */
46+
fun checkAmber(position: Coordinates): Int =
47+
piecePositions[position]?.let { piece ->
48+
arrayOf(piece.isAmber, piece.type.isLight && position.y == piece.team.opponent().startLine)
49+
.sumBy { if(it) 1 else 0 }
50+
.also { if(it > 0) piecePositions.remove(position) }
51+
} ?: 0
52+
4953
override fun toString() =
5054
boardrange.joinToString("\n") { y ->
5155
boardrange.joinToString("") { x ->

plugin/src/main/sc/plugin2022/GameState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ data class GameState @JvmOverloads constructor(
2828
constructor(other: GameState): this(other.board.clone(), other.turn, other.lastMove, other.ambers.clone())
2929

3030
fun performMove(move: Move) {
31-
if (board.movePiece(move) == null)
32-
ambers[currentTeam as Team] = (ambers[currentTeam] ?: 0) + 1
31+
ambers[currentTeam as Team] = (ambers[currentTeam] ?: 0) +
32+
board.movePiece(move)
3333
turn++
3434
}
3535

plugin/src/test/sc/plugin2022/BoardTest.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import io.kotest.matchers.collections.shouldBeOneOf
77
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
88
import io.kotest.matchers.maps.shouldBeEmpty
99
import io.kotest.matchers.maps.shouldHaveSize
10-
import io.kotest.matchers.maps.shouldNotBeEmpty
11-
import io.kotest.matchers.nulls.shouldBeNull
1210
import io.kotest.matchers.shouldBe
1311
import io.kotest.matchers.string.shouldHaveLineCount
1412
import io.kotest.matchers.string.shouldNotContain
@@ -67,20 +65,20 @@ class BoardTest: FunSpec({
6765
test("not for other team") {
6866
val moewe = Piece(Moewe, Team.TWO)
6967
val board = Board(mutableMapOf(coords to moewe))
70-
board.movePiece(Move(coords, coords.copy(y = 7))) shouldBe moewe
71-
board.shouldNotBeEmpty()
68+
board.movePiece(Move(0 y 6, 0 y 7)) shouldBe 0
69+
board shouldHaveSize 1
7270
}
7371
test("from position") {
7472
val moewe = Piece(Moewe, Team.ONE)
7573
val board = Board(mutableMapOf(coords to moewe))
76-
board.movePiece(Move(coords, coords.copy(y = 7))).shouldBeNull()
74+
board.movePiece(Move(0 y 6, 0 y 7)) shouldBe 1
7775
board.shouldBeEmpty()
7876
}
7977
test("not from Robbe in position") {
8078
val robbe = Piece(Robbe, Team.ONE)
8179
val board = Board(mutableMapOf(coords to robbe))
82-
board.movePiece(Move(coords, Coordinates(2, 7))) shouldBe robbe
83-
board.shouldNotBeEmpty()
80+
board.movePiece(Move(0 y 6, 2 y 7)) shouldBe 0
81+
board shouldHaveSize 1
8482
}
8583
context("from tower") {
8684
val board = makeBoard(0 y 1 to "M", 0 y 0 to "S2", 1 y 0 to "m", 1 y 1 to "r")
@@ -90,11 +88,10 @@ class BoardTest: FunSpec({
9088
}.mistake shouldBe MoveMistake.DESTINATION_BLOCKED
9189
}
9290
test("move tower") {
93-
board.movePiece(Move(0 y 0, 1 y 1)).shouldBeNull()
91+
board.movePiece(Move(0 y 0, 1 y 1)) shouldBe 1
9492
}
9593
test("move onto tower") {
96-
// TODO that should be a double amber
97-
board.movePiece(Move(1 y 0, 0 y 0)).shouldBeNull()
94+
board.movePiece(Move(1 y 0, 0 y 0)) shouldBe 2
9895
}
9996
}
10097
}

0 commit comments

Comments
 (0)