summaryrefslogtreecommitdiff
path: root/game-engine/src/main/kotlin/org
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@amadeus.com>2018-07-09 15:22:53 +0200
committerJoffrey Bion <joffrey.bion@amadeus.com>2018-07-09 15:22:53 +0200
commitf92facc7ccfc9564f7481940c5b23b66a473982f (patch)
tree00ba42ee9cad92aa7d82336a85b891e02f784869 /game-engine/src/main/kotlin/org
parentKotlin mig: Boards package (diff)
downloadseven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.tar.gz
seven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.tar.bz2
seven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.zip
Kotlin mig: moves package
Diffstat (limited to 'game-engine/src/main/kotlin/org')
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Action.kt2
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/HandCard.kt2
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt2
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt25
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt16
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt41
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt20
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/InvalidMoveException.kt3
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt21
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt24
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt25
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt34
12 files changed, 211 insertions, 4 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Action.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Action.kt
index 37ab3e6f..6cfd3679 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Action.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Action.kt
@@ -1,6 +1,6 @@
package org.luxons.sevenwonders.game.api
-enum class Action constructor(val message: String) {
+enum class Action(val message: String) {
PLAY("Pick the card you want to play or discard."),
PLAY_2("Pick the first card you want to play or discard. Note that you have the ability to play these 2 last "
+ "cards. You will choose how to play the last one during your next turn."),
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 d487a9ac..1b11edef 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
@@ -9,9 +9,7 @@ import org.luxons.sevenwonders.game.cards.Card
class HandCard(val card: Card, table: Table, playerIndex: Int) {
val isChainable: Boolean
-
val isFree: Boolean
-
val isPlayable: Boolean = card.isPlayable(table, playerIndex)
init {
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt
index 4d4ef7cc..95e6b13c 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt
@@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game.api
import org.luxons.sevenwonders.game.moves.MoveType
import org.luxons.sevenwonders.game.resources.ResourceTransaction
-data class PlayerMove @JvmOverloads constructor(
+data class PlayerMove(
val type: MoveType,
val cardName: String,
val transactions: Collection<ResourceTransaction> = emptyList()
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
new file mode 100644
index 00000000..bbd9ebab
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/BuildWonderMove.kt
@@ -0,0 +1,25 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ @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")
+ }
+ }
+
+ override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) =
+ table.getBoard(playerIndex).wonder.buildLevel(card.back)
+
+ override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) =
+ table.getBoard(playerIndex).wonder.activateLastBuiltStage(table, playerIndex, 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
new file mode 100644
index 00000000..7b084d43
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CardFromHandMove.kt
@@ -0,0 +1,16 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ @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")
+ }
+ }
+}
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
new file mode 100644
index 00000000..5a017e53
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/CopyGuildMove.kt
@@ -0,0 +1,41 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ @Throws(InvalidMoveException::class)
+ override fun validate(table: Table, playerHand: List<Card>) {
+ val board = table.getBoard(playerIndex)
+ if (!board.hasSpecial(SpecialAbility.COPY_GUILD)) {
+ throw InvalidMoveException("Player $playerIndex does not have the 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")
+ }
+ val leftNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.LEFT)
+ val rightNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.RIGHT)
+ if (!leftNeighbourHasIt && !rightNeighbourHasIt) {
+ throw InvalidMoveException("Player $playerIndex cannot copy card ${card.name} because none of his neighbour has it")
+ }
+ }
+
+ private fun neighbourHasTheCard(table: Table, position: RelativeBoardPosition): Boolean {
+ val neighbourBoard = table.getBoard(playerIndex, position)
+ return neighbourBoard.getPlayedCards().contains(card)
+ }
+
+ // nothing special to do here
+ override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) = Unit
+
+ override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) {
+ table.getBoard(playerIndex).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
new file mode 100644
index 00000000..cf1b69ba
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/DiscardMove.kt
@@ -0,0 +1,20 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ override fun place(table: Table, 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)
+ }
+}
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
new file mode 100644
index 00000000..d816355d
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/InvalidMoveException.kt
@@ -0,0 +1,3 @@
+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
new file mode 100644
index 00000000..885f267f
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/Move.kt
@@ -0,0 +1,21 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ 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 activate(table: Table, discardedCards: List<Card>, settings: Settings)
+}
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
new file mode 100644
index 00000000..ced0f3a1
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/MoveType.kt
@@ -0,0 +1,24 @@
+package org.luxons.sevenwonders.game.moves
+
+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)
+ };
+
+ abstract fun resolve(playerIndex: Int, card: Card, move: PlayerMove): Move
+}
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
new file mode 100644
index 00000000..b87916b1
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayCardMove.kt
@@ -0,0 +1,25 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ @Throws(InvalidMoveException::class)
+ override fun validate(table: Table, playerHand: List<Card>) {
+ super.validate(table, playerHand)
+ val board = table.getBoard(playerIndex)
+ if (!card.isChainableOn(board) && !card.requirements.areMetWithHelpBy(board, transactions)) {
+ throw InvalidMoveException("Player $playerIndex cannot play the card ${card.name} with the given resources")
+ }
+ }
+
+ override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) =
+ table.getBoard(playerIndex).addCard(card)
+
+ override fun activate(table: Table, discardedCards: List<Card>, settings: Settings) =
+ card.applyTo(table, playerIndex, 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
new file mode 100644
index 00000000..125b34a6
--- /dev/null
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.kt
@@ -0,0 +1,34 @@
+package org.luxons.sevenwonders.game.moves
+
+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) {
+
+ @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)
+ )
+ }
+ }
+
+ override fun place(table: Table, discardedCards: MutableList<Card>, settings: Settings) {
+ val board = table.getBoard(playerIndex)
+ board.addCard(card)
+ }
+
+ override fun activate(table: Table, 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)
+ }
+}
bgstack15