diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-22 00:26:20 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-22 00:26:20 +0100 |
commit | 606709b1347170f303f55e15c98f845d7a2f1e3d (patch) | |
tree | 423a20da0f70add2077ad3db594ed68990e36eb2 /src | |
parent | Remove unused "nb of guild cards" setting (diff) | |
download | seven-wonders-606709b1347170f303f55e15c98f845d7a2f1e3d.tar.gz seven-wonders-606709b1347170f303f55e15c98f845d7a2f1e3d.tar.bz2 seven-wonders-606709b1347170f303f55e15c98f845d7a2f1e3d.zip |
Huge refac to pass the Table instead of left/right/self boards
Diffstat (limited to 'src')
31 files changed, 186 insertions, 179 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index d9dec5fd..e9962e10 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -17,6 +17,8 @@ public class Game { private final Settings settings; + private final List<Player> players; + private final Table table; private final Decks decks; @@ -32,7 +34,8 @@ public class Game { public Game(long id, Settings settings, List<Player> players, List<Board> boards, Decks decks) { this.id = id; this.settings = settings; - this.table = new Table(players, boards); + this.players = players; + this.table = new Table(boards); this.decks = decks; this.discardedCards = new ArrayList<>(); this.hands = new HashMap<>(); @@ -44,12 +47,8 @@ public class Game { return id; } - public List<Player> getPlayers() { - return table.getPlayers(); - } - public boolean containsUser(String userName) { - return getPlayers().stream().anyMatch(p -> p.getUserName().equals(userName)); + return players.stream().anyMatch(p -> p.getUserName().equals(userName)); } private void startNewAge() { @@ -65,17 +64,15 @@ public class Game { .collect(Collectors.toList()); } - public void prepareCard(int playerIndex, Move move) throws InvalidMoveException { - validateMove(playerIndex, move); - preparedMoves.put(playerIndex, move); - } - - private void validateMove(int playerIndex, Move move) throws InvalidMoveException { - // TODO throw if invalid + public void prepareCard(Move move) throws InvalidMoveException { + if (!move.isValid(table)) { + throw new InvalidMoveException(); + } + preparedMoves.put(move.getPlayerIndex(), move); } public boolean areAllPlayersReady() { - return preparedMoves.size() == table.getPlayers().size(); + return preparedMoves.size() == players.size(); } public List<Move> playTurn() { @@ -131,7 +128,6 @@ public class Game { break; } }); - } private static class MissingPreparedMoveException extends RuntimeException { diff --git a/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java b/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java index 743a48db..80c09549 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java @@ -1,19 +1,24 @@ package org.luxons.sevenwonders.game.api; +import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Card; public class HandCard { private final Card data; - private boolean chainable; + private final boolean chainable; - private boolean free; + private final boolean free; - private boolean playable; + private final boolean playable; - public HandCard(Card card) { + public HandCard(Card card, Table table, int playerIndex) { + Board board = table.getBoard(playerIndex); this.data = card; + this.chainable = card.isChainableOn(board); + this.free = card.isAffordedBy(board) && card.getRequirements().getGold() == 0; + this.playable = card.isPlayable(table, playerIndex); } public Card getData() { @@ -24,23 +29,12 @@ public class HandCard { return chainable; } - public void setChainable(boolean chainable) { - this.chainable = chainable; - } - public boolean isFree() { return free; } - public void setFree(boolean free) { - this.free = free; - } - public boolean isPlayable() { return playable; } - public void setPlayable(boolean playable) { - this.playable = playable; - } } diff --git a/src/main/java/org/luxons/sevenwonders/game/api/Table.java b/src/main/java/org/luxons/sevenwonders/game/api/Table.java index 44813ee9..9d33c779 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java @@ -3,8 +3,8 @@ package org.luxons.sevenwonders.game.api; import java.util.List; import java.util.stream.Collectors; -import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; import org.luxons.sevenwonders.game.cards.Card; /** @@ -15,29 +15,25 @@ public class Table { private final int nbPlayers; - private final List<Player> players; - private final List<Board> boards; - public Table(List<Player> players, List<Board> boards) { - this.nbPlayers = players.size(); - this.players = players; + public Table(List<Board> boards) { + this.nbPlayers = boards.size(); this.boards = boards; - if (players.size() != boards.size()) { - throw new IllegalArgumentException( - String.format("There are %d boards for %d players, it doesn't make sense", boards.size(), - players.size())); - } - } - - public List<Player> getPlayers() { - return players; } public List<Board> getBoards() { return boards; } + public Board getBoard(int playerIndex) { + return boards.get(playerIndex); + } + + public Board getBoard(int playerIndex, RelativeBoardPosition position) { + return boards.get(position.getIndexFrom(playerIndex, nbPlayers)); + } + public int getNbPlayers() { return nbPlayers; } @@ -53,17 +49,12 @@ public class Table { } public void activateCard(int playerIndex, Card card) { - Board board = boards.get(playerIndex); - Board left = boards.get(wrapIndex(playerIndex - 1)); - Board right = boards.get(wrapIndex(playerIndex + 1)); - card.applyTo(board, left, right); + card.applyTo(this, playerIndex); } public void activateCurrentWonderStage(int playerIndex) { Board board = boards.get(playerIndex); - Board left = boards.get(wrapIndex(playerIndex - 1)); - Board right = boards.get(wrapIndex(playerIndex + 1)); - board.activateCurrentWonderLevel(left, right); + board.activateCurrentWonderLevel(this, playerIndex); } public void discard(int playerIndex, int goldBonus) { @@ -71,10 +62,6 @@ public class Table { board.setGold(board.getGold() + goldBonus); } - private int wrapIndex(int index) { - return Math.floorMod(index, nbPlayers); - } - public PlayerTurnInfo createPlayerTurnInfo(int playerIndex, List<Card> cards) { PlayerTurnInfo pti = new PlayerTurnInfo(playerIndex, this); pti.setHand(createHand(playerIndex, cards)); @@ -82,17 +69,6 @@ public class Table { } private List<HandCard> createHand(int playerIndex, List<Card> cards) { - return cards.stream().map(c -> createHandCard(playerIndex, c)).collect(Collectors.toList()); - } - - private HandCard createHandCard(int playerIndex, Card card) { - Board board = boards.get(playerIndex); - Board left = boards.get(wrapIndex(playerIndex - 1)); - Board right = boards.get(wrapIndex(playerIndex + 1)); - HandCard handCard = new HandCard(card); - handCard.setChainable(card.isChainableOn(board)); - handCard.setFree(card.isAffordedBy(board) && card.getRequirements().getGold() == 0); - handCard.setPlayable(card.isPlayable(board, left, right)); - return handCard; + return cards.stream().map(c -> new HandCard(c, this, playerIndex)).collect(Collectors.toList()); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java index 1ac70daf..c509ee3b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -3,7 +3,9 @@ package org.luxons.sevenwonders.game.boards; import java.util.ArrayList; import java.util.List; +import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.resources.Production; @@ -13,6 +15,8 @@ public class Board { private final Wonder wonder; + private final Player player; + private final List<Card> playedCards = new ArrayList<>(); private final Production production = new Production(); @@ -29,8 +33,9 @@ public class Board { private int nbDefeatTokens; - public Board(Wonder wonder, Settings settings) { + public Board(Wonder wonder, Player player, Settings settings) { this.wonder = wonder; + this.player = player; this.wonderLevel = 0; this.gold = settings.getInitialGold(); this.tradingRules = new TradingRules(settings.getDefaultTradingCost()); @@ -41,6 +46,10 @@ public class Board { return wonder; } + public Player getPlayer() { + return player; + } + public List<Card> getPlayedCards() { return playedCards; } @@ -89,12 +98,8 @@ public class Board { this.wonderLevel++; } - public void activateCurrentWonderLevel(Board leftNeighbourBoard, Board rightNeighbourBoard) { - activateWonderLevel(wonderLevel, leftNeighbourBoard, rightNeighbourBoard); - } - - public void activateWonderLevel(int level, Board leftNeighbourBoard, Board rightNeighbourBoard) { - wonder.getLevels().get(level).activate(this, leftNeighbourBoard, rightNeighbourBoard); + public void activateCurrentWonderLevel(Table table, int playerIndex) { + wonder.getLevels().get(wonderLevel).activate(table, playerIndex); } public int getNbWarSymbols() { diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java b/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java index 34cab4a4..5c029331 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java @@ -1,29 +1,28 @@ -package org.luxons.sevenwonders.game.effects; +package org.luxons.sevenwonders.game.boards; import java.util.List; -import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Color; public enum BoardElementType { CARD { @Override - int getElementCount(Board board, List<Color> colors) { + public int getElementCount(Board board, List<Color> colors) { return board.getNbCardsOfColor(colors); } }, WONDER_LEVEL { @Override - int getElementCount(Board board, List<Color> colors) { + public int getElementCount(Board board, List<Color> colors) { return board.getWonderLevel(); } }, DEFEAT_TOKEN { @Override - int getElementCount(Board board, List<Color> colors) { + public int getElementCount(Board board, List<Color> colors) { return board.getNbDefeatTokens(); } }; - abstract int getElementCount(Board board, List<Color> colors); + public abstract int getElementCount(Board board, List<Color> colors); } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/Provider.java b/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java index a586dcca..bacd1e73 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/Provider.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java @@ -1,4 +1,4 @@ -package org.luxons.sevenwonders.game.effects; +package org.luxons.sevenwonders.game.boards; public enum Provider { LEFT_PLAYER, RIGHT_PLAYER diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.java b/src/main/java/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.java new file mode 100644 index 00000000..16b2f3a9 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/boards/RelativeBoardPosition.java @@ -0,0 +1,28 @@ +package org.luxons.sevenwonders.game.boards; + +public enum RelativeBoardPosition { + LEFT { + @Override + public int getIndexFrom(int playerIndex, int nbPlayers) { + return wrapIndex(playerIndex - 1, nbPlayers); + } + }, + SELF { + @Override + public int getIndexFrom(int playerIndex, int nbPlayers) { + return playerIndex; + } + }, + RIGHT { + @Override + public int getIndexFrom(int playerIndex, int nbPlayers) { + return wrapIndex(playerIndex + 1, nbPlayers); + } + }; + + public abstract int getIndexFrom(int playerIndex, int nbPlayers); + + int wrapIndex(int index, int nbPlayers) { + return Math.floorMod(index, nbPlayers); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java b/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java index ac13c354..97b60ea0 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java @@ -3,7 +3,6 @@ package org.luxons.sevenwonders.game.boards; import java.util.EnumMap; import java.util.Map; -import org.luxons.sevenwonders.game.effects.Provider; import org.luxons.sevenwonders.game.resources.ResourceType; public class TradingRules { diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index fe160857..e2d1d940 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.cards; import java.util.List; +import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.effects.Effect; @@ -64,17 +65,19 @@ public class Card { return requirements.isAffordedBy(board); } - public boolean isPlayable(Board board, Board left, Board right) { - return !board.isPlayed(name) && (isChainableOn(board) || requirements.isAffordedBy(board, left, right)); + public boolean isPlayable(Table table, int playerIndex) { + Board board = table.getBoard(playerIndex); + return !board.isPlayed(name) && (isChainableOn(board) || requirements.isAffordedBy(table, playerIndex)); } - public void applyTo(Board board, Board left, Board right) { + public void applyTo(Table table, int playerIndex) { // TODO add paid resources cost deduction - if (!isChainableOn(board)) { + Board playerBoard = table.getBoard(playerIndex); + if (!isChainableOn(playerBoard)) { // TODO add paid resources exemption - requirements.pay(board); + requirements.pay(playerBoard); } - effects.forEach(e -> e.apply(board, left, right)); + effects.forEach(e -> e.apply(table, playerIndex)); } @Override diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java index 644eefd6..e729fedc 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.game.cards; +import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.resources.Resources; @@ -29,7 +30,8 @@ public class Requirements { return board.getGold() >= gold && board.getProduction().contains(resources); } - boolean isAffordedBy(Board board, Board left, Board right) { + boolean isAffordedBy(Table table, int playerIndex) { + Board board = table.getBoard(playerIndex); if (isAffordedBy(board)) { return true; } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java index a14411bd..5ab1a13f 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.game.data; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; import org.luxons.sevenwonders.game.Decks; import org.luxons.sevenwonders.game.Game; @@ -12,6 +12,7 @@ import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.data.definitions.DecksDefinition; import org.luxons.sevenwonders.game.data.definitions.WonderDefinition; +import org.luxons.sevenwonders.game.wonders.Wonder; public class GameDefinition { @@ -43,18 +44,22 @@ public class GameDefinition { } public Game initGame(long id, Settings settings, List<Player> orderedPlayers) { - List<Board> boards = pickRandomBoards(settings); + List<Board> boards = assignBoards(settings, orderedPlayers); Decks decks = decksDefinition.create(settings); return new Game(id, settings, orderedPlayers, boards, decks); } - private List<Board> pickRandomBoards(Settings settings) { + private List<Board> assignBoards(Settings settings, List<Player> orderedPlayers) { List<WonderDefinition> randomizedWonders = Arrays.asList(wonders); Collections.shuffle(randomizedWonders, settings.getRandom()); - return Arrays.stream(wonders) - .map(def -> def.create(settings)) - .map(w -> new Board(w, settings)) - .limit(settings.getNbPlayers()) - .collect(Collectors.toList()); + + List<Board> boards = new ArrayList<>(orderedPlayers.size()); + for (Player player : orderedPlayers) { + WonderDefinition def = randomizedWonders.remove(0); + Wonder w = def.create(settings); + Board b = new Board(w, player, settings); + boards.add(b); + } + return boards; } } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java b/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java index 37db3532..05794702 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java @@ -2,7 +2,10 @@ package org.luxons.sevenwonders.game.effects; import java.util.List; +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; public class BonusPerBoardElement implements Effect { @@ -59,28 +62,22 @@ public class BonusPerBoardElement implements Effect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - int goldGain = gold * computeNbOfMatchingElementsIn(board, leftNeighbourBoard, rightNeighbourBoard); + public void apply(Table table, int playerIndex) { + int goldGain = gold * computeNbOfMatchingElementsIn(table, playerIndex); + Board board = table.getBoard(playerIndex); board.setGold(board.getGold() + goldGain); } @Override - public int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - return points * computeNbOfMatchingElementsIn(board, leftNeighbourBoard, rightNeighbourBoard); + public int computePoints(Table table, int playerIndex) { + return points * computeNbOfMatchingElementsIn(table, playerIndex); } - private int computeNbOfMatchingElementsIn(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - int totalCount = 0; - if (boards.contains(RelativeBoardPosition.SELF)) { - totalCount += computeNbOfMatchingElementsIn(board); - } - if (boards.contains(RelativeBoardPosition.LEFT)) { - totalCount += computeNbOfMatchingElementsIn(leftNeighbourBoard); - } - if (boards.contains(RelativeBoardPosition.RIGHT)) { - totalCount += computeNbOfMatchingElementsIn(rightNeighbourBoard); - } - return totalCount; + private int computeNbOfMatchingElementsIn(Table table, int playerIndex) { + return boards.stream() + .map(pos -> table.getBoard(playerIndex, pos)) + .mapToInt(this::computeNbOfMatchingElementsIn) + .sum(); } private int computeNbOfMatchingElementsIn(Board board) { diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java index 9b5d071f..a943b027 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java @@ -4,10 +4,11 @@ import java.util.ArrayList; import java.util.List; import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.Provider; import org.luxons.sevenwonders.game.boards.TradingRules; import org.luxons.sevenwonders.game.resources.ResourceType; -public class Discount extends InstantEffect { +public class Discount extends InstantOwnBoardEffect { private final List<ResourceType> resourceTypes = new ArrayList<>(); @@ -32,7 +33,7 @@ public class Discount extends InstantEffect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Board board) { TradingRules rules = board.getTradingRules(); for (ResourceType type : resourceTypes) { for (Provider provider : providers) { diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/Effect.java b/src/main/java/org/luxons/sevenwonders/game/effects/Effect.java index 1d2189a2..692eaea0 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/Effect.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/Effect.java @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.effects; -import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.api.Table; /** * Represents an effect than can be applied to a player's board when playing a card or building his wonder. The effect @@ -9,7 +9,7 @@ import org.luxons.sevenwonders.game.boards.Board; */ public interface Effect { - void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard); + void apply(Table table, int playerIndex); - int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard); + int computePoints(Table table, int playerIndex); } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/EndGameEffect.java b/src/main/java/org/luxons/sevenwonders/game/effects/EndGameEffect.java index c2edc99b..1bae16a6 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/EndGameEffect.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/EndGameEffect.java @@ -1,11 +1,11 @@ package org.luxons.sevenwonders.game.effects; -import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.api.Table; public abstract class EndGameEffect implements Effect { @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Table table, int playerIndex) { // EndGameEffects don't do anything when applied to the board, they simply give more points in the end } } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/GoldIncrease.java b/src/main/java/org/luxons/sevenwonders/game/effects/GoldIncrease.java index 8a319ab6..5d4e5817 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/GoldIncrease.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/GoldIncrease.java @@ -4,7 +4,7 @@ import java.util.Objects; import org.luxons.sevenwonders.game.boards.Board; -public class GoldIncrease extends InstantEffect { +public class GoldIncrease extends InstantOwnBoardEffect { private final int amount; @@ -17,7 +17,7 @@ public class GoldIncrease extends InstantEffect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Board board) { board.setGold(board.getGold() + amount); } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/InstantEffect.java b/src/main/java/org/luxons/sevenwonders/game/effects/InstantEffect.java deleted file mode 100644 index e6e5aced..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/effects/InstantEffect.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.luxons.sevenwonders.game.effects; - -import org.luxons.sevenwonders.game.boards.Board; - -public abstract class InstantEffect implements Effect { - - @Override - public int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - // InstantEffects are only important when applied to the board, they don't give extra points in the end - return 0; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.java b/src/main/java/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.java new file mode 100644 index 00000000..79216c93 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/effects/InstantOwnBoardEffect.java @@ -0,0 +1,20 @@ +package org.luxons.sevenwonders.game.effects; + +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; + +public abstract class InstantOwnBoardEffect implements Effect { + + @Override + public void apply(Table table, int playerIndex) { + apply(table.getBoard(playerIndex)); + } + + public abstract void apply(Board board); + + @Override + public int computePoints(Table table, int playerIndex) { + // InstantEffects are only important when applied to the board, they don't give extra points in the end + return 0; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.java b/src/main/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.java index ad1497dd..8a0299ca 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcements.java @@ -4,7 +4,7 @@ import java.util.Objects; import org.luxons.sevenwonders.game.boards.Board; -public class MilitaryReinforcements extends InstantEffect { +public class MilitaryReinforcements extends InstantOwnBoardEffect { private final int count; @@ -17,7 +17,7 @@ public class MilitaryReinforcements extends InstantEffect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Board board) { board.setNbWarSymbols(board.getNbWarSymbols() + count); } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java b/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java index 22355a91..ff2a59c7 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java @@ -5,7 +5,7 @@ import java.util.Objects; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.resources.Production; -public class ProductionIncrease extends InstantEffect { +public class ProductionIncrease extends InstantOwnBoardEffect { private Production production = new Production(); @@ -17,7 +17,7 @@ public class ProductionIncrease extends InstantEffect { this.production = production; } - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Board board) { board.getProduction().addAll(production); } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/RawPointsIncrease.java b/src/main/java/org/luxons/sevenwonders/game/effects/RawPointsIncrease.java index 76ab30b0..0d117cec 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/RawPointsIncrease.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/RawPointsIncrease.java @@ -2,7 +2,7 @@ package org.luxons.sevenwonders.game.effects; import java.util.Objects; -import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.api.Table; public class RawPointsIncrease extends EndGameEffect { @@ -17,7 +17,7 @@ public class RawPointsIncrease extends EndGameEffect { } @Override - public int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public int computePoints(Table table, int playerIndex) { return points; } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/RelativeBoardPosition.java b/src/main/java/org/luxons/sevenwonders/game/effects/RelativeBoardPosition.java deleted file mode 100644 index 9b640539..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/effects/RelativeBoardPosition.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.luxons.sevenwonders.game.effects; - -public enum RelativeBoardPosition { - LEFT, SELF, RIGHT -} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java b/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java index 3eee7a9b..4e6764ee 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java @@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game.effects; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.Science; -public class ScienceProgress extends InstantEffect { +public class ScienceProgress extends InstantOwnBoardEffect { private Science science; @@ -16,7 +16,7 @@ public class ScienceProgress extends InstantEffect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public void apply(Board board) { board.getScience().addAll(science); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/SpecialActionTrigger.java b/src/main/java/org/luxons/sevenwonders/game/effects/SpecialActionTrigger.java index aef06741..22188a83 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/SpecialActionTrigger.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/SpecialActionTrigger.java @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.game.effects; -import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.api.Table; public class SpecialActionTrigger implements Effect { @@ -15,12 +15,12 @@ public class SpecialActionTrigger implements Effect { } @Override - public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - + public void apply(Table table, int playerIndex) { + // TODO do something to activate the special action } @Override - public int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + public int computePoints(Table table, int playerIndex) { return 0; } } diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java index 8f8d6a55..00b7d984 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java @@ -2,7 +2,7 @@ package org.luxons.sevenwonders.game.wonders; import java.util.List; -import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.Requirements; import org.luxons.sevenwonders.game.effects.Effect; @@ -28,7 +28,7 @@ public class WonderLevel { this.effects = effects; } - public void activate(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - effects.forEach(e -> e.apply(board, leftNeighbourBoard, rightNeighbourBoard)); + public void activate(Table table, int playerIndex) { + effects.forEach(e -> e.apply(table, playerIndex)); } } diff --git a/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java index ff044f4c..0ac96203 100644 --- a/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -17,7 +17,7 @@ public class BoardTest { @DataPoints public static int[] goldAmounts() { - return new int[]{-5, -1, 0, 1, 2, 5, 10}; + return new int[]{-5, -1, 0, 1, 2, 3, 5, 10}; } @DataPoints @@ -29,13 +29,13 @@ public class BoardTest { public void initialGold_respectsSettings(int goldAmountInSettings) { Settings settings = new Settings(); settings.setInitialGold(goldAmountInSettings); - Board board = new Board(TestUtils.createWonder(), settings); + Board board = new Board(TestUtils.createWonder(), null, settings); assertEquals(goldAmountInSettings, board.getGold()); } @Theory public void initialProduction_containsInitialResource(ResourceType type) { - Board board = new Board(TestUtils.createWonder(type), new Settings()); + Board board = new Board(TestUtils.createWonder(type), null, new Settings()); Resources resources = TestUtils.createResources(type); assertTrue(board.getProduction().contains(resources)); } diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java index cbd2fd71..351aee35 100644 --- a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -1,11 +1,13 @@ package org.luxons.sevenwonders.game.cards; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Test; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.effects.Effect; import org.luxons.sevenwonders.game.effects.ProductionIncrease; @@ -16,20 +18,19 @@ import static org.junit.Assert.assertEquals; public class CardTest { - private Board board; - - private Board leftBoard; - - private Board rightBoard; + private Table table; private Card treeFarmCard; @Before public void initBoard() { Settings settings = new Settings(); - board = new Board(new Wonder("TestWonder", ResourceType.WOOD), settings); - leftBoard = new Board(new Wonder("TestWonder", ResourceType.STONE), settings); - rightBoard = new Board(new Wonder("TestWonder", ResourceType.PAPYRUS), settings); + + List<Board> boards = new ArrayList<>(3); + boards.add(new Board(new Wonder("TestWonder", ResourceType.WOOD), null, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.STONE), null, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.PAPYRUS), null, settings)); + table = new Table(boards); Requirements treeFarmRequirements = new Requirements(); treeFarmRequirements.setGold(1); @@ -43,15 +44,13 @@ public class CardTest { } @Test - public void testInitialBoard() { - assertEquals(3, board.getGold()); - } - - @Test public void playCardCostingMoney() { - board.setGold(3); - treeFarmCard.applyTo(board, leftBoard, rightBoard); - assertEquals(2, board.getGold()); + table.getBoard(0).setGold(3); + table.getBoard(1).setGold(3); + table.getBoard(2).setGold(3); + treeFarmCard.applyTo(table, 0); + assertEquals(2, table.getBoard(0).getGold()); + assertEquals(3, table.getBoard(1).getGold()); + assertEquals(3, table.getBoard(2).getGold()); } - } diff --git a/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java b/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java index fdc4467e..f29eb286 100644 --- a/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java @@ -28,7 +28,7 @@ public class GoldIncreaseTest { Board board = TestUtils.createBoard(type, initialAmount); GoldIncrease goldIncrease = new GoldIncrease(goldIncreaseAmount); - goldIncrease.apply(board, null, null); + goldIncrease.apply(board); assertEquals(initialAmount + goldIncreaseAmount, board.getGold()); } diff --git a/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java b/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java index b5c9a2ee..0f95bea9 100644 --- a/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java @@ -30,7 +30,7 @@ public class MilitaryReinforcementsTest { MilitaryReinforcements reinforcements = new MilitaryReinforcements(additionalShields); - reinforcements.apply(board, null, null); + reinforcements.apply(board); assertEquals(initialShields + additionalShields, board.getNbWarSymbols()); } diff --git a/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java b/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java index d8ecf3ad..9ff35db3 100644 --- a/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java @@ -31,7 +31,7 @@ public class ProductionIncreaseTest { Board board = TestUtils.createBoard(initialType); ProductionIncrease effect = createProductionIncrease(addedType); - effect.apply(board, null, null); + effect.apply(board); Resources resources = TestUtils.createResources(initialType, addedType); assertTrue(board.getProduction().contains(resources)); diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index 1eebd3e4..2a0292cd 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -12,7 +12,7 @@ public class TestUtils { public static Board createBoard(ResourceType initialResource) { Settings settings = new Settings(); Wonder wonder = new Wonder("Test Wonder " + initialResource.getSymbol(), initialResource); - return new Board(wonder, settings); + return new Board(wonder, null, settings); } public static Board createBoard(ResourceType initialResource, ResourceType... production) { |