From ada5f8702fc154a222095927266f55da9658507e Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Wed, 11 Jul 2018 00:20:59 +0200 Subject: Random cleaning --- .../org/luxons/sevenwonders/game/api/Table.kt | 23 +++++++----------- .../sevenwonders/game/boards/BoardElementType.kt | 17 ------------- .../game/boards/RelativeBoardPosition.kt | 28 ++++++---------------- .../game/cards/HandRotationDirection.kt | 8 ++----- .../game/effects/BonusPerBoardElement.kt | 28 +++++++++++++--------- .../luxons/sevenwonders/game/boards/BoardTest.kt | 12 ++++++---- .../game/effects/BonusPerBoardElementTest.kt | 1 - .../game/effects/SpecialAbilityActivationTest.kt | 1 - 8 files changed, 42 insertions(+), 76 deletions(-) delete mode 100644 game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/BoardElementType.kt 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) { var lastPlayedMoves: List = 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) { 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) { } } - fun getNeighbourGuildCards(playerIndex: Int): List { - return getNeighbourBoards(playerIndex).flatMap(Board::getPlayedCards).filter { c -> c.color == Color.PURPLE } - } - - private fun getNeighbourBoards(playerIndex: Int): List { - return Provider.values().map { getBoard(playerIndex, it.boardPosition) } - } + fun getNeighbourGuildCards(playerIndex: Int): List = 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?): Int = board.getNbCardsOfColor(colors!!) - }, - BUILT_WONDER_STAGES { - override fun getElementCount(board: Board, colors: List?): Int = board.wonder.nbBuiltStages - }, - DEFEAT_TOKEN { - override fun getElementCount(board: Board, colors: List?): Int = board.military.nbDefeatTokens - }; - - abstract fun getElementCount(board: Board, colors: List?): 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, val type: BoardElementType, val gold: Int = 0, val points: Int = 0, - // only relevant if type=CARD - val colors: List? = null + val colors: List? = 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 -- cgit