summaryrefslogtreecommitdiff
path: root/game-engine/src/test/kotlin/org
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2019-02-20 21:48:18 +0100
committerjbion <joffrey.bion@amadeus.com>2019-02-20 21:48:18 +0100
commit073e348a23598dc62f8f46bc3057f16c6baf82df (patch)
tree4bc8bb708c7ab17c7eccf144665b4800a18cd3f2 /game-engine/src/test/kotlin/org
parentSeparate internal from API stuff in game engine (diff)
downloadseven-wonders-073e348a23598dc62f8f46bc3057f16c6baf82df.tar.gz
seven-wonders-073e348a23598dc62f8f46bc3057f16c6baf82df.tar.bz2
seven-wonders-073e348a23598dc62f8f46bc3057f16c6baf82df.zip
Improve hand playability calculations and API output
Diffstat (limited to 'game-engine/src/test/kotlin/org')
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt81
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt46
2 files changed, 83 insertions, 44 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 3736ddc0..5b9d36d0 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
@@ -2,15 +2,14 @@ package org.luxons.sevenwonders.game
import org.junit.Test
import org.luxons.sevenwonders.game.api.Action
-import org.luxons.sevenwonders.game.api.HandCard
+import org.luxons.sevenwonders.game.api.Board
+import org.luxons.sevenwonders.game.api.PlayedMove
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.GameDefinition
import org.luxons.sevenwonders.game.data.LAST_AGE
import org.luxons.sevenwonders.game.moves.MoveType
-import org.luxons.sevenwonders.game.resources.ResourceTransactions
-import org.luxons.sevenwonders.game.resources.noTransactions
import org.luxons.sevenwonders.game.test.testCustomizableSettings
import java.util.HashMap
import kotlin.test.assertEquals
@@ -21,13 +20,25 @@ import kotlin.test.assertTrue
class GameTest {
@Test
- fun testFullGame() {
- val nbPlayers = 5
+ fun testFullGame3Players() {
+ playGame(nbPlayers = 3)
+ }
+
+ @Test
+ fun testFullGame5Players() {
+ playGame(nbPlayers = 6)
+ }
+
+ @Test
+ fun testFullGame7Players() {
+ playGame(nbPlayers = 7)
+ }
+
+ private fun playGame(nbPlayers: Int) {
val game = createGame(nbPlayers)
- for (age in 1..LAST_AGE) {
- playAge(nbPlayers, game, age)
- }
+ (1..LAST_AGE).forEach { playAge(nbPlayers, game, it) }
+
game.computeScore()
}
@@ -59,36 +70,24 @@ class GameTest {
checkLastPlayedMoves(sentMoves, table)
}
- private fun getFirstAvailableMove(turnInfo: PlayerTurnInfo): PlayerMove? {
- 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
- }
+ private fun getFirstAvailableMove(turnInfo: PlayerTurnInfo): PlayerMove? = when (turnInfo.action) {
+ Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> createPlayCardMove(turnInfo)
+ Action.PICK_NEIGHBOR_GUILD -> createPickGuildMove(turnInfo)
+ Action.WAIT -> null
}
private fun createPlayCardMove(turnInfo: PlayerTurnInfo): PlayerMove {
- for (handCard in turnInfo.hand) {
- if (handCard.isPlayable) {
- val transactions = findResourcesToBuyFor(handCard)
- return PlayerMove(MoveType.PLAY, handCard.name, transactions)
- }
- }
- val firstCardInHand = turnInfo.hand[0]
- return PlayerMove(MoveType.DISCARD, firstCardInHand.name)
- }
+ val playableCard = turnInfo.hand.firstOrNull { it.isPlayable }
- private fun findResourcesToBuyFor(handCard: HandCard): ResourceTransactions {
- if (handCard.isFree) {
- return noTransactions()
+ return if (playableCard != null) {
+ PlayerMove(MoveType.PLAY, playableCard.name, playableCard.cheapestTransactions.first())
+ } else {
+ PlayerMove(MoveType.DISCARD, turnInfo.hand.first().name)
}
- return handCard.cheapestTransactions.possibleTransactions.first()
}
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)
@@ -100,6 +99,30 @@ class GameTest {
assertNotNull(sentMove)
assertNotNull(move.card)
assertEquals(sentMove.cardName, move.card.name)
+ assertEquals(sentMove.type, move.type)
+ assertEquals(sentMove.transactions, move.transactions)
+
+ val board = table.boards[move.playerIndex]
+ when (sentMove.type) {
+ MoveType.PLAY, MoveType.PLAY_FREE -> checkLastPlayedCard(move, board)
+ MoveType.UPGRADE_WONDER -> checkWonderUpgraded(move, board)
+ else -> Unit
+ }
}
}
+
+ private fun checkLastPlayedCard(move: PlayedMove, board: Board) {
+ val card = board.playedCards.first { it.name == move.card.name }
+ assertTrue(card.playedDuringLastMove)
+ }
+
+ private fun checkWonderUpgraded(move: PlayedMove, board: Board) {
+ val stages = board.wonder.stages
+
+ val lastBuiltStage = stages.last { it.isBuilt }
+ val otherStages = stages - lastBuiltStage
+
+ assertEquals(move.type == MoveType.UPGRADE_WONDER, lastBuiltStage.builtDuringLastMove)
+ assertFalse(otherStages.any { it.builtDuringLastMove })
+ }
}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt
index 5abbba6e..4ed244b6 100644
--- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt
@@ -16,9 +16,7 @@ import org.luxons.sevenwonders.game.test.createRequirements
import org.luxons.sevenwonders.game.test.createTransactions
import org.luxons.sevenwonders.game.test.singleBoardPlayer
import org.luxons.sevenwonders.game.test.testBoard
-import java.util.Arrays
import kotlin.test.assertEquals
-import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
@@ -45,9 +43,18 @@ class RequirementsTest {
val board = testBoard(ResourceType.CLAY, boardGold)
val player = singleBoardPlayer(board)
- assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board))
assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, noTransactions()))
- assertEquals(boardGold >= requiredGold, requirements.areMetBy(player))
+
+ val satisfaction = requirements.computeSatisfaction(player)
+ if (boardGold >= requiredGold) {
+ if (requiredGold == 0) {
+ assertEquals(RequirementsSatisfaction.noRequirements(), satisfaction)
+ } else {
+ assertEquals(RequirementsSatisfaction.enoughGold(requiredGold), satisfaction)
+ }
+ } else {
+ assertEquals(RequirementsSatisfaction.missingRequiredGold(requiredGold), satisfaction)
+ }
}
@Theory
@@ -57,11 +64,11 @@ class RequirementsTest {
val board = testBoard(initialResource, 0)
val player = singleBoardPlayer(board)
- assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board))
assertEquals(initialResource == requiredResource, requirements.areMetWithHelpBy(board, noTransactions()))
if (initialResource == requiredResource) {
- assertTrue(requirements.areMetBy(player))
+ val satisfaction = requirements.computeSatisfaction(player)
+ assertEquals(RequirementsSatisfaction.enoughResources(), satisfaction)
}
}
@@ -79,11 +86,11 @@ class RequirementsTest {
board.production.addFixedResource(producedResource, 1)
val player = singleBoardPlayer(board)
- assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board))
assertEquals(producedResource == requiredResource, requirements.areMetWithHelpBy(board, noTransactions()))
if (producedResource == requiredResource) {
- assertTrue(requirements.areMetBy(player))
+ val satisfaction = requirements.computeSatisfaction(player)
+ assertEquals(RequirementsSatisfaction.enoughResources(), satisfaction)
}
}
@@ -100,14 +107,24 @@ class RequirementsTest {
val board = testBoard(initialResource, 2)
val neighbourBoard = testBoard(initialResource, 0)
neighbourBoard.publicProduction.addFixedResource(boughtResource, 1)
- val table = Table(Arrays.asList(board, neighbourBoard))
+ val table = Table(listOf(board, neighbourBoard))
val player = SimplePlayer(0, table)
val resources = createTransactions(Provider.RIGHT_PLAYER, boughtResource)
- assertFalse(requirements.areMetWithoutNeighboursBy(board))
- assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources))
- assertEquals(boughtResource == requiredResource, requirements.areMetBy(player))
+ val neighbourHasResource = boughtResource == requiredResource
+ assertEquals(neighbourHasResource, requirements.areMetWithHelpBy(board, resources))
+
+ val satisfaction = requirements.computeSatisfaction(player)
+ if (neighbourHasResource) {
+ val transactions = setOf(
+ createTransactions(Provider.LEFT_PLAYER, requiredResource),
+ createTransactions(Provider.RIGHT_PLAYER, requiredResource)
+ )
+ assertEquals(RequirementsSatisfaction.metWithHelp(2, transactions), satisfaction)
+ } else {
+ assertEquals(RequirementsSatisfaction.resourcesUnavailable(), satisfaction)
+ }
}
@Theory
@@ -118,14 +135,13 @@ class RequirementsTest {
val board = testBoard(initialResource, 2)
val neighbourBoard = testBoard(requiredResource, 0)
- val table = Table(Arrays.asList(board, neighbourBoard))
+ val table = Table(listOf(board, neighbourBoard))
val player = SimplePlayer(0, table)
val transactions = createTransactions(Provider.RIGHT_PLAYER, requiredResource)
- assertFalse(requirements.areMetWithoutNeighboursBy(board))
assertTrue(requirements.areMetWithHelpBy(board, transactions))
- assertTrue(requirements.areMetBy(player))
+ assertTrue(requirements.computeSatisfaction(player).satisfied)
requirements.pay(player, transactions)
bgstack15