diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-11 00:20:59 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-11 00:50:17 +0200 |
commit | ada5f8702fc154a222095927266f55da9658507e (patch) | |
tree | 2eec50b0dcd91c72e502d18b2d58dd3f4f9b8cac /game-engine/src/main/kotlin/org/luxons | |
parent | Refactor wonders creation (diff) | |
download | seven-wonders-ada5f8702fc154a222095927266f55da9658507e.tar.gz seven-wonders-ada5f8702fc154a222095927266f55da9658507e.tar.bz2 seven-wonders-ada5f8702fc154a222095927266f55da9658507e.zip |
Random cleaning
Diffstat (limited to 'game-engine/src/main/kotlin/org/luxons')
5 files changed, 34 insertions, 70 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Table.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Table.kt index 2c2e5066..bf2bdfc7 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Table.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Table.kt @@ -2,12 +2,12 @@ package org.luxons.sevenwonders.game.api import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.boards.RelativeBoardPosition +import org.luxons.sevenwonders.game.boards.neighboursPositions import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.Color import org.luxons.sevenwonders.game.cards.HandRotationDirection import org.luxons.sevenwonders.game.data.Age import org.luxons.sevenwonders.game.moves.Move -import org.luxons.sevenwonders.game.resources.Provider /** * The table contains what is visible by all the players in the game: the boards and their played cards, and the @@ -25,13 +25,10 @@ class Table(val boards: List<Board>) { var lastPlayedMoves: List<Move> = emptyList() - fun getBoard(playerIndex: Int): Board { - return boards[playerIndex] - } + fun getBoard(playerIndex: Int): Board = boards[playerIndex] - fun getBoard(playerIndex: Int, position: RelativeBoardPosition): Board { - return boards[position.getIndexFrom(playerIndex, nbPlayers)] - } + fun getBoard(playerIndex: Int, position: RelativeBoardPosition): Board = + boards[position.getIndexFrom(playerIndex, nbPlayers)] fun increaseCurrentAge() { this.currentAge++ @@ -40,7 +37,7 @@ class Table(val boards: List<Board>) { fun resolveMilitaryConflicts() { for (i in 0 until nbPlayers) { val board1 = getBoard(i) - val board2 = getBoard((i + 1) % nbPlayers) + val board2 = getBoard(i, RelativeBoardPosition.RIGHT) resolveConflict(board1, board2) } } @@ -57,11 +54,7 @@ class Table(val boards: List<Board>) { } } - fun getNeighbourGuildCards(playerIndex: Int): List<Card> { - return getNeighbourBoards(playerIndex).flatMap(Board::getPlayedCards).filter { c -> c.color == Color.PURPLE } - } - - private fun getNeighbourBoards(playerIndex: Int): List<Board> { - return Provider.values().map { getBoard(playerIndex, it.boardPosition) } - } + fun getNeighbourGuildCards(playerIndex: Int): List<Card> = neighboursPositions() + .flatMap { getBoard(playerIndex, it).getPlayedCards() } + .filter { it.color == Color.PURPLE } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/BoardElementType.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/BoardElementType.kt deleted file mode 100644 index d35e9777..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/BoardElementType.kt +++ /dev/null @@ -1,17 +0,0 @@ -package org.luxons.sevenwonders.game.boards - -import org.luxons.sevenwonders.game.cards.Color - -enum class BoardElementType { - CARD { - override fun getElementCount(board: Board, colors: List<Color>?): Int = board.getNbCardsOfColor(colors!!) - }, - BUILT_WONDER_STAGES { - override fun getElementCount(board: Board, colors: List<Color>?): Int = board.wonder.nbBuiltStages - }, - DEFEAT_TOKEN { - override fun getElementCount(board: Board, colors: List<Color>?): Int = board.military.nbDefeatTokens - }; - - abstract fun getElementCount(board: Board, colors: List<Color>?): Int -} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.kt index fcd629ec..c89205c0 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.kt @@ -1,25 +1,11 @@ package org.luxons.sevenwonders.game.boards -enum class RelativeBoardPosition { - LEFT { - override fun getIndexFrom(playerIndex: Int, nbPlayers: Int): Int { - return wrapIndex(playerIndex - 1, nbPlayers) - } - }, - SELF { - override fun getIndexFrom(playerIndex: Int, nbPlayers: Int): Int { - return playerIndex - } - }, - RIGHT { - override fun getIndexFrom(playerIndex: Int, nbPlayers: Int): Int { - return wrapIndex(playerIndex + 1, nbPlayers) - } - }; +enum class RelativeBoardPosition(private val offset: Int) { + LEFT(-1), + SELF(0), + RIGHT(1); - abstract fun getIndexFrom(playerIndex: Int, nbPlayers: Int): Int - - internal fun wrapIndex(index: Int, nbPlayers: Int): Int { - return Math.floorMod(index, nbPlayers) - } + fun getIndexFrom(playerIndex: Int, nbPlayers: Int): Int = Math.floorMod(playerIndex + offset, nbPlayers) } + +fun neighboursPositions() = listOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirection.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirection.kt index 13494175..a10ec19f 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirection.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirection.kt @@ -5,11 +5,7 @@ enum class HandRotationDirection { RIGHT; companion object { - - fun forAge(age: Int): HandRotationDirection { - // clockwise (pass to the left) at age 1, and alternating - return if (age % 2 == 0) HandRotationDirection.RIGHT else HandRotationDirection.LEFT - } + // clockwise (pass to the left) at age 1, and alternating + fun forAge(age: Int): HandRotationDirection = if (age % 2 == 0) RIGHT else LEFT } } - 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 104dfc77..defd896a 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 @@ -2,32 +2,38 @@ package org.luxons.sevenwonders.game.effects import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board -import org.luxons.sevenwonders.game.boards.BoardElementType import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.Color +enum class BoardElementType { + CARD, + BUILT_WONDER_STAGES, + DEFEAT_TOKEN +} + data class BonusPerBoardElement ( val boards: List<RelativeBoardPosition>, val type: BoardElementType, val gold: Int = 0, val points: Int = 0, - // only relevant if type=CARD - val colors: List<Color>? = null + val colors: List<Color>? = null // only relevant if type=CARD ) : Effect { override fun apply(table: Table, playerIndex: Int) { - val goldGain = gold * computeNbOfMatchingElementsIn(table, playerIndex) - val board = table.getBoard(playerIndex) - board.addGold(goldGain) + val goldGain = gold * nbMatchingElementsIn(table, playerIndex) + table.getBoard(playerIndex).addGold(goldGain) } - override fun computePoints(table: Table, playerIndex: Int): Int = - points * computeNbOfMatchingElementsIn(table, playerIndex) + override fun computePoints(table: Table, playerIndex: Int): Int = points * nbMatchingElementsIn(table, playerIndex) - private fun computeNbOfMatchingElementsIn(table: Table, playerIndex: Int): Int = boards + private fun nbMatchingElementsIn(table: Table, playerIndex: Int): Int = boards .map { pos -> table.getBoard(playerIndex, pos) } - .map(::computeNbOfMatchingElementsIn) + .map(::nbMatchingElementsIn) .sum() - private fun computeNbOfMatchingElementsIn(board: Board): Int = type.getElementCount(board, colors) + private fun nbMatchingElementsIn(board: Board): Int = when (type) { + BoardElementType.CARD -> board.getNbCardsOfColor(colors!!) + BoardElementType.BUILT_WONDER_STAGES -> board.wonder.nbBuiltStages + BoardElementType.DEFEAT_TOKEN -> board.military.nbDefeatTokens + } } |