File tree Expand file tree Collapse file tree 4 files changed +23
-23
lines changed
main/kotlin/sc/plugin2023
test/kotlin/sc/plugin2023 Expand file tree Collapse file tree 4 files changed +23
-23
lines changed Original file line number Diff line number Diff line change @@ -2,9 +2,6 @@ package sc.plugin2023
22
33import com.thoughtworks.xstream.annotations.XStreamAlias
44import sc.api.plugins.*
5- import sc.shared.InvalidMoveException
6- import sc.shared.MoveMistake
7- import java.util.LinkedList
85import kotlin.random.Random
96import 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 ->
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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") {
Original file line number Diff line number Diff line change 11package sc.plugin2023
22
3+ import io.kotest.core.datatest.forAll
34import io.kotest.core.spec.style.FunSpec
45import io.kotest.matchers.*
56import 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 ) })))
You can’t perform that action at this time.
0 commit comments