diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-15 01:20:40 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-15 01:20:40 +0200 |
commit | 6be4b282381546060b2496b9ea9c9c7318d63247 (patch) | |
tree | f7ff056f7727cf49aca5b007b53010c29ea70d9b /sw-engine | |
parent | Clean up (diff) | |
download | seven-wonders-6be4b282381546060b2496b9ea9c9c7318d63247.tar.gz seven-wonders-6be4b282381546060b2496b9ea9c9c7318d63247.tar.bz2 seven-wonders-6be4b282381546060b2496b9ea9c9c7318d63247.zip |
Send score at end of game
Diffstat (limited to 'sw-engine')
-rw-r--r-- | sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt | 24 | ||||
-rw-r--r-- | sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt | 49 |
2 files changed, 49 insertions, 24 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt index c6c6c546..b79fd381 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt @@ -47,6 +47,19 @@ class Game internal constructor( currentTurnInfo = players.map { createPlayerTurnInfo(it) } } + private fun startEndGame() { + // some player may need to do additional stuff + startNewTurn() + val allDone = currentTurnInfo.all { it.action == Action.WAIT } + if (!allDone) { + return // we play the last turn like a normal one + } + val scoreBoard = computeScore() + currentTurnInfo = currentTurnInfo.map { + it.copy(action = Action.WATCH_SCORE, scoreBoard = scoreBoard) + } + } + private fun createPlayerTurnInfo(player: Player): PlayerTurnInfo { val hand = hands.createHand(player) val action = determineAction(hand, player.board) @@ -68,7 +81,10 @@ class Game internal constructor( fun getCurrentTurnInfo(): Collection<PlayerTurnInfo> = currentTurnInfo private fun determineAction(hand: List<HandCard>, board: Board): Action = when { - endOfGameReached() && board.hasSpecial(SpecialAbility.COPY_GUILD) -> determineCopyGuildAction(board) + endOfGameReached() -> when { + board.hasSpecial(SpecialAbility.COPY_GUILD) -> determineCopyGuildAction(board) + else -> Action.WAIT + } hand.size == 1 && board.hasSpecial(SpecialAbility.PLAY_LAST_CARD) -> Action.PLAY_LAST hand.size == 2 && board.hasSpecial(SpecialAbility.PLAY_LAST_CARD) -> Action.PLAY_2 hand.isEmpty() -> Action.WAIT @@ -115,7 +131,9 @@ class Game internal constructor( makeMoves() if (endOfAgeReached()) { executeEndOfAgeEvents() - if (!endOfGameReached()) { + if (endOfGameReached()) { + startEndGame() + } else { startNewAge() } } else { @@ -184,7 +202,7 @@ class Game internal constructor( /** * Computes the score for all players. */ - fun computeScore(): ScoreBoard = + private fun computeScore(): ScoreBoard = ScoreBoard(table.boards.map { it.computeScore(players[it.playerIndex]) }.sortedDescending()) private class MissingPreparedMoveException(playerIndex: Int) : diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt index ad3c28d4..320b3e94 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt @@ -15,7 +15,9 @@ import org.luxons.sevenwonders.engine.test.testCustomizableSettings import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse +import kotlin.test.assertNotNull import kotlin.test.assertTrue +import kotlin.test.fail class GameTest { @@ -33,18 +35,26 @@ class GameTest { (1..LAST_AGE).forEach { playAge(nbPlayers, game, it) } - game.computeScore() + val turnInfos = game.getCurrentTurnInfo() + assertEquals(nbPlayers, turnInfos.size) + turnInfos.forEach { + assertEquals(Action.WATCH_SCORE, it.action) + + val scoreBoard = it.scoreBoard + assertNotNull(scoreBoard) + assertEquals(nbPlayers, scoreBoard.scores.size) + } } + private fun createGame(nbPlayers: Int): Game = + GameDefinition.load().initGame(0, testCustomizableSettings(), nbPlayers) + private fun playAge(nbPlayers: Int, game: Game, age: Int) { repeat(6) { playTurn(nbPlayers, game, age, 7 - it) } } - private fun createGame(nbPlayers: Int): Game = - GameDefinition.load().initGame(0, testCustomizableSettings(), nbPlayers) - private fun playTurn(nbPlayers: Int, game: Game, ageToCheck: Int, handSize: Int) { val turnInfos = game.getCurrentTurnInfo() assertEquals(nbPlayers, turnInfos.size) @@ -68,6 +78,7 @@ class GameTest { Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> createPlayCardMove(this) Action.PICK_NEIGHBOR_GUILD -> createPickGuildMove(this) Action.WAIT, Action.SAY_READY -> null + Action.WATCH_SCORE -> fail("should not have WATCH_SCORE action before end of game") } private fun createPlayCardMove(turnInfo: PlayerTurnInfo): MoveExpectation { @@ -80,12 +91,21 @@ class GameTest { return if (playableCard != null) { planMove(turnInfo, MoveType.PLAY, playableCard, playableCard.playability.cheapestTransactions.first()) } else { - planMove(turnInfo, MoveType.DISCARD, turnInfo.hand!!.first(), - noTransactions() - ) + planMove(turnInfo, MoveType.DISCARD, turnInfo.hand!!.first(), noTransactions()) } } + private fun planMove( + turnInfo: PlayerTurnInfo, + moveType: MoveType, + card: HandCard, + transactions: ResourceTransactions + ): MoveExpectation = MoveExpectation( + turnInfo.playerIndex, + PlayerMove(moveType, card.name, transactions), + PlayedMove(turnInfo.playerIndex, moveType, card.toPlayedCard(), transactions) + ) + private fun createPickGuildMove(turnInfo: PlayerTurnInfo): MoveExpectation { val neighbourGuilds = turnInfo.neighbourGuildCards @@ -108,19 +128,6 @@ class GameTest { data class MoveExpectation(val playerIndex: Int, val moveToSend: PlayerMove, val expectedPlayedMove: PlayedMove) - private fun planMove( - turnInfo: PlayerTurnInfo, - moveType: MoveType, - card: HandCard, - transactions: ResourceTransactions - ): MoveExpectation = MoveExpectation( - turnInfo.playerIndex, - PlayerMove(moveType, card.name, transactions), - PlayedMove(turnInfo.playerIndex, moveType, card.toPlayedCard(), transactions) - ) - private fun HandCard.toPlayedCard(): TableCard = - TableCard( - name, color, requirements, chainParent, chainChildren, image, back, true - ) + TableCard(name, color, requirements, chainParent, chainChildren, image, back, true) } |