From d71576ee81abacfb16df91723e2545b308f47561 Mon Sep 17 00:00:00 2001 From: jbion Date: Sun, 8 Jul 2018 12:28:20 +0200 Subject: Kotlin mig: Game tests --- .../org/luxons/sevenwonders/game/GameTest.kt | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt (limited to 'game-engine/src/test/kotlin/org') diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt new file mode 100644 index 00000000..028e7a0e --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt @@ -0,0 +1,109 @@ +package org.luxons.sevenwonders.game + +import org.junit.Assert.* +import org.junit.Test +import org.luxons.sevenwonders.game.api.Action +import org.luxons.sevenwonders.game.api.HandCard +import org.luxons.sevenwonders.game.api.PlayerMove +import org.luxons.sevenwonders.game.api.PlayerTurnInfo +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.data.GameDefinitionLoader +import org.luxons.sevenwonders.game.moves.MoveType +import org.luxons.sevenwonders.game.resources.BestPriceCalculator +import org.luxons.sevenwonders.game.resources.ResourceTransaction +import org.luxons.sevenwonders.game.test.testCustomizableSettings +import java.util.HashMap + +class GameTest { + + @Test + fun testFullGame() { + val nbPlayers = 5 + val game = createGame(nbPlayers) + + for (age in 1..3) { + playAge(nbPlayers, game, age) + } + + game.computeScore() + } + + private fun playAge(nbPlayers: Int, game: Game, age: Int) { + for (i in 0..5) { + playTurn(nbPlayers, game, age, 7 - i) + } + } + + private fun createGame(nbPlayers: Int): Game { + val settings = testCustomizableSettings() + return GameDefinitionLoader().gameDefinition.initGame(0, settings, nbPlayers) + } + + private fun playTurn(nbPlayers: Int, game: Game, ageToCheck: Int, handSize: Int) { + val turnInfos = game.getCurrentTurnInfo() + assertEquals(nbPlayers.toLong(), turnInfos.size.toLong()) + + val sentMoves = HashMap(turnInfos.size) + for (turnInfo in turnInfos) { + assertEquals(ageToCheck.toLong(), turnInfo.currentAge.toLong()) + assertEquals(handSize.toLong(), turnInfo.hand.size.toLong()) + val move = getFirstAvailableMove(turnInfo) + if (move != null) { + game.prepareMove(turnInfo.playerIndex, move) + sentMoves[turnInfo.playerIndex] = move + } + } + assertTrue(game.allPlayersPreparedTheirMove()) + val table = game.playTurn() + checkLastPlayedMoves(sentMoves, table) + } + + private fun getFirstAvailableMove(turnInfo: PlayerTurnInfo): PlayerMove? { + when (turnInfo.action) { + Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> return createPlayCardMove(turnInfo) + Action.PICK_NEIGHBOR_GUILD -> return createPickGuildMove(turnInfo) + Action.WAIT -> return null + else -> return null + } + } + + private fun createPlayCardMove(turnInfo: PlayerTurnInfo): PlayerMove { + for (handCard in turnInfo.hand) { + if (handCard.isPlayable) { + val resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo) + return PlayerMove(MoveType.PLAY, handCard.card.name, resourcesToBuy) + } + } + val firstCardInHand = turnInfo.hand[0] + return PlayerMove(MoveType.DISCARD, firstCardInHand.card.name) + } + + private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): Set { + if (handCard.isFree) { + return emptySet() + } + val requiredResources = handCard.card.requirements.resources + val table = turnInfo.table + val playerIndex = turnInfo.playerIndex + val transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex) + return transactions.toTransactions() + } + + private fun createPickGuildMove(turnInfo: PlayerTurnInfo): PlayerMove { + val neighbourGuilds = turnInfo.neighbourGuildCards + assertNotNull(neighbourGuilds) + assertFalse(neighbourGuilds.isEmpty()) + val cardName = neighbourGuilds[0].name + return PlayerMove(MoveType.COPY_GUILD, cardName) + } + + private fun checkLastPlayedMoves(sentMoves: Map, table: Table) { + for (move in table.lastPlayedMoves) { + val sentMove = sentMoves[move.playerIndex] + assertNotNull(sentMove) + assertNotNull(move.card) + assertEquals(sentMove!!.cardName, move.card.name) + assertSame(sentMove.type, move.type) + } + } +} -- cgit