Skip to content

Commit 466236b

Browse files
committed
feat(plugin): split streamPossibleMoves up into start/nonStart parts
1 parent 09cacb4 commit 466236b

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

player/src/sc/player2021/logic/Logic.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Logic(private val client: SimpleClient): IGameHandler{
4545
client.sendMove(move)
4646
}
4747

48-
override fun gameEnded(data: GameResult, team: Team?, errorMessage: String) {
48+
override fun gameEnded(data: GameResult, team: Team?, errorMessage: String?) {
4949
logger.info("Das Spiel ist beendet.")
5050
}
5151

plugin/src/client/sc/plugin2021/AbstractClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class AbstractClient @Throws(IOException::class) constructor(
4747
}
4848

4949
/** Storage for the reason of a rule violation, if any occurs. */
50-
private lateinit var error: String
50+
private var error: String? = null
5151
fun getError() = error
5252

5353
private lateinit var roomID: String

plugin/src/client/sc/plugin2021/IGameHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ interface IGameHandler {
3434
* @param team Team des Spielers
3535
* @param errorMessage Fehlernachricht
3636
*/
37-
fun gameEnded(data: GameResult, team: Team?, errorMessage: String)
37+
fun gameEnded(data: GameResult, team: Team?, errorMessage: String?)
3838

3939
}

plugin/src/shared/sc/plugin2021/Board.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sc.plugin2021
22

33
import com.thoughtworks.xstream.annotations.XStreamAlias
4+
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
45
import sc.api.plugins.IBoard
56
import sc.plugin2021.util.Constants
67

plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object GameRuleLogic {
8080
if (bordersOnColor(gameState.board, it, move.color))
8181
throw InvalidMoveException("Field $it already borders on ${move.color}", move)
8282
}
83-
if (gameState.deployedPieces?.getValue(move.color).isNullOrEmpty()) {
83+
if (isFirstMove(gameState)) {
8484
// Check if it's the requested shape
8585
if (move.piece.kind != gameState.startPiece)
8686
throw InvalidMoveException("Expected the predetermined staring piece, ${gameState.startPiece}", move)
@@ -154,6 +154,11 @@ object GameRuleLogic {
154154
fun getPossibleMoves(gameState: GameState) =
155155
streamPossibleMoves(gameState).toSet()
156156

157+
/** Returns a list of all possible SetMoves, regardless of whether it's the first round. */
158+
@JvmStatic
159+
fun getAllPossibleMoves(gameState: GameState) =
160+
streamAllPossibleMoves(gameState).toSet()
161+
157162
/** Returns a list of possible SetMoves if it's the first round. */
158163
@JvmStatic
159164
fun getPossibleStartMoves(gameState: GameState) =
@@ -196,16 +201,22 @@ object GameRuleLogic {
196201
fun streamPossibleMoves(gameState: GameState) =
197202
if (isFirstMove(gameState))
198203
streamPossibleStartMoves(gameState)
199-
else sequence<SetMove> {
200-
val color = gameState.currentColor
201-
gameState.undeployedPieceShapes.getValue(color).map {
202-
val area = it.coordinates.area()
203-
for (y in 0 until Constants.BOARD_SIZE - area.dy)
204-
for (x in 0 until Constants.BOARD_SIZE - area.dx)
205-
for (variant in it.variants)
206-
yield(SetMove(Piece(color, it, variant.key, Coordinates(x, y))))
207-
}
208-
}.filter { isValidSetMove(gameState, it) }
204+
else
205+
streamAllPossibleMoves(gameState)
206+
207+
/** Streams all possible moves regardless of whether it's the first turn. */
208+
@JvmStatic
209+
fun streamAllPossibleMoves(gameState: GameState) = sequence<SetMove> {
210+
val color = gameState.currentColor
211+
gameState.undeployedPieceShapes.getValue(color).map {
212+
val area = it.coordinates.area()
213+
for (y in 0 until Constants.BOARD_SIZE - area.dy)
214+
for (x in 0 until Constants.BOARD_SIZE - area.dx)
215+
for (variant in it.variants) {
216+
yield(SetMove(Piece(color, it, variant.key, Coordinates(x, y))))
217+
}
218+
}
219+
}.filter { isValidSetMove(gameState, it) }
209220

210221
/** Streams all possible moves if it's the first turn of [gameState]. */
211222
@JvmStatic

server/test/sc/server/plugins/TestGame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected void onRoundBasedAction(Player fromPlayer, ProtocolMessage data) {
3737
}
3838

3939
@Override
40-
protected WinCondition checkWinCondition() {
40+
public WinCondition checkWinCondition() {
4141
if (this.getRound() > 1) {
4242
logger.info("Someone won");
4343
return new WinCondition(((TestGameState) this.getCurrentState()).getState() % 2 == 0 ? TestTeam.RED : TestTeam.BLUE, WinReason.ROUND_LIMIT_FREE_FIELDS);

0 commit comments

Comments
 (0)