summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2018-07-08 12:28:20 +0200
committerjbion <joffrey.bion@amadeus.com>2018-07-08 12:28:20 +0200
commitd71576ee81abacfb16df91723e2545b308f47561 (patch)
treecd6d1292e071ad8fc37e2c6eba89c3f3ca7dca3c
parentKotlin mig: serializers tests (diff)
downloadseven-wonders-d71576ee81abacfb16df91723e2545b308f47561.tar.gz
seven-wonders-d71576ee81abacfb16df91723e2545b308f47561.tar.bz2
seven-wonders-d71576ee81abacfb16df91723e2545b308f47561.zip
Kotlin mig: Game tests
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java129
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt109
2 files changed, 109 insertions, 129 deletions
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java
deleted file mode 100644
index b0c11836..00000000
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package org.luxons.sevenwonders.game;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.junit.Test;
-import org.luxons.sevenwonders.game.api.CustomizableSettings;
-import org.luxons.sevenwonders.game.api.HandCard;
-import org.luxons.sevenwonders.game.api.PlayerMove;
-import org.luxons.sevenwonders.game.api.PlayerTurnInfo;
-import org.luxons.sevenwonders.game.api.Table;
-import org.luxons.sevenwonders.game.cards.Card;
-import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
-import org.luxons.sevenwonders.game.moves.Move;
-import org.luxons.sevenwonders.game.moves.MoveType;
-import org.luxons.sevenwonders.game.resources.BestPriceCalculator;
-import org.luxons.sevenwonders.game.resources.ResourceTransaction;
-import org.luxons.sevenwonders.game.resources.ResourceTransactions;
-import org.luxons.sevenwonders.game.resources.Resources;
-import org.luxons.sevenwonders.game.test.TestUtilsKt;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-public class GameTest {
-
- @Test
- public void testFullGame() {
- int nbPlayers = 5;
- Game game = createGame(nbPlayers);
-
- for (int age = 1; age <= 3; age++) {
- playAge(nbPlayers, game, age);
- }
-
- game.computeScore();
- }
-
- private void playAge(int nbPlayers, Game game, int age) {
- for (int i = 0; i < 6; i++) {
- playTurn(nbPlayers, game, age, 7 - i);
- }
- }
-
- private static Game createGame(int nbPlayers) {
- CustomizableSettings settings = TestUtilsKt.testCustomizableSettings();
- return new GameDefinitionLoader().getGameDefinition().initGame(0, settings, nbPlayers);
- }
-
- private static void playTurn(int nbPlayers, Game game, int ageToCheck, int handSize) {
- Collection<PlayerTurnInfo> turnInfos = game.getCurrentTurnInfo();
- assertEquals(nbPlayers, turnInfos.size());
-
- Map<Integer, PlayerMove> sentMoves = new HashMap<>(turnInfos.size());
- for (PlayerTurnInfo turnInfo : turnInfos) {
- assertEquals(ageToCheck, turnInfo.getCurrentAge());
- assertEquals(handSize, turnInfo.getHand().size());
- PlayerMove move = getFirstAvailableMove(turnInfo);
- if (move != null) {
- game.prepareMove(turnInfo.getPlayerIndex(), move);
- sentMoves.put(turnInfo.getPlayerIndex(), move);
- }
- }
- assertTrue(game.allPlayersPreparedTheirMove());
- Table table = game.playTurn();
- checkLastPlayedMoves(sentMoves, table);
- }
-
- private static PlayerMove getFirstAvailableMove(PlayerTurnInfo turnInfo) {
- switch (turnInfo.getAction()) {
- case PLAY:
- case PLAY_2:
- case PLAY_LAST:
- return createPlayCardMove(turnInfo);
- case PICK_NEIGHBOR_GUILD:
- return createPickGuildMove(turnInfo);
- case WAIT:
- default:
- return null;
- }
- }
-
- private static PlayerMove createPlayCardMove(PlayerTurnInfo turnInfo) {
- for (HandCard handCard : turnInfo.getHand()) {
- if (handCard.isPlayable()) {
- Set<ResourceTransaction> resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo);
- return new PlayerMove(MoveType.PLAY, handCard.getCard().getName(), resourcesToBuy);
- }
- }
- HandCard firstCardInHand = turnInfo.getHand().get(0);
- return new PlayerMove(MoveType.DISCARD, firstCardInHand.getCard().getName());
- }
-
- private static Set<ResourceTransaction> findResourcesToBuyFor(HandCard handCard, PlayerTurnInfo turnInfo) {
- if (handCard.isFree()) {
- return Collections.emptySet();
- }
- Resources requiredResources = handCard.getCard().getRequirements().getResources();
- Table table = turnInfo.getTable();
- int playerIndex = turnInfo.getPlayerIndex();
- ResourceTransactions transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex);
- return transactions.toTransactions();
- }
-
- private static PlayerMove createPickGuildMove(PlayerTurnInfo turnInfo) {
- List<Card> neighbourGuilds = turnInfo.getNeighbourGuildCards();
- assertNotNull(neighbourGuilds);
- assertFalse(neighbourGuilds.isEmpty());
- String cardName = neighbourGuilds.get(0).getName();
- return new PlayerMove(MoveType.COPY_GUILD, cardName);
- }
-
- private static void checkLastPlayedMoves(Map<Integer, PlayerMove> sentMoves, Table table) {
- for (Move move : table.getLastPlayedMoves()) {
- PlayerMove sentMove = sentMoves.get(move.getPlayerIndex());
- assertNotNull(sentMove);
- assertNotNull(move.getCard());
- assertEquals(sentMove.getCardName(), move.getCard().getName());
- assertSame(sentMove.getType(), move.getType());
- }
- }
-}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt
new file mode 100644
index 00000000..028e7a0e
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt
@@ -0,0 +1,109 @@
+package org.luxons.sevenwonders.game
+
+import org.junit.Assert.*
+import org.junit.Test
+import org.luxons.sevenwonders.game.api.Action
+import org.luxons.sevenwonders.game.api.HandCard
+import org.luxons.sevenwonders.game.api.PlayerMove
+import org.luxons.sevenwonders.game.api.PlayerTurnInfo
+import org.luxons.sevenwonders.game.api.Table
+import org.luxons.sevenwonders.game.data.GameDefinitionLoader
+import org.luxons.sevenwonders.game.moves.MoveType
+import org.luxons.sevenwonders.game.resources.BestPriceCalculator
+import org.luxons.sevenwonders.game.resources.ResourceTransaction
+import org.luxons.sevenwonders.game.test.testCustomizableSettings
+import java.util.HashMap
+
+class GameTest {
+
+ @Test
+ fun testFullGame() {
+ val nbPlayers = 5
+ val game = createGame(nbPlayers)
+
+ for (age in 1..3) {
+ playAge(nbPlayers, game, age)
+ }
+
+ game.computeScore()
+ }
+
+ private fun playAge(nbPlayers: Int, game: Game, age: Int) {
+ for (i in 0..5) {
+ playTurn(nbPlayers, game, age, 7 - i)
+ }
+ }
+
+ private fun createGame(nbPlayers: Int): Game {
+ val settings = testCustomizableSettings()
+ return GameDefinitionLoader().gameDefinition.initGame(0, settings, nbPlayers)
+ }
+
+ private fun playTurn(nbPlayers: Int, game: Game, ageToCheck: Int, handSize: Int) {
+ val turnInfos = game.getCurrentTurnInfo()
+ assertEquals(nbPlayers.toLong(), turnInfos.size.toLong())
+
+ val sentMoves = HashMap<Int, PlayerMove>(turnInfos.size)
+ for (turnInfo in turnInfos) {
+ assertEquals(ageToCheck.toLong(), turnInfo.currentAge.toLong())
+ assertEquals(handSize.toLong(), turnInfo.hand.size.toLong())
+ val move = getFirstAvailableMove(turnInfo)
+ if (move != null) {
+ game.prepareMove(turnInfo.playerIndex, move)
+ sentMoves[turnInfo.playerIndex] = move
+ }
+ }
+ assertTrue(game.allPlayersPreparedTheirMove())
+ val table = game.playTurn()
+ checkLastPlayedMoves(sentMoves, table)
+ }
+
+ private fun getFirstAvailableMove(turnInfo: PlayerTurnInfo): PlayerMove? {
+ when (turnInfo.action) {
+ Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> return createPlayCardMove(turnInfo)
+ Action.PICK_NEIGHBOR_GUILD -> return createPickGuildMove(turnInfo)
+ Action.WAIT -> return null
+ else -> return null
+ }
+ }
+
+ private fun createPlayCardMove(turnInfo: PlayerTurnInfo): PlayerMove {
+ for (handCard in turnInfo.hand) {
+ if (handCard.isPlayable) {
+ val resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo)
+ return PlayerMove(MoveType.PLAY, handCard.card.name, resourcesToBuy)
+ }
+ }
+ val firstCardInHand = turnInfo.hand[0]
+ return PlayerMove(MoveType.DISCARD, firstCardInHand.card.name)
+ }
+
+ private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): Set<ResourceTransaction> {
+ if (handCard.isFree) {
+ return emptySet()
+ }
+ val requiredResources = handCard.card.requirements.resources
+ val table = turnInfo.table
+ val playerIndex = turnInfo.playerIndex
+ val transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex)
+ return transactions.toTransactions()
+ }
+
+ private fun createPickGuildMove(turnInfo: PlayerTurnInfo): PlayerMove {
+ val neighbourGuilds = turnInfo.neighbourGuildCards
+ assertNotNull(neighbourGuilds)
+ assertFalse(neighbourGuilds.isEmpty())
+ val cardName = neighbourGuilds[0].name
+ return PlayerMove(MoveType.COPY_GUILD, cardName)
+ }
+
+ private fun checkLastPlayedMoves(sentMoves: Map<Int, PlayerMove>, table: Table) {
+ for (move in table.lastPlayedMoves) {
+ val sentMove = sentMoves[move.playerIndex]
+ assertNotNull(sentMove)
+ assertNotNull(move.card)
+ assertEquals(sentMove!!.cardName, move.card.name)
+ assertSame(sentMove.type, move.type)
+ }
+ }
+}
bgstack15