Skip to content

Commit

Permalink
feat(mq): further improve move validation
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Sep 4, 2023
1 parent 455c7bc commit 9b5e341
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion backend
2 changes: 1 addition & 1 deletion src/main/kotlin/sc/gui/controller/ClientController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ClientController: Controller() {
val best = ArrayList<IMove>()
var bestValue = Integer.MIN_VALUE
var count = 0
while(possibleMoves.hasNext() && count < 100) {
while(possibleMoves.hasNext() && count < 64) {
val next = possibleMoves.next()
val newState = (state as TwoPlayerGameState<IMove>).performMove(next)
val points = @Suppress("UNCHECKED_CAST") newState.getPointsForTeam(state.currentTeam).sum()
Expand Down
47 changes: 25 additions & 22 deletions src/main/kotlin/sc/gui/view/MississippiBoard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class MississippiBoard: View() {
KeyCode.UP, KeyCode.W ->
Advance(1).takeIf {
ship.coal + ship.movement + ship.freeAcc > 0 &&
ship.speed - ship.movement < 6
// negative movement points are turned into acceleration
ship.speed - ship.movement < PluginConstants.MAX_SPEED
}

KeyCode.LEFT, KeyCode.A ->
Expand All @@ -185,7 +186,7 @@ class MississippiBoard: View() {

KeyCode.ACCEPT, KeyCode.ENTER, KeyCode.S, KeyCode.SPACE -> {
keyEvent.consume()
if(humanMove.isEmpty()) {
if(humanMove.isEmpty() || state.currentShip.movement > state.currentShip.freeAcc + state.currentShip.coal) {
alert(Alert.AlertType.ERROR, "Unvollständiger Zug!")
} else {
if(state.currentShip.movement != 0) {
Expand All @@ -208,26 +209,28 @@ class MississippiBoard: View() {
}
}
logger.debug("Adding Human Action {}", action)
if(action != null) {
keyEvent.consume()
if(state.mustPush && action !is Push) {
alert(Alert.AlertType.ERROR, "Abdrängaktion mit Richtung 0 (Rechts) - 5 (Oben Rechts) festlegen!")
return@setOnKeyPressed
}
val newState = state.clone()
newState.currentShip.movement += PluginConstants.MAX_SPEED
action.perform(newState)?.let {
alert(Alert.AlertType.ERROR, it.message)
} ?: run {
newState.currentShip.movement -= PluginConstants.MAX_SPEED
if(humanMove.lastOrNull() is Advance &&
action is Advance &&
newState.board.doesFieldHaveCurrent(newState.currentShip.position) &&
state.board.doesFieldHaveCurrent(state.currentShip.position))
newState.currentShip.movement++
humanMove.add(action)
gameModel.gameState.set(newState)
}
if(action == null) return@setOnKeyPressed

keyEvent.consume()
if(state.mustPush && action !is Push) {
alert(Alert.AlertType.ERROR, "Abdrängaktion mit Richtung 0 (Rechts) - 5 (Oben Rechts) festlegen!")
return@setOnKeyPressed
}
val extraMovement = (ship.coal + ship.freeAcc).coerceAtMost(PluginConstants.MAX_SPEED - ship.speed)
val newState = state.clone()
val newShip = newState.currentShip
val currentAdvance = humanMove.lastOrNull() is Advance && action is Advance && state.isCurrentShipOnCurrent()
if(currentAdvance)
newShip.movement++
newShip.movement += extraMovement
action.perform(newState)?.let {
alert(Alert.AlertType.ERROR, it.message)
} ?: run {
if(currentAdvance && !newState.isCurrentShipOnCurrent())
newShip.movement--
newShip.movement -= extraMovement
humanMove.add(action)
gameModel.gameState.set(newState)
}
}
}
Expand Down

0 comments on commit 9b5e341

Please sign in to comment.