Skip to content

Commit e7f8d43

Browse files
committed
fix(plugin25): allow follow-up move
1 parent b17f3ab commit e7f8d43

File tree

3 files changed

+67
-40
lines changed

3 files changed

+67
-40
lines changed

plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ data class GameState @JvmOverloads constructor(
6464
get() = currentTeamFromTurn()
6565

6666
override val isOver: Boolean
67-
get() = players.any { it.inGoal } || round >= HuIConstants.ROUND_LIMIT
67+
get() = players.any { it.inGoal } && turn.mod(2) == 0 || round >= HuIConstants.ROUND_LIMIT
68+
69+
val Hare.inGoal
70+
get() = position == board.size - 1
6871

6972
override fun clone(): GameState =
7073
copy(players = players.clone())
@@ -147,7 +150,7 @@ data class GameState @JvmOverloads constructor(
147150
lastMove = move
148151
turn++
149152
awardPositionFields()
150-
if(!moveIterator().hasNext()) {
153+
if(!isOver && !moveIterator().hasNext()) {
151154
turn++
152155
awardPositionFields()
153156
}

plugin2025/src/main/kotlin/sc/plugin2025/Hare.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ data class Hare(
2121
fun hasCard(card: Card) = cards.contains(card)
2222
fun removeCard(card: Card) = cards.remove(card)
2323

24-
val inGoal: Boolean
25-
get() = position == HuIConstants.NUM_FIELDS - 1
26-
2724
val canEnterGoal: Boolean
2825
get() = carrots <= 10 && salads == 0
2926

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,87 @@
11
package sc.plugin2025
22

3-
import io.kotest.core.spec.style.WordSpec
3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.core.spec.style.FunSpec
45
import io.kotest.matchers.*
56
import sc.api.plugins.Team
67
import sc.helpers.shouldSerializeTo
8+
import sc.shared.InvalidMoveException
79

8-
class GameStateTest: WordSpec({
9-
"GameState" should {
10-
"clone correctly" {
11-
val state = GameState()
12-
val clone = state.clone()
13-
state.currentPlayer.getCards().size shouldBe 0
14-
clone.currentPlayer.addCard(Card.EAT_SALAD)
15-
state.currentPlayer.getCards().size shouldBe 0
16-
}
17-
val state = GameState(
18-
Board(arrayOf(Field.START, Field.MARKET, Field.CARROTS, Field.SALAD, Field.HARE, Field.GOAL)),
19-
lastMove = Advance(5, Card.EAT_SALAD),
20-
turn = 1,
21-
players = listOf(
22-
Hare(Team.TWO, cards = arrayListOf(Card.SWAP_CARROTS), lastAction = Advance(5)),
23-
Hare(Team.ONE, position = 3, lastAction = Card.EAT_SALAD)
24-
)
10+
class GameStateTest: FunSpec({
11+
test("clone correctly") {
12+
val state = GameState()
13+
val clone = state.clone()
14+
state.currentPlayer.getCards().size shouldBe 0
15+
clone.currentPlayer.addCard(Card.EAT_SALAD)
16+
state.currentPlayer.getCards().size shouldBe 0
17+
}
18+
val state = GameState(
19+
Board(arrayOf(Field.START, Field.MARKET, Field.CARROTS, Field.SALAD, Field.HARE, Field.GOAL)),
20+
lastMove = Advance(5, Card.EAT_SALAD),
21+
turn = 1,
22+
players = listOf(
23+
Hare(Team.TWO, cards = arrayListOf(Card.SWAP_CARROTS), lastAction = Advance(5), carrots = 0, salads = 0),
24+
Hare(Team.ONE, position = 3, lastAction = Card.EAT_SALAD)
2525
)
26-
"behave properly" {
27-
state.currentTeam shouldBe Team.ONE
28-
state.currentField shouldBe Field.SALAD
29-
state.currentPlayer.team shouldBe Team.ONE
30-
state.currentPlayer.lastAction shouldBe Card.EAT_SALAD
31-
state.mayEatSalad() shouldBe true
32-
state.currentPlayer.lastAction = EatSalad
33-
state.mayEatSalad() shouldBe false
26+
)
27+
test("behave properly") {
28+
state.isOver shouldBe false
29+
state.currentTeam shouldBe Team.ONE
30+
state.currentField shouldBe Field.SALAD
31+
state.currentPlayer.team shouldBe Team.ONE
32+
state.currentPlayer.lastAction shouldBe Card.EAT_SALAD
33+
state.mayEatSalad() shouldBe true
34+
state.currentPlayer.lastAction = EatSalad
35+
state.mayEatSalad() shouldBe false
36+
}
37+
context("allow follow-up Move") {
38+
state.players.first().clone() shouldBe state.otherPlayer
39+
state.players.first().position = state.board.size - 1
40+
state.turn shouldBe 1
41+
state.isOver shouldBe false
42+
test("winner") {
43+
shouldThrow<InvalidMoveException> {
44+
state.performMoveDirectly(Advance(1))
45+
}.mistake shouldBe HuIMoveMistake.MUST_EAT_SALAD
46+
state.turn shouldBe 1
47+
48+
state.performMoveDirectly(EatSalad)
49+
state.turn shouldBe 2
50+
state.isOver shouldBe true
51+
}
52+
test("tie") {
53+
state.currentPlayer.run {
54+
lastAction = EatSalad
55+
carrots = 13
56+
salads = 0
57+
}
58+
state.performMoveDirectly(Advance(2))
59+
state.turn shouldBe 2
60+
state.isOver shouldBe true
3461
}
35-
"produce nice XML" {
36-
Hare(Team.TWO, lastAction = EatSalad) shouldSerializeTo """
62+
}
63+
test("produce nice XML") {
64+
Hare(Team.TWO, lastAction = EatSalad) shouldSerializeTo """
3765
<hare team="TWO" position="0" salads="5" carrots="68">
3866
<lastAction class="eatsalad"/>
3967
<cards/>
4068
</hare>
4169
""".trimIndent()
42-
Hare(Team.TWO, cards = arrayListOf(Card.HURRY_AHEAD)) shouldSerializeTo """
70+
Hare(Team.TWO, cards = arrayListOf(Card.HURRY_AHEAD)) shouldSerializeTo """
4371
<hare team="TWO" position="0" salads="5" carrots="68">
4472
<cards>
4573
<card>HURRY_AHEAD</card>
4674
</cards>
4775
</hare>
4876
""".trimIndent()
49-
50-
Board(arrayOf(Field.START)) shouldSerializeTo """
77+
78+
Board(arrayOf(Field.START)) shouldSerializeTo """
5179
<board>
5280
<field>START</field>
5381
</board>
5482
""".trimIndent()
55-
56-
state shouldSerializeTo """
83+
84+
state shouldSerializeTo """
5785
<state startTeam="TWO" turn="1">
5886
<board>
5987
<field>START</field>
@@ -63,7 +91,7 @@ class GameStateTest: WordSpec({
6391
<field>HARE</field>
6492
<field>GOAL</field>
6593
</board>
66-
<hare team="TWO" position="0" salads="5" carrots="68">
94+
<hare team="TWO" position="0" salads="0" carrots="0">
6795
<lastAction class="advance" distance="5"/>
6896
<cards>
6997
<card>SWAP_CARROTS</card>
@@ -78,6 +106,5 @@ class GameStateTest: WordSpec({
78106
</lastMove>
79107
</state>
80108
""".trimIndent()
81-
}
82109
}
83110
})

0 commit comments

Comments
 (0)