diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-12 01:52:51 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-12 01:52:51 +0200 |
commit | b2ba2034dd10f0050b43fab377668e56c448b6b4 (patch) | |
tree | e342ae1c98a7d35173713c28a8c4e5010fe2e748 | |
parent | Random cleaning (diff) | |
download | seven-wonders-b2ba2034dd10f0050b43fab377668e56c448b6b4.tar.gz seven-wonders-b2ba2034dd10f0050b43fab377668e56c448b6b4.tar.bz2 seven-wonders-b2ba2034dd10f0050b43fab377668e56c448b6b4.zip |
Wrap table+playerIndex into Player object
51 files changed, 409 insertions, 452 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt index 46c4a366..7d77a8cd 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt @@ -17,10 +17,7 @@ import org.luxons.sevenwonders.game.moves.Move import org.luxons.sevenwonders.game.score.ScoreBoard class Game( - val id: Long, - val settings: Settings, - boards: List<Board>, - private val decks: Decks + val id: Long, val settings: Settings, boards: List<Board>, private val decks: Decks ) { private val nbPlayers: Int = boards.size private val table: Table = Table(boards) @@ -44,7 +41,7 @@ class Game( } private fun createPlayerTurnInfo(playerIndex: Int): PlayerTurnInfo { - val hand = hands.createHand(table, playerIndex) + val hand = hands.createHand(player(playerIndex)) val action = determineAction(hand, table.getBoard(playerIndex)) var neighbourGuildCards = emptyList<Card>() @@ -81,17 +78,12 @@ class Game( @Throws(InvalidMoveException::class) fun prepareMove(playerIndex: Int, playerMove: PlayerMove): CardBack { val card = decks.getCard(table.currentAge, playerMove.cardName) - val move = playerMove.type.resolve(playerIndex, card, playerMove) - validate(move) + val context = PlayerContext(playerIndex, table, hands[playerIndex]) + val move = playerMove.type.resolve(playerMove, card, context) preparedMoves[playerIndex] = move return card.back } - @Throws(InvalidMoveException::class) - private fun validate(move: Move) { - move.validate(table, hands[move.playerIndex]) - } - fun allPlayersPreparedTheirMove(): Boolean { val nbExpectedMoves = currentTurnInfo.filter { it.action !== Action.WAIT }.count() return preparedMoves.size == nbExpectedMoves @@ -147,8 +139,8 @@ class Game( private fun placePreparedCards(playedMoves: List<Move>) { playedMoves.forEach { move -> - move.place(table, discardedCards, settings) - hands = hands.remove(move.playerIndex, move.card) + move.place(discardedCards, settings) + hands = hands.remove(move.playerContext.index, move.card) } } @@ -168,9 +160,11 @@ class Game( } private fun activatePlayedCards(playedMoves: List<Move>) = - playedMoves.forEach { it.activate(table, discardedCards, settings) } + playedMoves.forEach { it.activate(discardedCards, settings) } + + fun computeScore(): ScoreBoard = ScoreBoard(table.boards.map { it.computeScore(player(it.playerIndex)) }) - fun computeScore(): ScoreBoard = ScoreBoard(table.boards.map { it.computePoints(table) }) + private fun player(playerIndex: Int) = SimplePlayer(playerIndex, table) private class MissingPreparedMoveException internal constructor(playerIndex: Int) : IllegalStateException("Player $playerIndex has not prepared his move") diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Player.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Player.kt new file mode 100644 index 00000000..1fc34711 --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Player.kt @@ -0,0 +1,28 @@ +package org.luxons.sevenwonders.game + +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.boards.Board +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition +import org.luxons.sevenwonders.game.cards.Card + +interface Player { + val index: Int + val board: Board + fun getBoard(relativePosition: RelativeBoardPosition): Board +} + +data class SimplePlayer( + override val index: Int, + private val table: Table +): Player { + override val board = table.getBoard(index) + override fun getBoard(relativePosition: RelativeBoardPosition) = table.getBoard(index, relativePosition) +} + +data class PlayerContext( + override val index: Int, + private val table: Table, + val hand: List<Card> +) : Player by SimplePlayer(index, table) { + val currentAge = table.currentAge +} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/HandCard.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/HandCard.kt index 1b11edef..22b04953 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/HandCard.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/HandCard.kt @@ -1,20 +1,14 @@ package org.luxons.sevenwonders.game.api +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.cards.Card /** * A card with contextual information relative to the hand it is sitting in. The extra information is especially useful * because it frees the client from a painful business logic implementation. */ -class HandCard(val card: Card, table: Table, playerIndex: Int) { - - val isChainable: Boolean - val isFree: Boolean - val isPlayable: Boolean = card.isPlayable(table, playerIndex) - - init { - val board = table.getBoard(playerIndex) - this.isChainable = card.isChainableOn(board) - this.isFree = card.isFreeFor(board) - } +class HandCard(val card: Card, player: Player) { + val isChainable: Boolean = card.isChainableOn(player.board) + val isFree: Boolean = card.isFreeFor(player.board) + val isPlayable: Boolean = card.isPlayableBy(player) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt index 77358d03..38a80989 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt @@ -1,7 +1,7 @@ package org.luxons.sevenwonders.game.boards +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.Settings -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.Color import org.luxons.sevenwonders.game.data.Age @@ -74,22 +74,22 @@ class Board(val wonder: Wonder, val playerIndex: Int, settings: Settings) { consumedFreeCards[age] = true } - fun computePoints(table: Table): PlayerScore = PlayerScore( + fun computeScore(player: Player): PlayerScore = PlayerScore( gold, mapOf( - ScoreCategory.CIVIL to computePointsForCards(table, Color.BLUE), + ScoreCategory.CIVIL to computePointsForCards(player, Color.BLUE), ScoreCategory.MILITARY to military.totalPoints, ScoreCategory.SCIENCE to science.computePoints(), - ScoreCategory.TRADE to computePointsForCards(table, Color.YELLOW), - ScoreCategory.GUILD to computePointsForCards(table, Color.PURPLE), - ScoreCategory.WONDER to wonder.computePoints(table, playerIndex), + ScoreCategory.TRADE to computePointsForCards(player, Color.YELLOW), + ScoreCategory.GUILD to computePointsForCards(player, Color.PURPLE), + ScoreCategory.WONDER to wonder.computePoints(player), ScoreCategory.GOLD to computeGoldPoints() ) ) - private fun computePointsForCards(table: Table, color: Color): Int = + private fun computePointsForCards(player: Player, color: Color): Int = playedCards.filter { it.color === color } .flatMap { it.effects } - .map { it.computePoints(table, playerIndex) } + .map { it.computePoints(player) } .sum() private fun computeGoldPoints(): Int = gold / 3 * pointsPer3Gold diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt index cf94bef4..2c22ca54 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.cards -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.effects.Effect import org.luxons.sevenwonders.game.resources.ResourceTransactions @@ -27,17 +27,16 @@ data class Card( private fun isFreeWithoutChainingOn(board: Board) = isAllowedOnBoard(board) && requirements.areMetWithoutNeighboursBy(board) && requirements.gold == 0 - fun isPlayable(table: Table, playerIndex: Int): Boolean { - val board = table.getBoard(playerIndex) - return isAllowedOnBoard(board) && (isChainableOn(board) || requirements.areMetBy(table, playerIndex)) + fun isPlayableBy(player: Player): Boolean { + val board = player.board + return isAllowedOnBoard(board) && (isChainableOn(board) || requirements.areMetBy(player)) } - fun applyTo(table: Table, playerIndex: Int, transactions: ResourceTransactions) { - val playerBoard = table.getBoard(playerIndex) - if (!isChainableOn(playerBoard)) { - requirements.pay(table, playerIndex, transactions) + fun applyTo(player: Player, transactions: ResourceTransactions) { + if (!isChainableOn(player.board)) { + requirements.pay(player, transactions) } - effects.forEach { e -> e.apply(table, playerIndex) } + effects.forEach { it.applyTo(player) } } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Hands.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Hands.kt index 50959596..c64e1886 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Hands.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Hands.kt @@ -1,7 +1,7 @@ package org.luxons.sevenwonders.game.cards +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.api.HandCard -import org.luxons.sevenwonders.game.api.Table class Hands internal constructor(private val hands: List<List<Card>>) { @@ -23,8 +23,8 @@ class Hands internal constructor(private val hands: List<List<Card>>) { return Hands(mutatedHands) } - fun createHand(table: Table, playerIndex: Int): List<HandCard> { - return hands[playerIndex].map { c -> HandCard(c, table, playerIndex) } + fun createHand(player: Player): List<HandCard> { + return hands[player.index].map { c -> HandCard(c, player) } } fun rotate(direction: HandRotationDirection): Hands { diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Requirements.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Requirements.kt index 4417620f..c253f86c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Requirements.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Requirements.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.cards -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.Resources @@ -33,9 +33,7 @@ data class Requirements @JvmOverloads constructor( if (!hasRequiredGold(board, boughtResources)) { return false } - return if (producesRequiredResources(board)) { - true - } else producesRequiredResourcesWithHelp(board, boughtResources) + return producesRequiredResources(board) || producesRequiredResourcesWithHelp(board, boughtResources) } /** @@ -47,15 +45,15 @@ data class Requirements @JvmOverloads constructor( * * @return true if the given player's board could meet these requirements */ - fun areMetBy(table: Table, playerIndex: Int): Boolean { - val board = table.getBoard(playerIndex) + fun areMetBy(player: Player): Boolean { + val board = player.board if (!hasRequiredGold(board)) { return false } if (producesRequiredResources(board)) { return true } - val bestPrice = bestPrice(resources, table, playerIndex) + val bestPrice = bestPrice(resources, player) return bestPrice != null && bestPrice <= board.gold - gold } @@ -78,8 +76,8 @@ data class Requirements @JvmOverloads constructor( return board.production.contains(remainingResources) } - fun pay(table: Table, playerIndex: Int, transactions: ResourceTransactions) { - table.getBoard(playerIndex).removeGold(gold) - transactions.execute(table, playerIndex) + fun pay(player: Player, transactions: ResourceTransactions) { + player.board.removeGold(gold) + transactions.execute(player) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.kt index defd896a..bcac2a9c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.Color @@ -19,15 +19,12 @@ data class BonusPerBoardElement ( val colors: List<Color>? = null // only relevant if type=CARD ) : Effect { - override fun apply(table: Table, playerIndex: Int) { - val goldGain = gold * nbMatchingElementsIn(table, playerIndex) - table.getBoard(playerIndex).addGold(goldGain) - } + override fun applyTo(player: Player) = player.board.addGold(gold * nbMatchingElementsFor(player)) - override fun computePoints(table: Table, playerIndex: Int): Int = points * nbMatchingElementsIn(table, playerIndex) + override fun computePoints(player: Player): Int = points * nbMatchingElementsFor(player) - private fun nbMatchingElementsIn(table: Table, playerIndex: Int): Int = boards - .map { pos -> table.getBoard(playerIndex, pos) } + private fun nbMatchingElementsFor(player: Player): Int = boards + .map(player::getBoard) .map(::nbMatchingElementsIn) .sum() diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Discount.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Discount.kt index b1455397..56e9befb 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Discount.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Discount.kt @@ -10,7 +10,7 @@ data class Discount( val discountedPrice: Int = 1 ) : InstantOwnBoardEffect() { - public override fun apply(board: Board) { + public override fun applyTo(board: Board) { val rules = board.tradingRules for (type in resourceTypes) { providers.forEach { rules.setCost(type, it, discountedPrice) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Effect.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Effect.kt index 3bf48086..c3b7b140 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Effect.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/Effect.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player /** * Represents an effect than can be applied to a player's board when playing a card or building his wonder. The effect @@ -9,7 +9,7 @@ import org.luxons.sevenwonders.game.api.Table */ interface Effect { - fun apply(table: Table, playerIndex: Int) + fun applyTo(player: Player) - fun computePoints(table: Table, playerIndex: Int): Int + fun computePoints(player: Player): Int } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/EndGameEffect.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/EndGameEffect.kt index 392f53b9..09b86ab7 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/EndGameEffect.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/EndGameEffect.kt @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player abstract class EndGameEffect : Effect { // EndGameEffects don't do anything when applied to the board, they simply give more points in the end - override fun apply(table: Table, playerIndex: Int) = Unit + override fun applyTo(player: Player) = Unit } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/GoldIncrease.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/GoldIncrease.kt index 0d206231..6f5e5b2a 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/GoldIncrease.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/GoldIncrease.kt @@ -4,5 +4,5 @@ import org.luxons.sevenwonders.game.boards.Board data class GoldIncrease(val amount: Int) : InstantOwnBoardEffect() { - public override fun apply(board: Board) = board.addGold(amount) + public override fun applyTo(board: Board) = board.addGold(amount) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.kt index fb26dcce..fb0d155c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.kt @@ -1,14 +1,14 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.boards.Board abstract class InstantOwnBoardEffect : Effect { - override fun apply(table: Table, playerIndex: Int) = apply(table.getBoard(playerIndex)) + override fun applyTo(player: Player) = applyTo(player.board) - protected abstract fun apply(board: Board) + protected abstract fun applyTo(board: Board) // InstantEffects are only important when applied to the board, they don't give extra points in the end - override fun computePoints(table: Table, playerIndex: Int): Int = 0 + override fun computePoints(player: Player): Int = 0 } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.kt index ad4d64eb..e01e9481 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.kt @@ -4,5 +4,5 @@ import org.luxons.sevenwonders.game.boards.Board data class MilitaryReinforcements(val count: Int) : InstantOwnBoardEffect() { - public override fun apply(board: Board) = board.military.addShields(count) + public override fun applyTo(board: Board) = board.military.addShields(count) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncrease.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncrease.kt index aa56705e..e8578eed 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncrease.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncrease.kt @@ -5,7 +5,7 @@ import org.luxons.sevenwonders.game.resources.Production data class ProductionIncrease(val production: Production, val isSellable: Boolean) : InstantOwnBoardEffect() { - public override fun apply(board: Board) { + public override fun applyTo(board: Board) { board.production.addAll(production) if (isSellable) { board.publicProduction.addAll(production) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncrease.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncrease.kt index ed6eb332..f075742e 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncrease.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncrease.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player data class RawPointsIncrease(val points: Int) : EndGameEffect() { - override fun computePoints(table: Table, playerIndex: Int): Int = points + override fun computePoints(player: Player): Int = points } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgress.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgress.kt index 850f8ec3..af4c0d53 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgress.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgress.kt @@ -5,5 +5,5 @@ import org.luxons.sevenwonders.game.boards.Science class ScienceProgress(val science: Science) : InstantOwnBoardEffect() { - public override fun apply(board: Board) = board.science.addAll(science) + public override fun applyTo(board: Board) = board.science.addAll(science) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbility.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbility.kt index 30d57e27..deddd780 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbility.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbility.kt @@ -1,8 +1,7 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import org.luxons.sevenwonders.game.boards.Board -import org.luxons.sevenwonders.game.cards.Card enum class SpecialAbility { @@ -27,14 +26,14 @@ enum class SpecialAbility { * her two neighboring cities. */ COPY_GUILD { - override fun computePoints(table: Table, playerIndex: Int): Int { - val copiedGuild = table.getBoard(playerIndex).copiedGuild + override fun computePoints(player: Player): Int { + val copiedGuild = player.board.copiedGuild ?: throw IllegalStateException("The copied Guild has not been chosen, cannot compute points") - return copiedGuild.effects.stream().mapToInt { e -> e.computePoints(table, playerIndex) }.sum() + return copiedGuild.effects.stream().mapToInt { it.computePoints(player) }.sum() } }; fun apply(board: Board) = board.addSpecial(this) - open fun computePoints(table: Table, playerIndex: Int): Int = 0 + open fun computePoints(player: Player): Int = 0 } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivation.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivation.kt index fd83d7fe..136beab1 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivation.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivation.kt @@ -1,10 +1,10 @@ package org.luxons.sevenwonders.game.effects -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player data class SpecialAbilityActivation(val specialAbility: SpecialAbility) : Effect { - override fun apply(table: Table, playerIndex: Int) = specialAbility.apply(table.getBoard(playerIndex)) + override fun applyTo(player: Player) = specialAbility.apply(player.board) - override fun computePoints(table: Table, playerIndex: Int): Int = specialAbility.computePoints(table, playerIndex) + override fun computePoints(player: Player): Int = specialAbility.computePoints(player) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt index bbd9ebab..c4508401 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt @@ -1,25 +1,23 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card -class BuildWonderMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - CardFromHandMove(playerIndex, card, move) { +internal class BuildWonderMove(move: PlayerMove, card: Card, player: PlayerContext) : + CardFromHandMove(move, card, player) { - @Throws(InvalidMoveException::class) - override fun validate(table: Table, playerHand: List<Card>) { - super.validate(table, playerHand) - val board = table.getBoard(playerIndex) - if (!board.wonder.isNextStageBuildable(table, playerIndex, transactions)) { - throw InvalidMoveException("Player $playerIndex cannot upgrade his wonder with the given resources") + private val wonder = player.board.wonder + + init { + if (!wonder.isNextStageBuildable(playerContext.board, transactions)) { + throw InvalidMoveException(this, "all levels are already built, or the given resources are insufficient") } } - override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) = - table.getBoard(playerIndex).wonder.buildLevel(card.back) + override fun place(discardedCards: MutableList<Card>, settings: Settings) = wonder.placeCard(card.back) - override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) = - table.getBoard(playerIndex).wonder.activateLastBuiltStage(table, playerIndex, transactions) + override fun activate(discardedCards: List<Card>, settings: Settings) = + wonder.activateLastBuiltStage(playerContext, transactions) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt index 7b084d43..17d612af 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt @@ -1,16 +1,15 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card -abstract class CardFromHandMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - Move(playerIndex, card, move) { +internal abstract class CardFromHandMove(move: PlayerMove, card: Card, player: PlayerContext) : + Move(move, card, player) { - @Throws(InvalidMoveException::class) - override fun validate(table: Table, playerHand: List<Card>) { - if (!playerHand.contains(card)) { - throw InvalidMoveException("Player $playerIndex does not have the card '${card.name}' in his hand") + init { + if (!player.hand.contains(card)) { + throw InvalidMoveException(this, "card '${card.name}' not in hand") } } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt index 5a017e53..3a7fe792 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt @@ -1,41 +1,37 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.Color import org.luxons.sevenwonders.game.effects.SpecialAbility -class CopyGuildMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - Move(playerIndex, card, move) { +internal class CopyGuildMove(move: PlayerMove, card: Card, player: PlayerContext) : Move(move, card, player) { - @Throws(InvalidMoveException::class) - override fun validate(table: Table, playerHand: List<Card>) { - val board = table.getBoard(playerIndex) + init { + val board = player.board if (!board.hasSpecial(SpecialAbility.COPY_GUILD)) { - throw InvalidMoveException("Player $playerIndex does not have the ability to copy guild cards") + throw InvalidMoveException(this, "no ability to copy guild cards") } if (card.color !== Color.PURPLE) { - throw InvalidMoveException("Player $playerIndex cannot copy card ${card.name} because it is not a guild card") + throw InvalidMoveException(this, "card '${card.name}' is not a guild card") } - val leftNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.LEFT) - val rightNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.RIGHT) + val leftNeighbourHasIt = neighbourHasTheCard(RelativeBoardPosition.LEFT) + val rightNeighbourHasIt = neighbourHasTheCard(RelativeBoardPosition.RIGHT) if (!leftNeighbourHasIt && !rightNeighbourHasIt) { - throw InvalidMoveException("Player $playerIndex cannot copy card ${card.name} because none of his neighbour has it") + throw InvalidMoveException(this, "neighbours don't have card '${card.name}'") } } - private fun neighbourHasTheCard(table: Table, position: RelativeBoardPosition): Boolean { - val neighbourBoard = table.getBoard(playerIndex, position) - return neighbourBoard.getPlayedCards().contains(card) - } + private fun neighbourHasTheCard(position: RelativeBoardPosition): Boolean = + playerContext.getBoard(position).getPlayedCards().contains(card) // nothing special to do here - override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) = Unit + override fun place(discardedCards: MutableList<Card>, settings: Settings) = Unit - override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) { - table.getBoard(playerIndex).copiedGuild = card + override fun activate(discardedCards: List<Card>, settings: Settings) { + playerContext.board.copiedGuild = card } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt index cf1b69ba..a8cd42a6 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt @@ -1,20 +1,18 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table -import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.cards.Card -class DiscardMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - CardFromHandMove(playerIndex, card, move) { +internal class DiscardMove(move: PlayerMove, card: Card, player: PlayerContext) : + CardFromHandMove(move, card, player) { - override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) { + override fun place(discardedCards: MutableList<Card>, settings: Settings) { discardedCards.add(card) } - override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) { - val board = table.getBoard(playerIndex) - board.addGold(settings.discardedCardGold) + override fun activate(discardedCards: List<Card>, settings: Settings) { + playerContext.board.addGold(settings.discardedCardGold) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/InvalidMoveException.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/InvalidMoveException.kt deleted file mode 100644 index d816355d..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/InvalidMoveException.kt +++ /dev/null @@ -1,3 +0,0 @@ -package org.luxons.sevenwonders.game.moves - -class InvalidMoveException(message: String) : IllegalArgumentException(message) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt index 885f267f..86b26ca9 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt @@ -1,21 +1,25 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.resources.ResourceTransactions -abstract class Move internal constructor(val playerIndex: Int, val card: Card, move: PlayerMove) { - +abstract class Move internal constructor( + move: PlayerMove, + val card: Card, + internal val playerContext: PlayerContext +) { val type: MoveType = move.type val transactions: ResourceTransactions = ResourceTransactions(move.transactions) - @Throws(InvalidMoveException::class) - abstract fun validate(table: Table, playerHand: List<Card>) - - abstract fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) + abstract fun place(discardedCards: MutableList<Card>, settings: Settings) - abstract fun activate(table: Table, discardedCards: List<Card>, settings: Settings) + abstract fun activate(discardedCards: List<Card>, settings: Settings) } + +class InvalidMoveException internal constructor(move: Move, message: String) : IllegalArgumentException( + "Player ${move.playerContext.index} cannot perform move ${move.type}: $message" +) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt index ced0f3a1..fc859931 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt @@ -1,24 +1,17 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.api.PlayerMove import org.luxons.sevenwonders.game.cards.Card -enum class MoveType { - PLAY { - override fun resolve(playerIndex: Int, card: Card, move: PlayerMove) = PlayCardMove(playerIndex, card, move) - }, - PLAY_FREE { - override fun resolve(playerIndex: Int, card: Card, move: PlayerMove) = PlayFreeCardMove(playerIndex, card, move) - }, - UPGRADE_WONDER { - override fun resolve(playerIndex: Int, card: Card, move: PlayerMove) = BuildWonderMove(playerIndex, card, move) - }, - DISCARD { - override fun resolve(playerIndex: Int, card: Card, move: PlayerMove) = DiscardMove(playerIndex, card, move) - }, - COPY_GUILD { - override fun resolve(playerIndex: Int, card: Card, move: PlayerMove) = CopyGuildMove(playerIndex, card, move) - }; +enum class MoveType(val create: (move: PlayerMove, card: Card, context: PlayerContext) -> Move) { + PLAY(::PlayCardMove), + PLAY_FREE(::PlayFreeCardMove), + UPGRADE_WONDER(::BuildWonderMove), + DISCARD(::DiscardMove), + COPY_GUILD(::CopyGuildMove); - abstract fun resolve(playerIndex: Int, card: Card, move: PlayerMove): Move + internal fun resolve(move: PlayerMove, card: Card, context: PlayerContext): Move { + return create(move, card, context) + } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt index b87916b1..f0c18bc7 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt @@ -1,25 +1,21 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card -class PlayCardMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - CardFromHandMove(playerIndex, card, move) { +internal class PlayCardMove(move: PlayerMove, card: Card, player: PlayerContext) : + CardFromHandMove(move, card, player) { - @Throws(InvalidMoveException::class) - override fun validate(table: Table, playerHand: List<Card>) { - super.validate(table, playerHand) - val board = table.getBoard(playerIndex) + init { + val board = player.board if (!card.isChainableOn(board) && !card.requirements.areMetWithHelpBy(board, transactions)) { - throw InvalidMoveException("Player $playerIndex cannot play the card ${card.name} with the given resources") + throw InvalidMoveException(this, "requirements not met to play the card ${card.name}") } } - override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) = - table.getBoard(playerIndex).addCard(card) + override fun place(discardedCards: MutableList<Card>, settings: Settings) = playerContext.board.addCard(card) - override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) = - card.applyTo(table, playerIndex, transactions) + override fun activate(discardedCards: List<Card>, settings: Settings) = card.applyTo(playerContext, transactions) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt index 125b34a6..bac185d9 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt @@ -1,34 +1,25 @@ package org.luxons.sevenwonders.game.moves +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.PlayerMove -import org.luxons.sevenwonders.game.api.Table -import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.cards.Card -class PlayFreeCardMove internal constructor(playerIndex: Int, card: Card, move: PlayerMove) : - CardFromHandMove(playerIndex, card, move) { +internal class PlayFreeCardMove(move: PlayerMove, card: Card, playerContext: PlayerContext) : + CardFromHandMove(move, card, playerContext) { - @Throws(InvalidMoveException::class) - override fun validate(table: Table, playerHand: List<Card>) { - super.validate(table, playerHand) - val board = table.getBoard(playerIndex) - if (!board.canPlayFreeCard(table.currentAge)) { - throw InvalidMoveException( - String.format("Player %d cannot play the card %s for free", playerIndex, card.name) - ) + init { + val board = playerContext.board + if (!board.canPlayFreeCard(playerContext.currentAge)) { + throw InvalidMoveException(this, "no free card available for the current age ${playerContext.currentAge}") } } - override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) { - val board = table.getBoard(playerIndex) - board.addCard(card) - } + override fun place(discardedCards: MutableList<Card>, settings: Settings) = playerContext.board.addCard(card) - override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) { + override fun activate(discardedCards: List<Card>, settings: Settings) { // only apply effects, without paying the cost - card.effects.forEach { e -> e.apply(table, playerIndex) } - val board = table.getBoard(playerIndex) - board.consumeFreeCard(table.currentAge) + card.effects.forEach { it.applyTo(playerContext) } + playerContext.board.consumeFreeCard(playerContext.currentAge) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt index 6e7d76f6..24149e59 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt @@ -1,19 +1,19 @@ package org.luxons.sevenwonders.game.resources -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player import java.util.ArrayList import java.util.EnumSet -fun bestPrice(resources: Resources, table: Table, playerIndex: Int): Int? { - return bestSolution(resources, table, playerIndex)?.price +fun bestPrice(resources: Resources, player: Player): Int? { + return bestSolution(resources, player)?.price } -fun bestTransaction(resources: Resources, table: Table, playerIndex: Int): ResourceTransactions? { - return bestSolution(resources, table, playerIndex)?.transactions +fun bestTransaction(resources: Resources, player: Player): ResourceTransactions? { + return bestSolution(resources, player)?.transactions } -fun bestSolution(resources: Resources, table: Table, playerIndex: Int): TransactionPlan? { - val calculator = BestPriceCalculator(resources, table, playerIndex) +fun bestSolution(resources: Resources, player: Player): TransactionPlan? { + val calculator = BestPriceCalculator(resources, player) return calculator.computeBestSolution() } @@ -27,7 +27,7 @@ private class ResourcePool( fun getCost(type: ResourceType): Int = if (provider == null) 0 else rules.getCost(type, provider) } -private class BestPriceCalculator(resourcesToPay: Resources, table: Table, playerIndex: Int) { +private class BestPriceCalculator(resourcesToPay: Resources, player: Player) { private val pools: List<ResourcePool> private val resourcesLeftToPay: Resources @@ -38,15 +38,15 @@ private class BestPriceCalculator(resourcesToPay: Resources, table: Table, playe var bestPrice: Int = Integer.MAX_VALUE init { - val board = table.getBoard(playerIndex) + val board = player.board this.resourcesLeftToPay = resourcesToPay.minus(board.production.fixedResources) - this.pools = createResourcePools(table, playerIndex) + this.pools = createResourcePools(player) } - private fun createResourcePools(table: Table, playerIndex: Int): List<ResourcePool> { + private fun createResourcePools(player: Player): List<ResourcePool> { val providers = Provider.values() - val board = table.getBoard(playerIndex) + val board = player.board val rules = board.tradingRules val pools = ArrayList<ResourcePool>(providers.size + 1) @@ -55,7 +55,7 @@ private class BestPriceCalculator(resourcesToPay: Resources, table: Table, playe pools.add(ResourcePool(null, rules, ownBoardChoices.map { it.toMutableSet() }.toSet())) for (provider in providers) { - val providerBoard = table.getBoard(playerIndex, provider.boardPosition) + val providerBoard = player.getBoard(provider.boardPosition) val pool = ResourcePool(provider, rules, providerBoard.publicProduction.asChoices().map { it.toMutableSet() }.toSet()) pools.add(pool) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransaction.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransaction.kt index c500f0d0..30f4d014 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransaction.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransaction.kt @@ -1,15 +1,15 @@ package org.luxons.sevenwonders.game.resources -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player data class ResourceTransaction(val provider: Provider, val resources: Resources) { - internal fun execute(table: Table, playerIndex: Int) { - val board = table.getBoard(playerIndex) + internal fun execute(player: Player) { + val board = player.board val price = board.tradingRules.computeCost(this) board.removeGold(price) val providerPosition = provider.boardPosition - val providerBoard = table.getBoard(playerIndex, providerPosition) + val providerBoard = player.getBoard(providerPosition) providerBoard.addGold(price) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt index 24556d19..1f92e764 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.resources -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player data class ResourceTransactions(private val resourcesByProvider: MutableMap<Provider, Resources> = mutableMapOf()) { @@ -13,7 +13,7 @@ data class ResourceTransactions(private val resourcesByProvider: MutableMap<Prov } fun remove(provider: Provider, resources: Resources) { - resourcesByProvider.compute(provider) { p, prevResources -> + resourcesByProvider.compute(provider) { _, prevResources -> if (prevResources == null) { throw IllegalStateException("Cannot remove resources from resource transactions") } @@ -21,8 +21,8 @@ data class ResourceTransactions(private val resourcesByProvider: MutableMap<Prov } } - fun execute(table: Table, playerIndex: Int) { - asList().forEach { t -> t.execute(table, playerIndex) } + fun execute(player: Player) { + asList().forEach { it.execute(player) } } fun asList(): List<ResourceTransaction> { diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt index beb87580..e7b42eca 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt @@ -1,6 +1,7 @@ package org.luxons.sevenwonders.game.wonders -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player +import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.cards.CardBack import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.ResourceType @@ -29,17 +30,17 @@ class Wonder( return stages[lastLevel] } - fun isNextStageBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean = - nbBuiltStages < stages.size && nextStage.isBuildable(table, playerIndex, boughtResources) + fun isNextStageBuildable(board: Board, boughtResources: ResourceTransactions): Boolean = + nbBuiltStages < stages.size && nextStage.isBuildable(board, boughtResources) - fun buildLevel(cardBack: CardBack) = nextStage.build(cardBack) + fun placeCard(cardBack: CardBack) = nextStage.placeCard(cardBack) - fun activateLastBuiltStage(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) = - lastBuiltStage.activate(table, playerIndex, boughtResources) + fun activateLastBuiltStage(player: Player, boughtResources: ResourceTransactions) = + lastBuiltStage.activate(player, boughtResources) - fun computePoints(table: Table, playerIndex: Int): Int = stages - .filter { it.isBuilt } - .flatMap { c -> c.effects } - .map { e -> e.computePoints(table, playerIndex) } - .sum() + fun computePoints(player: Player): Int = + stages.filter { it.isBuilt } + .flatMap { it.effects } + .map { it.computePoints(player) } + .sum() } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt index 6aad135d..04a6de0c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt @@ -1,6 +1,7 @@ package org.luxons.sevenwonders.game.wonders -import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.Player +import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.cards.CardBack import org.luxons.sevenwonders.game.cards.Requirements import org.luxons.sevenwonders.game.effects.Effect @@ -14,17 +15,16 @@ class WonderStage(val requirements: Requirements, val effects: List<Effect>) { val isBuilt: Boolean get() = cardBack != null - fun isBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean { - val board = table.getBoard(playerIndex) + fun isBuildable(board: Board, boughtResources: ResourceTransactions): Boolean { return requirements.areMetWithHelpBy(board, boughtResources) } - internal fun build(cardBack: CardBack) { + internal fun placeCard(cardBack: CardBack) { this.cardBack = cardBack } - internal fun activate(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) { - effects.forEach { e -> e.apply(table, playerIndex) } - requirements.pay(table, playerIndex, boughtResources) + internal fun activate(player: Player, boughtResources: ResourceTransactions) { + effects.forEach { it.applyTo(player) } + requirements.pay(player, boughtResources) } } 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 6ec279b6..5f76b42e 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 @@ -85,7 +85,7 @@ class GameTest { val requiredResources = handCard.card.requirements.resources val table = turnInfo.table val playerIndex = turnInfo.playerIndex - val transactions = bestTransaction(requiredResources, table, playerIndex) + val transactions = bestTransaction(requiredResources, PlayerContext(playerIndex, table, listOf())) // we're supposed to have a best transaction plan because the card is playable return transactions!!.asList() } @@ -100,11 +100,10 @@ class GameTest { private fun checkLastPlayedMoves(sentMoves: Map<Int, PlayerMove>, table: Table) { for (move in table.lastPlayedMoves) { - val sentMove = sentMoves[move.playerIndex] + val sentMove = sentMoves[move.playerContext.index] assertNotNull(sentMove) assertNotNull(move.card) assertEquals(sentMove!!.cardName, move.card.name) - assertSame(sentMove.type, move.type) } } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt index 7c23bbd0..68799145 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt @@ -12,7 +12,6 @@ import org.junit.experimental.theories.Theory import org.junit.rules.ExpectedException import org.junit.runner.RunWith import org.luxons.sevenwonders.game.Settings -import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board.InsufficientFundsException import org.luxons.sevenwonders.game.cards.Color import org.luxons.sevenwonders.game.effects.RawPointsIncrease @@ -24,6 +23,7 @@ import org.luxons.sevenwonders.game.test.addCards import org.luxons.sevenwonders.game.test.createResources import org.luxons.sevenwonders.game.test.getDifferentColorFrom import org.luxons.sevenwonders.game.test.playCardWithEffect +import org.luxons.sevenwonders.game.test.singleBoardPlayer import org.luxons.sevenwonders.game.test.testBoard import org.luxons.sevenwonders.game.test.testCard import org.luxons.sevenwonders.game.test.testCustomizableSettings @@ -128,10 +128,9 @@ class BoardTest { @Theory fun hasSpecial(applied: SpecialAbility, tested: SpecialAbility) { val board = testBoard(ResourceType.CLAY) - val table = Table(listOf(board)) val special = SpecialAbilityActivation(applied) - special.apply(table, 0) + special.applyTo(singleBoardPlayer(board)) assertEquals(applied === tested, board.hasSpecial(tested)) } @@ -139,10 +138,9 @@ class BoardTest { @Test fun canPlayFreeCard() { val board = testBoard(ResourceType.CLAY) - val table = Table(listOf(board)) val special = SpecialAbilityActivation(SpecialAbility.ONE_FREE_PER_AGE) - special.apply(table, 0) + special.applyTo(singleBoardPlayer(board)) assertTrue(board.canPlayFreeCard(0)) assertTrue(board.canPlayFreeCard(1)) @@ -171,10 +169,9 @@ class BoardTest { fun computePoints_gold(@FromDataPoints("gold") gold: Int) { assumeTrue(gold >= 0) val board = testBoard(ResourceType.WOOD) - val table = Table(listOf(board)) board.gold = gold - val score = board.computePoints(table) + val score = board.computeScore(singleBoardPlayer(board)) assertEquals(gold / 3, score.pointsByCategory[ScoreCategory.GOLD]) assertEquals(gold / 3, score.totalPoints) } @@ -183,48 +180,38 @@ class BoardTest { fun computePoints_(@FromDataPoints("gold") gold: Int) { assumeTrue(gold >= 0) val board = testBoard(ResourceType.WOOD) - val table = Table(listOf(board)) board.gold = gold val effect = RawPointsIncrease(5) - playCardWithEffect(table, 0, Color.BLUE, effect) + playCardWithEffect(singleBoardPlayer(board), Color.BLUE, effect) - val score = board.computePoints(table) + val score = board.computeScore(singleBoardPlayer(board)) assertEquals(gold / 3, score.pointsByCategory[ScoreCategory.GOLD]) assertEquals(5, score.pointsByCategory[ScoreCategory.CIVIL]) assertEquals(5 + gold / 3, score.totalPoints) } + companion object { @JvmStatic @DataPoints("gold") - fun goldAmounts(): IntArray { - return intArrayOf(-3, -1, 0, 1, 2, 3) - } + fun goldAmounts(): IntArray = intArrayOf(-3, -1, 0, 1, 2, 3) @JvmStatic @DataPoints("nbCards") - fun nbCards(): IntArray { - return intArrayOf(0, 1, 2) - } + fun nbCards(): IntArray = intArrayOf(0, 1, 2) @JvmStatic @DataPoints - fun resourceTypes(): Array<ResourceType> { - return ResourceType.values() - } + fun resourceTypes(): Array<ResourceType> = ResourceType.values() @JvmStatic @DataPoints - fun colors(): Array<Color> { - return Color.values() - } + fun colors(): Array<Color> = Color.values() @JvmStatic @DataPoints - fun specialAbilities(): Array<SpecialAbility> { - return SpecialAbility.values() - } + fun specialAbilities(): Array<SpecialAbility> = SpecialAbility.values() } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt index 783e4c38..e9f95b55 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.game.cards -import org.junit.Assert.* -import org.junit.Before +import org.junit.Assert.assertEquals import org.junit.Test import org.luxons.sevenwonders.game.Settings +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.effects.ProductionIncrease @@ -15,12 +15,8 @@ import org.luxons.sevenwonders.game.wonders.Wonder class CardTest { - private var table: Table? = null - - private var treeFarmCard: Card? = null - - @Before - fun initBoard() { + @Test + fun playCardCostingMoney() { val settings = Settings(3) val boards = listOf( @@ -28,24 +24,21 @@ class CardTest { Board(Wonder("TestWonder", ResourceType.STONE, emptyList(), ""), 1, settings), Board(Wonder("TestWonder", ResourceType.PAPYRUS, emptyList(), ""), 2, settings) ) - table = Table(boards) + val table = Table(boards) val treeFarmRequirements = Requirements(1) val treeFarmProduction = Production() treeFarmProduction.addChoice(ResourceType.WOOD, ResourceType.CLAY) val treeFarmEffect = ProductionIncrease(treeFarmProduction, false) - treeFarmCard = testCard("Tree Farm", Color.BROWN, treeFarmEffect, treeFarmRequirements) - } + val treeFarmCard = testCard("Tree Farm", Color.BROWN, treeFarmEffect, treeFarmRequirements) - @Test - fun playCardCostingMoney() { - table!!.getBoard(0).gold = 3 - table!!.getBoard(1).gold = 3 - table!!.getBoard(2).gold = 3 - treeFarmCard!!.applyTo(table!!, 0, ResourceTransactions()) - assertEquals(2, table!!.getBoard(0).gold.toLong()) - assertEquals(3, table!!.getBoard(1).gold.toLong()) - assertEquals(3, table!!.getBoard(2).gold.toLong()) + table.getBoard(0).gold = 3 + table.getBoard(1).gold = 3 + table.getBoard(2).gold = 3 + treeFarmCard.applyTo(SimplePlayer(0, table), ResourceTransactions()) + assertEquals(2, table.getBoard(0).gold.toLong()) + assertEquals(3, table.getBoard(1).gold.toLong()) + assertEquals(3, table.getBoard(2).gold.toLong()) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt index 7ba21089..e7c3835a 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt @@ -8,6 +8,7 @@ import org.junit.experimental.theories.FromDataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.test.sampleCards import org.luxons.sevenwonders.game.test.testTable @@ -97,7 +98,7 @@ class HandsTest { val hands = Hands(listOf(hand0, hand1)) val table = testTable(2) - val hand = hands.createHand(table, 0) + val hand = hands.createHand(SimplePlayer(0, table)) for (handCard in hand) { assertTrue(hand0.contains(handCard.card)) 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 196091a6..95a5e174 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 @@ -1,26 +1,23 @@ package org.luxons.sevenwonders.game.cards -import java.util.Arrays -import java.util.Collections - +import org.junit.Assert.* +import org.junit.Assume.assumeTrue import org.junit.Test import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.api.Table -import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.resources.Provider import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.ResourceType import org.luxons.sevenwonders.game.resources.Resources -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertSame -import org.junit.Assert.assertTrue -import org.junit.Assume.assumeTrue +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 @RunWith(Theories::class) class RequirementsTest { @@ -43,11 +40,11 @@ class RequirementsTest { val requirements = Requirements(requiredGold) val board = testBoard(ResourceType.CLAY, boardGold) - val table = Table(listOf(board)) + val player = singleBoardPlayer(board) assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board)) assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, ResourceTransactions())) - assertEquals(boardGold >= requiredGold, requirements.areMetBy(table, 0)) + assertEquals(boardGold >= requiredGold, requirements.areMetBy(player)) } @Theory @@ -55,7 +52,7 @@ class RequirementsTest { val requirements = createRequirements(requiredResource) val board = testBoard(initialResource, 0) - val table = Table(listOf(board)) + val player = singleBoardPlayer(board) assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) assertEquals( @@ -64,7 +61,7 @@ class RequirementsTest { ) if (initialResource == requiredResource) { - assertTrue(requirements.areMetBy(table, 0)) + assertTrue(requirements.areMetBy(player)) } } @@ -79,7 +76,7 @@ class RequirementsTest { val board = testBoard(initialResource, 0) board.production.addFixedResource(producedResource, 1) - val table = Table(listOf(board)) + val player = singleBoardPlayer(board) assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) assertEquals( @@ -88,7 +85,7 @@ class RequirementsTest { ) if (producedResource == requiredResource) { - assertTrue(requirements.areMetBy(table, 0)) + assertTrue(requirements.areMetBy(player)) } } @@ -105,6 +102,7 @@ class RequirementsTest { val neighbourBoard = testBoard(initialResource, 0) neighbourBoard.publicProduction.addFixedResource(boughtResource, 1) val table = Table(Arrays.asList(board, neighbourBoard)) + val player = SimplePlayer(0, table) val resources = createTransactions(Provider.RIGHT_PLAYER, boughtResource) @@ -112,7 +110,7 @@ class RequirementsTest { assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources)) if (boughtResource == requiredResource) { - assertTrue(requirements.areMetBy(table, 0)) + assertTrue(requirements.areMetBy(player)) } } @@ -125,6 +123,7 @@ class RequirementsTest { val board = testBoard(initialResource, 2) val neighbourBoard = testBoard(requiredResource, 0) val table = Table(Arrays.asList(board, neighbourBoard)) + val player = SimplePlayer(0, table) val transactions = createTransactions( Provider.RIGHT_PLAYER, @@ -133,9 +132,9 @@ class RequirementsTest { assertFalse(requirements.areMetWithoutNeighboursBy(board)) assertTrue(requirements.areMetWithHelpBy(board, transactions)) - assertTrue(requirements.areMetBy(table, 0)) + assertTrue(requirements.areMetBy(player)) - requirements.pay(table, 0, transactions) + requirements.pay(player, transactions) assertEquals(0, board.gold.toLong()) assertEquals(2, neighbourBoard.gold.toLong()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt index 3f0b0aa0..8899eb06 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt @@ -1,45 +1,47 @@ package org.luxons.sevenwonders.game.effects +import org.junit.Assert.assertEquals import org.junit.Before import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.Player +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.CardBack import org.luxons.sevenwonders.game.cards.Color -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals +import org.luxons.sevenwonders.game.test.addCards +import org.luxons.sevenwonders.game.test.testTable @RunWith(Theories::class) class BonusPerBoardElementTest { private var table: Table? = null + private var player0: Player? = null @Before fun setUp() { table = testTable(4) + player0 = SimplePlayer(0, table!!) } @Theory fun computePoints_countsCards( - boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, - points: Int, gold: Int, color: Color + boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, points: Int, gold: Int, color: Color ) { val board = table!!.getBoard(0, boardPosition) addCards(board, nbCards, nbOtherCards, color) val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.CARD, gold, points, listOf(color)) - assertEquals((nbCards * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + assertEquals((nbCards * points).toLong(), bonus.computePoints(player0!!).toLong()) } @Theory fun computePoints_countsDefeatTokens( - boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, - gold: Int + boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, gold: Int ) { val board = table!!.getBoard(0, boardPosition) for (i in 0 until nbDefeatTokens) { @@ -48,29 +50,25 @@ class BonusPerBoardElementTest { val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.DEFEAT_TOKEN, gold, points, listOf()) - assertEquals((nbDefeatTokens * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + assertEquals((nbDefeatTokens * points).toLong(), bonus.computePoints(player0!!).toLong()) } @Theory - fun computePoints_countsWonderStages( - boardPosition: RelativeBoardPosition, nbStages: Int, points: Int, - gold: Int - ) { + fun computePoints_countsWonderStages(boardPosition: RelativeBoardPosition, nbStages: Int, points: Int, gold: Int) { val board = table!!.getBoard(0, boardPosition) for (i in 0 until nbStages) { - board.wonder.buildLevel(CardBack("")) + board.wonder.placeCard(CardBack("")) } val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.BUILT_WONDER_STAGES, gold, points, listOf()) - assertEquals((nbStages * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + assertEquals((nbStages * points).toLong(), bonus.computePoints(player0!!).toLong()) } @Theory fun apply_countsCards( - boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, points: Int, - gold: Int, color: Color + boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, points: Int, gold: Int, color: Color ) { val board = table!!.getBoard(0, boardPosition) addCards(board, nbCards, nbOtherCards, color) @@ -79,14 +77,13 @@ class BonusPerBoardElementTest { val selfBoard = table!!.getBoard(0) val initialGold = selfBoard.gold - bonus.apply(table!!, 0) + bonus.applyTo(player0!!) assertEquals((initialGold + nbCards * gold).toLong(), selfBoard.gold.toLong()) } @Theory fun apply_countsDefeatTokens( - boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, - gold: Int + boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, gold: Int ) { val board = table!!.getBoard(0, boardPosition) for (i in 0 until nbDefeatTokens) { @@ -97,7 +94,7 @@ class BonusPerBoardElementTest { val selfBoard = table!!.getBoard(0) val initialGold = selfBoard.gold - bonus.apply(table!!, 0) + bonus.applyTo(player0!!) assertEquals((initialGold + nbDefeatTokens * gold).toLong(), selfBoard.gold.toLong()) } @@ -105,7 +102,7 @@ class BonusPerBoardElementTest { fun apply_countsWonderStages(boardPosition: RelativeBoardPosition, nbStages: Int, points: Int, gold: Int) { val board = table!!.getBoard(0, boardPosition) for (i in 0 until nbStages) { - board.wonder.buildLevel(CardBack("")) + board.wonder.placeCard(CardBack("")) } val bonus = @@ -113,7 +110,7 @@ class BonusPerBoardElementTest { val selfBoard = table!!.getBoard(0) val initialGold = selfBoard.gold - bonus.apply(table!!, 0) + bonus.applyTo(player0!!) assertEquals((initialGold + nbStages * gold).toLong(), selfBoard.gold.toLong()) } @@ -121,20 +118,14 @@ class BonusPerBoardElementTest { @JvmStatic @DataPoints - fun values(): IntArray { - return intArrayOf(0, 1, 2, 3) - } + fun values(): IntArray = intArrayOf(0, 1, 2, 3) @JvmStatic @DataPoints - fun colors(): Array<Color> { - return Color.values() - } + fun colors(): Array<Color> = Color.values() @JvmStatic @DataPoints - fun positions(): Array<RelativeBoardPosition> { - return RelativeBoardPosition.values() - } + fun positions(): Array<RelativeBoardPosition> = RelativeBoardPosition.values() } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt index e228d585..2cd54b2e 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt @@ -18,7 +18,7 @@ class DiscountTest { fun apply_givesDiscountedPrice(discountedPrice: Int, discountedType: ResourceType, provider: Provider) { val board = testBoard(ResourceType.CLAY, 3) val discount = Discount(listOf(discountedType), listOf(provider), discountedPrice) - discount.apply(board) + discount.applyTo(board) val transactions = createTransactions(provider, discountedType) assertEquals(discountedPrice.toLong(), board.tradingRules.computeCost(transactions).toLong()) @@ -34,7 +34,7 @@ class DiscountTest { val board = testBoard(ResourceType.CLAY, 3) val discount = Discount(listOf(discountedType), listOf(provider), discountedPrice) - discount.apply(board) + discount.applyTo(board) // this is the default in the settings used by TestUtilsKt.testBoard() val normalPrice = 2 @@ -53,20 +53,14 @@ class DiscountTest { @JvmStatic @DataPoints - fun discountedPrices(): IntArray { - return intArrayOf(0, 1, 2) - } + fun discountedPrices(): IntArray = intArrayOf(0, 1, 2) @JvmStatic @DataPoints - fun resourceTypes(): Array<ResourceType> { - return ResourceType.values() - } + fun resourceTypes(): Array<ResourceType> = ResourceType.values() @JvmStatic @DataPoints - fun providers(): Array<Provider> { - return Provider.values() - } + fun providers(): Array<Provider> = Provider.values() } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt index 175f15ea..897dcf30 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt @@ -1,13 +1,14 @@ package org.luxons.sevenwonders.game.effects +import org.junit.Assert.assertEquals import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals +import org.luxons.sevenwonders.game.test.testBoard +import org.luxons.sevenwonders.game.test.testTable @RunWith(Theories::class) class GoldIncreaseTest { @@ -17,7 +18,7 @@ class GoldIncreaseTest { val board = testBoard(type, initialAmount) val goldIncrease = GoldIncrease(goldIncreaseAmount) - goldIncrease.apply(board) + goldIncrease.applyTo(board) assertEquals((initialAmount + goldIncreaseAmount).toLong(), board.gold.toLong()) } @@ -25,8 +26,8 @@ class GoldIncreaseTest { @Theory fun computePoints_isAlwaysZero(gold: Int) { val goldIncrease = GoldIncrease(gold) - val table = testTable(5) - assertEquals(0, goldIncrease.computePoints(table, 0).toLong()) + val player = SimplePlayer(0, testTable(5)) + assertEquals(0, goldIncrease.computePoints(player).toLong()) } companion object { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt index a8bd01d6..fce3d2bd 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt @@ -1,13 +1,14 @@ package org.luxons.sevenwonders.game.effects +import org.junit.Assert.assertEquals import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals +import org.luxons.sevenwonders.game.test.testBoard +import org.luxons.sevenwonders.game.test.testTable @RunWith(Theories::class) class MilitaryReinforcementsTest { @@ -18,7 +19,7 @@ class MilitaryReinforcementsTest { board.military.addShields(initialShields) val reinforcements = MilitaryReinforcements(additionalShields) - reinforcements.apply(board) + reinforcements.applyTo(board) assertEquals((initialShields + additionalShields).toLong(), board.military.nbShields.toLong()) } @@ -26,22 +27,18 @@ class MilitaryReinforcementsTest { @Theory fun computePoints_isAlwaysZero(shields: Int) { val reinforcements = MilitaryReinforcements(shields) - val table = testTable(5) - assertEquals(0, reinforcements.computePoints(table, 0).toLong()) + val player = SimplePlayer(0, testTable(5)) + assertEquals(0, reinforcements.computePoints(player).toLong()) } companion object { @JvmStatic @DataPoints - fun shieldCounts(): IntArray { - return intArrayOf(0, 1, 2, 3, 5) - } + fun shieldCounts(): IntArray = intArrayOf(0, 1, 2, 3, 5) @JvmStatic @DataPoints - fun resourceTypes(): Array<ResourceType> { - return ResourceType.values() - } + fun resourceTypes(): Array<ResourceType> = ResourceType.values() } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt index 6f8da55d..9d5be34f 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt @@ -1,15 +1,16 @@ package org.luxons.sevenwonders.game.effects +import org.junit.Assert.* import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.test.* - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue +import org.luxons.sevenwonders.game.test.createResources +import org.luxons.sevenwonders.game.test.fixedProduction +import org.luxons.sevenwonders.game.test.testBoard +import org.luxons.sevenwonders.game.test.testTable @RunWith(Theories::class) class ProductionIncreaseTest { @@ -23,7 +24,7 @@ class ProductionIncreaseTest { val board = testBoard(initialType) val effect = ProductionIncrease(fixedProduction(addedType), false) - effect.apply(board) + effect.applyTo(board) val resources = createResources(initialType, addedType) assertTrue(board.production.contains(resources)) @@ -43,7 +44,7 @@ class ProductionIncreaseTest { val board = testBoard(initialType) val effect = ProductionIncrease(fixedProduction(addedType), true) - effect.apply(board) + effect.applyTo(board) val resources = createResources(initialType, addedType) assertTrue(board.production.contains(resources)) @@ -57,8 +58,8 @@ class ProductionIncreaseTest { @Theory fun computePoints_isAlwaysZero(addedType: ResourceType) { val effect = ProductionIncrease(fixedProduction(addedType), false) - val table = testTable(5) - assertEquals(0, effect.computePoints(table, 0).toLong()) + val player = SimplePlayer(0, testTable(5)) + assertEquals(0, effect.computePoints(player).toLong()) } companion object { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt index 844c28eb..2cdf6781 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt @@ -5,6 +5,7 @@ import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.test.testTable @RunWith(Theories::class) @@ -13,8 +14,8 @@ class RawPointsIncreaseTest { @Theory fun computePoints_equalsNbOfPoints(points: Int) { val rawPointsIncrease = RawPointsIncrease(points) - val table = testTable(5) - assertEquals(points.toLong(), rawPointsIncrease.computePoints(table, 0).toLong()) + val player = SimplePlayer(0, testTable(5)) + assertEquals(points.toLong(), rawPointsIncrease.computePoints(player).toLong()) } companion object { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt index cbea1581..db13e641 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt @@ -24,7 +24,7 @@ class ScienceProgressTest { board.science.addAll(initialScience) val effect = createScienceProgress(compasses, wheels, tablets, jokers) - effect.apply(board) + effect.applyTo(board) assertEquals((initCompasses + compasses).toLong(), board.science.getQuantity(ScienceType.COMPASS).toLong()) assertEquals((initWheels + wheels).toLong(), board.science.getQuantity(ScienceType.WHEEL).toLong()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt index 0531022d..eeaa5047 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt @@ -8,6 +8,7 @@ import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.Color @@ -20,12 +21,11 @@ class SpecialAbilityActivationTest { @Theory fun apply_addsAbility(ability: SpecialAbility) { val effect = SpecialAbilityActivation(ability) - val table = testTable(5) + val player = SimplePlayer(0, testTable(5)) - effect.apply(table, 0) + effect.applyTo(player) - val board = table.getBoard(0) - assertTrue(board.hasSpecial(ability)) + assertTrue(player.board.hasSpecial(ability)) } @Theory @@ -33,46 +33,41 @@ class SpecialAbilityActivationTest { Assume.assumeTrue(ability !== SpecialAbility.COPY_GUILD) val effect = SpecialAbilityActivation(ability) - val table = testTable(5) + val player = SimplePlayer(0, testTable(5)) - assertEquals(0, effect.computePoints(table, 0).toLong()) + assertEquals(0, effect.computePoints(player).toLong()) } @Theory fun computePoints_copiedGuild(guildCard: Card, neighbour: RelativeBoardPosition) { val effect = SpecialAbilityActivation(SpecialAbility.COPY_GUILD) - val table = testTable(5) + val player = SimplePlayer(0, testTable(5)) - val neighbourBoard = table.getBoard(0, neighbour) + val neighbourBoard = player.getBoard(neighbour) neighbourBoard.addCard(guildCard) - val board = table.getBoard(0) - board.copiedGuild = guildCard + player.board.copiedGuild = guildCard - val directPointsFromGuildCard = guildCard.effects.stream().mapToInt { e -> e.computePoints(table, 0) }.sum() - assertEquals(directPointsFromGuildCard.toLong(), effect.computePoints(table, 0).toLong()) + val directPointsFromGuildCard = guildCard.effects.stream().mapToInt { e -> e.computePoints(player) }.sum() + assertEquals(directPointsFromGuildCard.toLong(), effect.computePoints(player).toLong()) } @Test(expected = IllegalStateException::class) fun computePoints_copyGuild_failWhenNoChosenGuild() { val effect = SpecialAbilityActivation(SpecialAbility.COPY_GUILD) - val table = testTable(5) - effect.computePoints(table, 0) + val player = SimplePlayer(0, testTable(5)) + effect.computePoints(player) } companion object { @JvmStatic @DataPoints - fun abilities(): Array<SpecialAbility> { - return SpecialAbility.values() - } + fun abilities(): Array<SpecialAbility> = SpecialAbility.values() @JvmStatic @DataPoints - fun neighbours(): Array<RelativeBoardPosition> { - return arrayOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT) - } + fun neighbours(): Array<RelativeBoardPosition> = arrayOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT) @JvmStatic @DataPoints diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.kt index ee461e38..d1b380e0 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.kt @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.game.moves import org.junit.Assert.assertEquals import org.junit.Assert.fail import org.junit.Test +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.cards.Card @@ -15,17 +16,16 @@ import org.luxons.sevenwonders.game.test.testTable class BuildWonderMoveTest { @Test(expected = InvalidMoveException::class) - fun validate_failsWhenCardNotInHand() { + fun init_failsWhenCardNotInHand() { val table = testTable(3) val hand = sampleCards(0, 7) + val playerContext = PlayerContext(0, table, hand) val anotherCard = testCard("Card that is not in the hand") - val move = createMove(0, anotherCard, MoveType.UPGRADE_WONDER) - - move.validate(table, hand) + createMove(playerContext, anotherCard, MoveType.UPGRADE_WONDER) } @Test(expected = InvalidMoveException::class) - fun validate_failsWhenWonderIsCompletelyBuilt() { + fun init_failsWhenWonderIsCompletelyBuilt() { val settings = testSettings(3) val table = testTable(settings) val hand = sampleCards(0, 7) @@ -49,10 +49,10 @@ class BuildWonderMoveTest { private fun buildOneWonderLevel(settings: Settings, table: Table, hand: List<Card>, cardIndex: Int) { val card = hand[cardIndex] - val move = createMove(0, card, MoveType.UPGRADE_WONDER) - move.validate(table, hand) - move.place(table, mutableListOf(), settings) - move.activate(table, emptyList(), settings) + val playerContext = PlayerContext(0, table, hand) + val move = createMove(playerContext, card, MoveType.UPGRADE_WONDER) + move.place(mutableListOf(), settings) + move.activate(emptyList(), settings) } @Test @@ -61,12 +61,12 @@ class BuildWonderMoveTest { val table = testTable(settings) val hand = sampleCards(0, 7) val cardToUse = hand[0] - val move = createMove(0, cardToUse, MoveType.UPGRADE_WONDER) - move.validate(table, hand) // should not fail + val playerContext = PlayerContext(0, table, hand) + val move = createMove(playerContext, cardToUse, MoveType.UPGRADE_WONDER) val initialStage = table.getBoard(0).wonder.nbBuiltStages - move.place(table, mutableListOf(), settings) + move.place(mutableListOf(), settings) val newStage = table.getBoard(0).wonder.nbBuiltStages 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 005959bc..ca279d97 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 @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.resources import org.junit.Assert.assertEquals import org.junit.Test +import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.resources.Provider.LEFT_PLAYER import org.luxons.sevenwonders.game.resources.Provider.RIGHT_PLAYER @@ -18,9 +19,10 @@ class BestPriceCalculatorTest { @Test fun bestPrice_0forEmptyResources() { val table = testTable(3) + val player0 = SimplePlayer(0, table) val emptyResources = Resources() val emptyTransactions = ResourceTransactions() - assertEquals(TransactionPlan(0, emptyTransactions), bestSolution(emptyResources, table, 0)) + assertEquals(TransactionPlan(0, emptyTransactions), bestSolution(emptyResources, player0)) } @Test @@ -29,6 +31,10 @@ class BestPriceCalculatorTest { val main = testBoard(STONE) val right = testBoard(WOOD) val table = Table(Arrays.asList(main, right, left)) + + val player0 = SimplePlayer(0, table) + val player1 = SimplePlayer(1, table) + val player2 = SimplePlayer(2, table) val resources = createResources(STONE, STONE) @@ -39,9 +45,9 @@ class BestPriceCalculatorTest { val stoneRight = createTransactions(stoneRightSingle) val stoneLeftAndRight = createTransactions(stoneLeftSingle, stoneRightSingle) - assertEquals(TransactionPlan(2, stoneLeft), bestSolution(resources, table, 0)) - assertEquals(TransactionPlan(4, stoneLeftAndRight), bestSolution(resources, table, 1)) - assertEquals(TransactionPlan(2, stoneRight), bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(2, stoneLeft), bestSolution(resources, player0)) + assertEquals(TransactionPlan(4, stoneLeftAndRight), bestSolution(resources, player1)) + assertEquals(TransactionPlan(2, stoneRight), bestSolution(resources, player2)) } @Test @@ -54,15 +60,20 @@ class BestPriceCalculatorTest { val opposite = testBoard(GLASS) val table = Table(Arrays.asList(main, right, opposite, left)) + val player0 = SimplePlayer(0, table) + val player1 = SimplePlayer(1, table) + val player2 = SimplePlayer(2, table) + val player3 = SimplePlayer(3, table) + val resources = createResources(WOOD) val woodLeft = createTransactions(LEFT_PLAYER, WOOD) val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - 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)) + assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, player0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) + assertEquals(TransactionPlan(2, woodLeft), bestSolution(resources, player2)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player3)) } @Test @@ -78,12 +89,16 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, left)) + val player0 = SimplePlayer(0, table) + val player1 = SimplePlayer(1, table) + val player2 = SimplePlayer(2, table) + val resources = createResources(WOOD) val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, table, 0)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 1)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, player0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player2)) } @Test @@ -102,12 +117,16 @@ class BestPriceCalculatorTest { val table = Table(Arrays.asList(main, right, left)) + val player0 = SimplePlayer(0, table) + val player1 = SimplePlayer(1, table) + val player2 = SimplePlayer(2, table) + val resources = createResources(ORE, CLAY) val oreAndClayLeft = createTransactions(LEFT_PLAYER, ORE, CLAY) val clayRight = createTransactions(RIGHT_PLAYER, CLAY) - assertEquals(TransactionPlan(1, clayRight), bestSolution(resources, table, 0)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, table, 1)) - assertEquals(TransactionPlan(4, oreAndClayLeft), bestSolution(resources, table, 2)) + assertEquals(TransactionPlan(1, clayRight), bestSolution(resources, player0)) + assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) + assertEquals(TransactionPlan(4, oreAndClayLeft), bestSolution(resources, player2)) } } 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 ab0b22cf..92623a2b 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 @@ -1,14 +1,10 @@ package org.luxons.sevenwonders.game.resources -import java.util.EnumSet -import java.util.HashSet - +import org.junit.Assert.* import org.junit.Before import org.junit.Test - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue +import java.util.EnumSet +import java.util.HashSet class ProductionTest { @@ -266,15 +262,6 @@ class ProductionTest { } @Test - fun equals_falseWhenNull() { - val production = Production() - production.addFixedResource(ResourceType.GLASS, 1) - production.addChoice(ResourceType.ORE, ResourceType.WOOD) - - assertFalse(production == null) - } - - @Test fun equals_trueWhenSame() { val production = Production() assertEquals(production, production) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt index feda9423..2208431d 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt @@ -1,10 +1,13 @@ package org.luxons.sevenwonders.game.test +import org.luxons.sevenwonders.game.Player +import org.luxons.sevenwonders.game.PlayerContext import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.CustomizableSettings import org.luxons.sevenwonders.game.api.PlayerMove import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.boards.Science import org.luxons.sevenwonders.game.boards.ScienceType import org.luxons.sevenwonders.game.cards.Card @@ -203,14 +206,21 @@ fun createScience(compasses: Int, wheels: Int, tablets: Int, jokers: Int): Scien return science } -fun playCardWithEffect(table: Table, playerIndex: Int, color: Color, effect: Effect) { +fun playCardWithEffect(player: Player, color: Color, effect: Effect) { val card = testCard(color, effect) - val board = table.getBoard(playerIndex) - board.addCard(card) - card.applyTo(table, playerIndex, ResourceTransactions()) + player.board.addCard(card) + card.applyTo(player, ResourceTransactions()) } -fun createMove(playerIndex: Int, card: Card, type: MoveType, vararg transactions: ResourceTransaction): Move { +fun createMove(context: PlayerContext, card: Card, type: MoveType, vararg transactions: ResourceTransaction): Move { val playerMove = PlayerMove(type, card.name, Arrays.asList(*transactions)) - return type.resolve(playerIndex, card, playerMove) + return type.create(playerMove, card, context) +} + +fun singleBoardPlayer(board: Board): Player { + return object: Player { + override val index=0 + override val board = board + override fun getBoard(relativePosition: RelativeBoardPosition): Board = board + } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt index 9270bcca..3d2f3a80 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt @@ -13,22 +13,22 @@ class WonderTest { fun buildLevel_increasesNbBuiltStages() { val wonder = testWonder() assertEquals(0, wonder.nbBuiltStages.toLong()) - wonder.buildLevel(CardBack("img")) + wonder.placeCard(CardBack("img")) assertEquals(1, wonder.nbBuiltStages.toLong()) - wonder.buildLevel(CardBack("img")) + wonder.placeCard(CardBack("img")) assertEquals(2, wonder.nbBuiltStages.toLong()) - wonder.buildLevel(CardBack("img")) + wonder.placeCard(CardBack("img")) assertEquals(3, wonder.nbBuiltStages.toLong()) } @Test fun buildLevel_failsIfFull() { val wonder = testWonder() - wonder.buildLevel(CardBack("img")) - wonder.buildLevel(CardBack("img")) - wonder.buildLevel(CardBack("img")) + wonder.placeCard(CardBack("img")) + wonder.placeCard(CardBack("img")) + wonder.placeCard(CardBack("img")) try { - wonder.buildLevel(CardBack("img")) + wonder.placeCard(CardBack("img")) fail() } catch (e: IllegalStateException) { // expected exception because there is no 4th level in this wonder |