From 38c4cd33e676d1e042cd3dcf30d7f3915d37d253 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 30 Apr 2023 03:31:12 +0200 Subject: Prevent duplicate cards even with "special free" ability Resolves: https://github.com/joffrey-bion/seven-wonders/issues/142 --- .../src/main/kotlin/org/luxons/sevenwonders/engine/cards/Cards.kt | 2 +- .../main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt | 3 +++ .../kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) (limited to 'sw-engine') diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Cards.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Cards.kt index a20f37a0..4ca87734 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Cards.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Cards.kt @@ -32,7 +32,7 @@ internal data class Card( private fun isChainableOn(board: Board): Boolean = !isAlreadyOnBoard(board) && isAnyParentOnBoard(board) - private fun isAlreadyOnBoard(board: Board): Boolean = board.isPlayed(name) + fun isAlreadyOnBoard(board: Board): Boolean = board.isPlayed(name) private fun isAnyParentOnBoard(board: Board): Boolean = chainParents.any { board.isPlayed(it) } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt index 66ac4e4c..f87350d4 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt @@ -9,6 +9,9 @@ internal class PlayCardMove(move: PlayerMove, card: Card, player: PlayerContext) CardFromHandMove(move, card, player) { init { + if (card.isAlreadyOnBoard(player.board)) { + throw InvalidMoveException(this, "card ${card.name} is already on the board") + } if (!card.isPlayableOnBoardWith(player.board, transactions)) { throw InvalidMoveException(this, "requirements not met to play the card ${card.name}") } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt index d4067a2b..13212e4a 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt @@ -13,6 +13,9 @@ internal class PlayFreeCardMove(move: PlayerMove, card: Card, playerContext: Pla if (!board.canPlayFreeCard(playerContext.currentAge)) { throw InvalidMoveException(this, "no free card available for the current age ${playerContext.currentAge}") } + if (card.isAlreadyOnBoard(board)) { + throw InvalidMoveException(this, "card ${card.name} is already on the board") + } } override fun place(discardedCards: MutableList) = playerContext.board.addCard(card) -- cgit