diff --git a/game-engine/build.gradle.kts b/game-engine/build.gradle.kts index bb5881ef..f50f96f2 100644 --- a/game-engine/build.gradle.kts +++ b/game-engine/build.gradle.kts @@ -11,7 +11,7 @@ plugins { } group = "za.co.entelect.challenge" -version = "2019.3.1" +version = "2019.3.2" repositories { mavenLocal() diff --git a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutor.kt b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutor.kt index b90dc6e6..df22b9c9 100644 --- a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutor.kt +++ b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutor.kt @@ -27,7 +27,7 @@ class CommandExecutor(private val player: WormsPlayer, else -> player.consecutiveDoNothingsCount = 0 } - if (worm.roundsUntilUnfrozen != freezeDuration && worm.roundsUntilUnfrozen > 0) { + if (!command.ignoresBeingFrozen() && worm.roundsUntilUnfrozen != freezeDuration && worm.roundsUntilUnfrozen > 0) { logger.info { "Tried to execute command $command, but $worm is still frozen for ${worm.roundsUntilUnfrozen} round" } map.addFeedback(StandardCommandFeedback(command.toString(), 0, player.id, false, "Frozen worms cannot follow your commands", VisualizerEvent(CommandStrings.NOTHING.string, "frozen", worm, null, null, null))) diff --git a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/WormsCommand.kt b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/WormsCommand.kt index cde5d62c..89d9f53f 100644 --- a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/WormsCommand.kt +++ b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/WormsCommand.kt @@ -22,5 +22,13 @@ interface WormsCommand { */ fun execute(gameMap: WormsMap, worm: Worm): CommandFeedback + /** + * Produce true if the command should be executed regardless + * of whether the current worm is frozen. + */ + fun ignoresBeingFrozen(): Boolean { + return false + } + override fun toString(): String } diff --git a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/implementation/SelectCommand.kt b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/implementation/SelectCommand.kt index 2598b446..25d81c59 100644 --- a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/implementation/SelectCommand.kt +++ b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/command/implementation/SelectCommand.kt @@ -41,6 +41,10 @@ class SelectCommand(val wormId: Int) : WormsCommand { } } + override fun ignoresBeingFrozen(): Boolean { + return true + } + override fun toString(): String = "${CommandStrings.SELECT.string} $wormId" } diff --git a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayer.kt b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayer.kt index cd4785f1..2d20316f 100644 --- a/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayer.kt +++ b/game-engine/src/commonMain/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayer.kt @@ -46,8 +46,8 @@ class WormsPlayer private constructor(val id: Int, //Assign living worms to a local variable since it is a computed property val livingWorms = this.livingWorms if (livingWorms.isNotEmpty()) { - val nextIndex = (livingWorms.indexOf(currentWorm) + 1) % livingWorms.size - updateCurrentWorm(livingWorms[nextIndex]) + val nextWorm = livingWorms.firstOrNull { it.id > currentWorm.id } ?: livingWorms.first() + updateCurrentWorm(nextWorm) } } diff --git a/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutorTest.kt b/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutorTest.kt index 1bbe5fcd..4e98c2cc 100644 --- a/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutorTest.kt +++ b/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/command/CommandExecutorTest.kt @@ -6,6 +6,7 @@ import za.co.entelect.challenge.game.delegate.factory.TEST_CONFIG import za.co.entelect.challenge.game.engine.command.feedback.StandardCommandFeedback import za.co.entelect.challenge.game.engine.command.feedback.CommandValidation import za.co.entelect.challenge.game.engine.factory.TestMapFactory.buildMapWithCellType +import za.co.entelect.challenge.game.engine.command.implementation.SelectCommand import za.co.entelect.challenge.game.engine.map.CellType import za.co.entelect.challenge.game.engine.map.Point import za.co.entelect.challenge.game.engine.player.CommandoWorm @@ -39,6 +40,19 @@ class CommandExecutorTest { verify(command, times(0)).execute(any(), any()) } + @Test + fun test_selectFrozenWorm() { + val worms = listOf(CommandoWorm.build(0, config, Point(1, 1)), + CommandoWorm.build(1, config, Point(1, 2))) + worms[0].setAsFrozen(3) + val player = WormsPlayer.build(1, worms, config) + val command = SelectCommand(1) + val executor = CommandExecutor(player, mockMap, command, config) + + executor.execute() + assertEquals(worms[1], player.currentWorm) + } + @Test fun test_validMove() { val validCommand: WormsCommand = mock { diff --git a/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayerTest.kt b/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayerTest.kt index 4539c257..95ff5c36 100644 --- a/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayerTest.kt +++ b/game-engine/src/jvmTest/kotlin/za/co/entelect/challenge/game/engine/player/WormsPlayerTest.kt @@ -49,6 +49,46 @@ class WormsPlayerTest { assertEquals(player.worms[0], player.currentWorm) } + @Test + fun test_player_wormSelection_All() { + val player = WormsPlayer.build(2, config) + assertEquals(1, player.currentWorm.id) + + player.selectNextWorm() + assertEquals(player.worms[1], player.currentWorm) + assertEquals(2, player.currentWorm.id) + + player.selectNextWorm() + assertEquals(player.worms[2], player.currentWorm) + assertEquals(3, player.currentWorm.id) + + player.selectNextWorm() + assertEquals(player.worms[0], player.currentWorm) + assertEquals(1, player.currentWorm.id) + + player.selectNextWorm() + assertEquals(player.worms[1], player.currentWorm) + assertEquals(2, player.currentWorm.id) + + player.selectNextWorm() + assertEquals(player.worms[2], player.currentWorm) + assertEquals(3, player.currentWorm.id) + } + + @Test + fun test_player_wormSelection_currentWormDead() { + val player = WormsPlayer.build(2, config) + + player.updateCurrentWorm(player.worms[1]) + player.worms[1].health = 0; + + assertEquals(2, player.currentWorm.id); + + player.selectNextWorm(); + + assertEquals(3, player.currentWorm.id) + } + @Test fun test_player_dead() { val player = WormsPlayer.build(0, config)