diff options
Diffstat (limited to 'game-engine/src/test/kotlin')
6 files changed, 118 insertions, 118 deletions
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 index 028e7a0e..6ec279b6 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt @@ -8,9 +8,10 @@ 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.data.LAST_AGE 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.resources.bestTransaction import org.luxons.sevenwonders.game.test.testCustomizableSettings import java.util.HashMap @@ -21,10 +22,9 @@ class GameTest { val nbPlayers = 5 val game = createGame(nbPlayers) - for (age in 1..3) { + for (age in 1..LAST_AGE) { playAge(nbPlayers, game, age) } - game.computeScore() } @@ -59,11 +59,11 @@ class GameTest { } 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 + return when (turnInfo.action) { + Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> createPlayCardMove(turnInfo) + Action.PICK_NEIGHBOR_GUILD -> createPickGuildMove(turnInfo) + Action.WAIT -> null + else -> null } } @@ -78,15 +78,16 @@ class GameTest { return PlayerMove(MoveType.DISCARD, firstCardInHand.card.name) } - private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): Set<ResourceTransaction> { + private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): List<ResourceTransaction> { if (handCard.isFree) { - return emptySet() + return emptyList() } val requiredResources = handCard.card.requirements.resources val table = turnInfo.table val playerIndex = turnInfo.playerIndex - val transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex) - return transactions.toTransactions() + val transactions = bestTransaction(requiredResources, table, playerIndex) + // we're supposed to have a best transaction plan because the card is playable + return transactions!!.asList() } private fun createPickGuildMove(turnInfo: PlayerTurnInfo): PlayerMove { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt index ffb4114c..500c2666 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt @@ -59,7 +59,7 @@ class ResourcesSerializerTest { resources.add(ResourceType.WOOD, 2) resources.add(ResourceType.CLAY, 1) resources.add(ResourceType.STONE, 1) - assertEquals("\"WWSCC\"", gson!!.toJson(resources)) + assertEquals("\"CCWWS\"", gson!!.toJson(resources)) } @Test diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt index f4cf5294..005959bc 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt @@ -1,29 +1,26 @@ package org.luxons.sevenwonders.game.resources -import java.util.Arrays - +import org.junit.Assert.assertEquals import org.junit.Test import org.luxons.sevenwonders.game.api.Table -import org.luxons.sevenwonders.game.boards.Board -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals import org.luxons.sevenwonders.game.resources.Provider.LEFT_PLAYER import org.luxons.sevenwonders.game.resources.Provider.RIGHT_PLAYER -import org.luxons.sevenwonders.game.resources.ResourceType.CLAY -import org.luxons.sevenwonders.game.resources.ResourceType.GLASS -import org.luxons.sevenwonders.game.resources.ResourceType.ORE -import org.luxons.sevenwonders.game.resources.ResourceType.STONE -import org.luxons.sevenwonders.game.resources.ResourceType.WOOD +import org.luxons.sevenwonders.game.resources.ResourceType.* +import org.luxons.sevenwonders.game.test.createResources +import org.luxons.sevenwonders.game.test.createTransaction +import org.luxons.sevenwonders.game.test.createTransactions +import org.luxons.sevenwonders.game.test.testBoard +import org.luxons.sevenwonders.game.test.testTable +import java.util.Arrays class BestPriceCalculatorTest { @Test fun bestPrice_0forEmptyResources() { val table = testTable(3) - val resources = Resources() - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0).toLong()) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 0)) + val emptyResources = Resources() + val emptyTransactions = ResourceTransactions() + assertEquals(TransactionPlan(0, emptyTransactions), bestSolution(emptyResources, table, 0)) } @Test @@ -34,9 +31,6 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, left)) val resources = createResources(STONE, STONE) - assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 0).toLong()) - assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 1).toLong()) - assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2).toLong()) val stoneLeftSingle = createTransaction(LEFT_PLAYER, STONE) val stoneRightSingle = createTransaction(RIGHT_PLAYER, STONE) @@ -45,9 +39,9 @@ class BestPriceCalculatorTest { val stoneRight = createTransactions(stoneRightSingle) val stoneLeftAndRight = createTransactions(stoneLeftSingle, stoneRightSingle) - assertEquals(stoneLeft, BestPriceCalculator.bestSolution(resources, table, 0)) - assertEquals(stoneLeftAndRight, BestPriceCalculator.bestSolution(resources, table, 1)) - assertEquals(stoneRight, BestPriceCalculator.bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(2, stoneLeft), bestSolution(resources, table, 0)) + assertEquals(TransactionPlan(4, stoneLeftAndRight), bestSolution(resources, table, 1)) + assertEquals(TransactionPlan(2, stoneRight), bestSolution(resources, table, 2)) } @Test @@ -61,17 +55,14 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, opposite, left)) val resources = createResources(WOOD) - assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0).toLong()) - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1).toLong()) - assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2).toLong()) - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 3).toLong()) val woodLeft = createTransactions(LEFT_PLAYER, WOOD) val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0)) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)) - assertEquals(woodLeft, BestPriceCalculator.bestSolution(resources, table, 2)) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 3)) + + assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, table, 0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 1)) + assertEquals(TransactionPlan(2, woodLeft), bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 3)) } @Test @@ -88,15 +79,11 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, left)) val resources = createResources(WOOD) - assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0).toLong()) - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1).toLong()) - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2).toLong()) - val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0)) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, table, 0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 1)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 2)) } @Test @@ -116,14 +103,11 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, left)) val resources = createResources(ORE, CLAY) - assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0).toLong()) - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1).toLong()) - assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 2).toLong()) - val oreAndClayLeft = createTransactions(LEFT_PLAYER, ORE, CLAY) val clayRight = createTransactions(RIGHT_PLAYER, CLAY) - assertEquals(clayRight, BestPriceCalculator.bestSolution(resources, table, 0)) - assertEquals(ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)) - assertEquals(oreAndClayLeft, BestPriceCalculator.bestSolution(resources, table, 2)) + + assertEquals(TransactionPlan(1, clayRight), bestSolution(resources, table, 0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 1)) + assertEquals(TransactionPlan(4, oreAndClayLeft), bestSolution(resources, table, 2)) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt index 27f85c1a..ab0b22cf 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt @@ -12,38 +12,33 @@ import org.junit.Assert.assertTrue class ProductionTest { - private var emptyResources: Resources? = null - - private var resources1Wood: Resources? = null - - private var resources1Stone: Resources? = null - - private var resources1Stone1Wood: Resources? = null - - private var resources2Stones: Resources? = null - - private var resources2Stones3Clay: Resources? = null + private var emptyResources: Resources = Resources() + private var resources1Wood: Resources = Resources() + private var resources1Stone: Resources = Resources() + private var resources1Stone1Wood: Resources = Resources() + private var resources2Stones: Resources = Resources() + private var resources2Stones3Clay: Resources = Resources() @Before fun init() { emptyResources = Resources() resources1Wood = Resources() - resources1Wood!!.add(ResourceType.WOOD, 1) + resources1Wood.add(ResourceType.WOOD, 1) resources1Stone = Resources() - resources1Stone!!.add(ResourceType.STONE, 1) + resources1Stone.add(ResourceType.STONE, 1) resources1Stone1Wood = Resources() - resources1Stone1Wood!!.add(ResourceType.STONE, 1) - resources1Stone1Wood!!.add(ResourceType.WOOD, 1) + resources1Stone1Wood.add(ResourceType.STONE, 1) + resources1Stone1Wood.add(ResourceType.WOOD, 1) resources2Stones = Resources() - resources2Stones!!.add(ResourceType.STONE, 2) + resources2Stones.add(ResourceType.STONE, 2) resources2Stones3Clay = Resources() - resources2Stones3Clay!!.add(ResourceType.STONE, 2) - resources2Stones3Clay!!.add(ResourceType.CLAY, 3) + resources2Stones3Clay.add(ResourceType.STONE, 2) + resources2Stones3Clay.add(ResourceType.CLAY, 3) } @Test @@ -235,7 +230,7 @@ class ProductionTest { production.addChoice(ResourceType.STONE, ResourceType.WOOD) production.addChoice(ResourceType.STONE, ResourceType.ORE) production.addChoice(ResourceType.CLAY, ResourceType.LOOM, ResourceType.GLASS) - assertEquals(production.alternativeResources, production.asChoices()) + assertEquals(production.getAlternativeResources(), production.asChoices()) } @Test @@ -280,16 +275,6 @@ class ProductionTest { } @Test - fun equals_falseWhenDifferentClass() { - val production = Production() - production.addFixedResource(ResourceType.GLASS, 1) - val resources = Resources() - resources.add(ResourceType.GLASS, 1) - - assertFalse(production == resources) - } - - @Test fun equals_trueWhenSame() { val production = Production() assertEquals(production, production) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt index 11a03cd4..26ab16fa 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt @@ -1,36 +1,32 @@ package org.luxons.sevenwonders.game.resources -import java.util.ArrayList -import java.util.HashSet - -import org.junit.Test -import org.luxons.sevenwonders.game.test.* - import org.junit.Assert.assertEquals +import org.junit.Test +import org.luxons.sevenwonders.game.test.createTransaction class ResourceTransactionsTest { @Test fun toTransactions() { - val transactionList = ArrayList<ResourceTransaction>() - transactionList.add(createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD)) - transactionList.add(createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY)) - transactionList.add(createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD)) + val transactionList = listOf( + createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD), + createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY), + createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD) + ) val transactions = ResourceTransactions(transactionList) - val expectedNormalized = HashSet<ResourceTransaction>() - expectedNormalized.add( - createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY) + val expectedNormalized = setOf( + createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY), + createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD) ) - expectedNormalized.add(createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD)) - assertEquals(expectedNormalized, HashSet(transactions.toTransactions())) + assertEquals(expectedNormalized, transactions.asList().toSet()) } @Test(expected = IllegalStateException::class) fun remove_failsIfNoResourceForThatProvider() { val transactions = ResourceTransactions() - transactions.remove(Provider.LEFT_PLAYER, Resources.of(ResourceType.WOOD)) + transactions.remove(Provider.LEFT_PLAYER, Resources(ResourceType.WOOD)) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt index 72419fe3..a6ffb6c6 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt @@ -63,6 +63,50 @@ class ResourcesTest { } @Test + fun plus_zero() { + val resources = Resources(mapOf(ResourceType.CLAY to 2)) + val resourcesPlusZero = resources + Resources() + val zeroPlusResources = Resources() + resources + + assertEquals(2, resourcesPlusZero.getQuantity(ResourceType.CLAY)) + assertEquals(2, resourcesPlusZero.size()) + assertEquals(2, zeroPlusResources.getQuantity(ResourceType.CLAY)) + assertEquals(2, zeroPlusResources.size()) + } + + @Test + fun plus_sameResource() { + val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) + val resources2 = Resources(mapOf(ResourceType.WOOD to 3)) + val sum = resources1 + resources2 + + assertEquals(4, sum.getQuantity(ResourceType.WOOD).toLong()) + assertEquals(4, sum.size()) + } + + @Test + fun plus_differentResources() { + val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) + val resources2 = Resources(mapOf(ResourceType.ORE to 3)) + val sum = resources1 + resources2 + + assertEquals(1, sum.getQuantity(ResourceType.WOOD).toLong()) + assertEquals(3, sum.getQuantity(ResourceType.ORE).toLong()) + assertEquals(4, sum.size()) + } + + @Test + fun plus_overlappingResources() { + val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) + val resources2 = Resources(mapOf(ResourceType.WOOD to 2, ResourceType.ORE to 4)) + val sum = resources1 + resources2 + + assertEquals(3, sum.getQuantity(ResourceType.WOOD).toLong()) + assertEquals(4, sum.getQuantity(ResourceType.ORE).toLong()) + assertEquals(7, sum.size()) + } + + @Test fun remove_some() { val resources = Resources() resources.add(ResourceType.WOOD, 3) @@ -173,7 +217,7 @@ class ResourcesTest { fun contains_emptyContainsEmpty() { val emptyResources = Resources() val emptyResources2 = Resources() - assertTrue(emptyResources.contains(emptyResources2)) + assertTrue(emptyResources.containsAll(emptyResources2)) } @Test @@ -183,7 +227,7 @@ class ResourcesTest { val emptyResources = Resources() - assertTrue(resources.contains(emptyResources)) + assertTrue(resources.containsAll(emptyResources)) } @Test @@ -194,7 +238,7 @@ class ResourcesTest { val emptyResources = Resources() - assertTrue(resources.contains(emptyResources)) + assertTrue(resources.containsAll(emptyResources)) } @Test @@ -203,7 +247,7 @@ class ResourcesTest { resources.add(ResourceType.STONE, 1) resources.add(ResourceType.CLAY, 3) - assertTrue(resources.contains(resources)) + assertTrue(resources.containsAll(resources)) } @Test @@ -216,7 +260,7 @@ class ResourcesTest { resources2.add(ResourceType.STONE, 1) resources2.add(ResourceType.CLAY, 3) - assertTrue(resources.contains(resources2)) + assertTrue(resources.containsAll(resources2)) } @Test @@ -229,7 +273,7 @@ class ResourcesTest { resources2.add(ResourceType.STONE, 1) resources2.add(ResourceType.CLAY, 3) - assertTrue(resources.contains(resources2)) + assertTrue(resources.containsAll(resources2)) } @Test @@ -241,7 +285,7 @@ class ResourcesTest { val resources2 = Resources() resources2.add(ResourceType.CLAY, 3) - assertTrue(resources.contains(resources2)) + assertTrue(resources.containsAll(resources2)) } @Test @@ -253,7 +297,7 @@ class ResourcesTest { val resources2 = Resources() resources2.add(ResourceType.CLAY, 4) - assertTrue(resources.contains(resources2)) + assertTrue(resources.containsAll(resources2)) } @Test @@ -445,16 +489,6 @@ class ResourcesTest { } @Test - fun equals_falseWhenDifferentClass() { - val resources = Resources() - resources.add(ResourceType.GLASS, 1) - val production = Production() - production.addFixedResource(ResourceType.GLASS, 1) - - assertFalse(resources == production) - } - - @Test fun equals_trueWhenSame() { val resources = Resources() assertEquals(resources, resources) |