Skip to content

Commit fe40b2f

Browse files
committed
fix(plugin): proper possibleMoves calculation
1 parent ae22001 commit fe40b2f

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package sc.plugin2023
22

33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import sc.api.plugins.*
5-
import sc.shared.InvalidMoveException
6-
import sc.shared.MoveMistake
7-
import java.util.LinkedList
85
import kotlin.random.Random
96
import sc.plugin2023.util.PluginConstants as Constants
107

@@ -65,6 +62,13 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
6562
return field.fish
6663
}
6764

65+
fun possibleMovesFrom(pos: Coordinates) =
66+
Vector.DoubledHex.directions.flatMap { vector ->
67+
(1 until Constants.BOARD_SIZE).map {
68+
Move.run(pos, vector * it)
69+
}.takeWhile { getOrEmpty(it.to).fish > 0 }
70+
}
71+
6872
/** Returns a list of the non-null filter outputs */
6973
fun <T> filterFields(filter: (Field, Coordinates) -> T?): Collection<T> =
7074
gameField.flatMapIndexed { y, row ->

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data class GameState @JvmOverloads constructor(
4040
if(!move.to.minus(move.from).straight)
4141
throw InvalidMoveException(MoveMistake.INVALID_MOVE, move)
4242
// TODO avoid this check
43-
if(move !in getPossibleMoves())
43+
if(move !in board.possibleMovesFrom(move.from))
4444
throw InvalidMoveException(MoveMistake.INVALID_MOVE, move)
4545
board[move.from] = null
4646
} else {
@@ -62,14 +62,7 @@ data class GameState @JvmOverloads constructor(
6262
return if(pieces.size < PluginConstants.PENGUINS) {
6363
board.filterValues { it.fish == 1 }.map { Move(null, it.key) }
6464
} else {
65-
pieces.flatMap { (pos, _) ->
66-
// TODO incomplete
67-
Vector.DoubledHex.directions.mapNotNull { vector ->
68-
Move.run(pos, vector).takeIf { move ->
69-
board.getOrEmpty(move.to).fish > 0
70-
}
71-
}
72-
}
65+
pieces.flatMap { (pos, _) -> board.possibleMovesFrom(pos) }
7366
}
7467
}
7568

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ class BoardTest: FunSpec({
4646
clone shouldBe makeBoard(0 y 0 to 1)
4747
}
4848
}
49-
context("Board performs Moves") {
50-
context("refuses invalid moves") {
51-
test("can't move off the fields") {
52-
// TODO
53-
}
54-
test("can't move onto own piece") {
55-
// TODO
56-
}
49+
context("Board generates Moves") {
50+
val board = makeBoard(0 y 0 to 0)
51+
test("many possible moves") {
52+
// right, right down
53+
board.possibleMovesFrom(0 y 0) shouldHaveSize 2 * (PluginConstants.BOARD_SIZE - 1)
54+
}
55+
test("restricted moves") {
56+
board[1 y 1] = Team.ONE
57+
board.possibleMovesFrom(0 y 0) shouldHaveSize PluginConstants.BOARD_SIZE - 1
5758
}
5859
}
5960
context("Board calculates diffs") {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package sc.plugin2023
22

3+
import io.kotest.core.datatest.forAll
34
import io.kotest.core.spec.style.FunSpec
45
import io.kotest.matchers.*
56
import io.kotest.matchers.booleans.*
@@ -54,15 +55,16 @@ class GameStateTest: FunSpec({
5455
}
5556
context("move calculation") {
5657
test("initial placement") {
57-
val board = makeBoard()
58-
GameState(board).getPossibleMoves() shouldHaveSize board.size
58+
forAll(Board(), makeBoard()) { board ->
59+
GameState(board).getPossibleMoves() shouldHaveSize board.size
60+
}
5961
}
6062
test("first moves") {
6163
// Board with max penguins for one player
6264
GameState(makeBoard(*Array(PluginConstants.PENGUINS) { it y it to 0 })).getPossibleMoves() shouldHaveAtLeastSize PluginConstants.PENGUINS * 2
6365
}
6466
test("immovable") {
65-
// Board with max penguins for one player
67+
// Board with max penguins for both players
6668
val state = GameState(Board(listOf(
6769
MutableList(PluginConstants.PENGUINS) { Field(penguin = Team.ONE) },
6870
MutableList(PluginConstants.PENGUINS) { Field(penguin = Team.TWO) })))

0 commit comments

Comments
 (0)