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 | |
parent | Refactor wonders creation (diff) | |
download | seven-wonders-ada5f8702fc154a222095927266f55da9658507e.tar.gz seven-wonders-ada5f8702fc154a222095927266f55da9658507e.tar.bz2 seven-wonders-ada5f8702fc154a222095927266f55da9658507e.zip |
Random cleaning
8 files changed, 42 insertions, 76 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 + } } 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 526fbced..7c23bbd0 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 @@ -80,8 +80,10 @@ class BoardTest { @Theory fun getNbCardsOfColor_properCount_singleColor( - type: ResourceType, @FromDataPoints("nbCards") nbCards: Int, - @FromDataPoints("nbCards") nbOtherCards: Int, color: Color + type: ResourceType, + @FromDataPoints("nbCards") nbCards: Int, + @FromDataPoints("nbCards") nbOtherCards: Int, + color: Color ) { val board = testBoard(type) addCards(board, nbCards, nbOtherCards, color) @@ -90,9 +92,11 @@ class BoardTest { @Theory fun getNbCardsOfColor_properCount_multiColors( - type: ResourceType, @FromDataPoints("nbCards") nbCards1: Int, + type: ResourceType, + @FromDataPoints("nbCards") nbCards1: Int, @FromDataPoints("nbCards") nbCards2: Int, - @FromDataPoints("nbCards") nbOtherCards: Int, color1: Color, + @FromDataPoints("nbCards") nbOtherCards: Int, + color1: Color, color2: Color ) { val board = testBoard(type) 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 ccc1c142..3f0b0aa0 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 @@ -6,7 +6,6 @@ import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith import org.luxons.sevenwonders.game.api.Table -import org.luxons.sevenwonders.game.boards.BoardElementType import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.CardBack import org.luxons.sevenwonders.game.cards.Color 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 7282d60d..0531022d 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,7 +8,6 @@ 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.boards.BoardElementType import org.luxons.sevenwonders.game.boards.RelativeBoardPosition import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.Color |