From 6a5b40ff51e480c1204551074aa738fe593ada3d Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 22 Dec 2016 18:45:48 +0100 Subject: Add CardBack for wonder stages display in the UI --- .../java/org/luxons/sevenwonders/game/Game.java | 11 ++--- .../org/luxons/sevenwonders/game/api/Table.java | 13 +++--- .../org/luxons/sevenwonders/game/boards/Board.java | 21 +++------ .../sevenwonders/game/boards/BoardElementType.java | 4 +- .../org/luxons/sevenwonders/game/cards/Card.java | 22 +++++++++- .../luxons/sevenwonders/game/cards/CardBack.java | 14 ++++++ .../sevenwonders/game/cards/Requirements.java | 14 +++++- .../game/data/definitions/DecksDefinition.java | 46 ++++++++++---------- .../game/data/definitions/WonderDefinition.java | 8 ++-- .../data/definitions/WonderLevelDefinition.java | 20 --------- .../data/definitions/WonderStageDefinition.java | 20 +++++++++ .../luxons/sevenwonders/game/wonders/Wonder.java | 42 +++++++++++++++--- .../sevenwonders/game/wonders/WonderLevel.java | 34 --------------- .../sevenwonders/game/wonders/WonderStage.java | 50 ++++++++++++++++++++++ 14 files changed, 199 insertions(+), 120 deletions(-) create mode 100644 src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderStageDefinition.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java (limited to 'src/main/java/org/luxons') diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index 1fab129c..50481103 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -147,15 +147,16 @@ public class Game { } private void placeCard(Move move) { + Card card = decks.getCard(move.getCardName()); switch (move.getType()) { case PLAY: - table.placeCard(move.getPlayerIndex(), decks.getCard(move.getCardName())); + table.placeCard(move.getPlayerIndex(), card); break; case UPGRADE_WONDER: - table.upgradeWonderStage(move.getPlayerIndex()); + table.buildWonderStage(move.getPlayerIndex(), card.getBack()); break; case DISCARD: - discardedCards.add(decks.getCard(move.getCardName())); + discardedCards.add(card); break; } } @@ -173,10 +174,10 @@ public class Game { private void activateCard(Move move) { switch (move.getType()) { case PLAY: - table.activateCard(move.getPlayerIndex(), decks.getCard(move.getCardName())); + table.activateCard(move.getPlayerIndex(), decks.getCard(move.getCardName()), move.getBoughtResources()); break; case UPGRADE_WONDER: - table.activateCurrentWonderStage(move.getPlayerIndex()); + table.activateCurrentWonderStage(move.getPlayerIndex(), move.getBoughtResources()); break; case DISCARD: table.discard(move.getPlayerIndex(), settings.getDiscardedCardGold()); 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 2ef27a2a..d906a5f1 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java @@ -5,6 +5,7 @@ import java.util.List; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.CardBack; /** * The table contains what is visible by all the players in the game: the boards and their played cards, and the @@ -52,18 +53,18 @@ public class Table { board.addCard(card); } - public void upgradeWonderStage(int playerIndex) { + public void buildWonderStage(int playerIndex, CardBack cardBack) { Board board = boards.get(playerIndex); - board.increaseWonderLevel(); + board.buildWonderStage(cardBack); } - public void activateCard(int playerIndex, Card card) { - card.applyTo(this, playerIndex); + public void activateCard(int playerIndex, Card card, List boughtResources) { + card.applyTo(this, playerIndex, boughtResources); } - public void activateCurrentWonderStage(int playerIndex) { + public void activateCurrentWonderStage(int playerIndex, List boughtResources) { Board board = boards.get(playerIndex); - board.activateCurrentWonderLevel(this, playerIndex); + board.activateCurrentWonderLevel(this, playerIndex, boughtResources); } public void discard(int playerIndex, int goldBonus) { 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 c509ee3b..7e310979 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -5,8 +5,10 @@ import java.util.List; import org.luxons.sevenwonders.game.Player; import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.resources.Production; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -27,8 +29,6 @@ public class Board { private int gold; - private int wonderLevel; - private int nbWarSymbols; private int nbDefeatTokens; @@ -36,7 +36,6 @@ public class Board { 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()); production.addFixedResource(wonder.getInitialResource(), 1); @@ -86,20 +85,12 @@ public class Board { this.gold = amount; } - public int getWonderLevel() { - return wonderLevel; - } - - public void increaseWonderLevel() { - int maxLevel = wonder.getLevels().size(); - if (maxLevel == wonderLevel) { - throw new IllegalStateException("This wonder has already reached its maximum level"); - } - this.wonderLevel++; + public void buildWonderStage(CardBack cardBack) { + wonder.buildLevel(cardBack); } - public void activateCurrentWonderLevel(Table table, int playerIndex) { - wonder.getLevels().get(wonderLevel).activate(table, playerIndex); + public void activateCurrentWonderLevel(Table table, int playerIndex, List boughtResources) { + wonder.activateLastBuiltStage(table, playerIndex, boughtResources); } public int getNbWarSymbols() { diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java b/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java index 5c029331..d1fba60c 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java @@ -11,10 +11,10 @@ public enum BoardElementType { return board.getNbCardsOfColor(colors); } }, - WONDER_LEVEL { + BUILT_WONDER_STAGES { @Override public int getElementCount(Board board, List colors) { - return board.getWonderLevel(); + return board.getWonder().getNbBuiltStages(); } }, DEFEAT_TOKEN { 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 148d7ed0..94c46839 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.game.cards; import java.util.List; import java.util.Objects; +import org.luxons.sevenwonders.game.api.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.effects.Effect; @@ -23,6 +24,8 @@ public class Card { private final String image; + private CardBack back; + public Card(String name, Color color, Requirements requirements, List effects, String chainParent, List chainChildren, String image) { this.name = name; @@ -58,6 +61,18 @@ public class Card { return chainChildren; } + public String getImage() { + return image; + } + + public CardBack getBack() { + return back; + } + + public void setBack(CardBack back) { + this.back = back; + } + public boolean isChainableOn(Board board) { return board.isPlayed(chainParent); } @@ -68,10 +83,13 @@ public class Card { public boolean isPlayable(Table table, int playerIndex) { Board board = table.getBoard(playerIndex); - return !board.isPlayed(name) && (isChainableOn(board) || requirements.isAffordedBy(table, playerIndex)); + if (board.isPlayed(name)) { + return false; // cannot play twice the same card + } + return isChainableOn(board) || requirements.isAffordedBy(table, playerIndex); } - public void applyTo(Table table, int playerIndex) { + public void applyTo(Table table, int playerIndex, List boughtResources) { // TODO add paid resources cost deduction Board playerBoard = table.getBoard(playerIndex); if (!isChainableOn(playerBoard)) { diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java b/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java new file mode 100644 index 00000000..f925b6c4 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java @@ -0,0 +1,14 @@ +package org.luxons.sevenwonders.game.cards; + +public class CardBack { + + private final String image; + + public CardBack(String image) { + this.image = image; + } + + public String getImage() { + return image; + } +} 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 e729fedc..714f86a6 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,8 @@ package org.luxons.sevenwonders.game.cards; +import java.util.List; + +import org.luxons.sevenwonders.game.api.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.resources.Resources; @@ -30,7 +33,16 @@ public class Requirements { return board.getGold() >= gold && board.getProduction().contains(resources); } - boolean isAffordedBy(Table table, int playerIndex) { + public boolean isAffordedBy(Table table, int playerIndex) { + Board board = table.getBoard(playerIndex); + if (isAffordedBy(board)) { + return true; + } + // TODO take into account resources buyable from neighbours + return false; + } + + public boolean isAffordedBy(Table table, int playerIndex, List boughtResources) { Board board = table.getBoard(playerIndex); if (isAffordedBy(board)) { return true; diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java index e50b6214..c95977f2 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java @@ -9,6 +9,7 @@ import java.util.stream.Collectors; import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.cards.Decks; public class DecksDefinition implements Definition { @@ -19,58 +20,55 @@ public class DecksDefinition implements Definition { private List age3; - private List guildCards; + private String age1Back; - public List getAge1() { - return age1; - } + private String age2Back; - public List getAge2() { - return age2; - } + private String age3Back; - public List getAge3() { - return age3; - } - - public List getGuildCards() { - return guildCards; - } + private List guildCards; @Override public Decks create(Settings settings) { Map> cardsPerAge = new HashMap<>(); - cardsPerAge.put(1, prepareStandardDeck(age1, settings)); - cardsPerAge.put(2, prepareStandardDeck(age2, settings)); + cardsPerAge.put(1, prepareStandardDeck(age1, settings, age1Back)); + cardsPerAge.put(2, prepareStandardDeck(age2, settings, age2Back)); cardsPerAge.put(3, prepareAge3Deck(settings)); return new Decks(cardsPerAge); } - private static List prepareStandardDeck(List defs, Settings settings) { - List cards = createDeck(defs, settings); + private static List prepareStandardDeck(List defs, Settings settings, String backImage) { + CardBack back = new CardBack(backImage); + List cards = createDeck(defs, settings, back); Collections.shuffle(cards, settings.getRandom()); return cards; } private List prepareAge3Deck(Settings settings) { - List age3deck = createDeck(age3, settings); - age3deck.addAll(createGuildCards(guildCards, settings)); + CardBack back = new CardBack(age3Back); + List age3deck = createDeck(age3, settings, back); + age3deck.addAll(createGuildCards(settings, back)); Collections.shuffle(age3deck, settings.getRandom()); return age3deck; } - private static List createDeck(List defs, Settings settings) { + private static List createDeck(List defs, Settings settings, CardBack back) { List cards = new ArrayList<>(); for (CardDefinition def : defs) { for (int i = 0; i < def.getCountPerNbPlayer().get(settings.getNbPlayers()); i++) { - cards.add(def.create(settings)); + Card card = def.create(settings); + card.setBack(back); + cards.add(card); } } return cards; } - private static List createGuildCards(List defs, Settings settings) { - List guild = defs.stream().map((def) -> def.create(settings)).collect(Collectors.toList()); + private List createGuildCards(Settings settings, CardBack back) { + List guild = guildCards.stream() + .map((def) -> def.create(settings)) + .peek(c -> c.setBack(back)) + .collect(Collectors.toList()); Collections.shuffle(guild, settings.getRandom()); return guild.subList(0, settings.getNbPlayers() + 2); } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java index 576f2bc2..4d0f411d 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java @@ -6,7 +6,7 @@ import java.util.stream.Collectors; import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.wonders.Wonder; -import org.luxons.sevenwonders.game.wonders.WonderLevel; +import org.luxons.sevenwonders.game.wonders.WonderStage; public class WonderDefinition implements Definition { @@ -23,7 +23,7 @@ public class WonderDefinition implements Definition { WonderSideDefinition wonderSideDef = pickSide(settings); wonder.setInitialResource(wonderSideDef.getInitialResource()); - wonder.setLevels(wonderSideDef.createStages(settings)); + wonder.setStages(wonderSideDef.createStages(settings)); wonder.setImage(wonderSideDef.getImage()); return wonder; } @@ -42,7 +42,7 @@ public class WonderDefinition implements Definition { private ResourceType initialResource; - private List stages; + private List stages; private String image; @@ -50,7 +50,7 @@ public class WonderDefinition implements Definition { return initialResource; } - List createStages(Settings settings) { + List createStages(Settings settings) { return stages.stream().map(def -> def.create(settings)).collect(Collectors.toList()); } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java deleted file mode 100644 index 6064734c..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.luxons.sevenwonders.game.data.definitions; - -import org.luxons.sevenwonders.game.Settings; -import org.luxons.sevenwonders.game.cards.Requirements; -import org.luxons.sevenwonders.game.wonders.WonderLevel; - -public class WonderLevelDefinition implements Definition { - - private Requirements requirements; - - private EffectsDefinition effects; - - @Override - public WonderLevel create(Settings settings) { - WonderLevel level = new WonderLevel(); - level.setRequirements(requirements); - level.setEffects(effects.create(settings)); - return level; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderStageDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderStageDefinition.java new file mode 100644 index 00000000..7a866e05 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderStageDefinition.java @@ -0,0 +1,20 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.wonders.WonderStage; + +public class WonderStageDefinition implements Definition { + + private Requirements requirements; + + private EffectsDefinition effects; + + @Override + public WonderStage create(Settings settings) { + WonderStage stage = new WonderStage(); + stage.setRequirements(requirements); + stage.setEffects(effects.create(settings)); + return stage; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java index b5698508..d739354a 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java @@ -3,6 +3,9 @@ package org.luxons.sevenwonders.game.wonders; import java.util.Arrays; import java.util.List; +import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.resources.ResourceType; public class Wonder { @@ -11,17 +14,17 @@ public class Wonder { private ResourceType initialResource; - private List levels; + private List stages; private String image; public Wonder() { } - public Wonder(String name, ResourceType initialResource, WonderLevel... levels) { + public Wonder(String name, ResourceType initialResource, WonderStage... stages) { this.name = name; this.initialResource = initialResource; - this.levels = Arrays.asList(levels); + this.stages = Arrays.asList(stages); } public String getName() { @@ -40,12 +43,16 @@ public class Wonder { this.initialResource = initialResource; } - public List getLevels() { - return levels; + public List getStages() { + return stages; } - public void setLevels(List levels) { - this.levels = levels; + public void setStages(List stages) { + this.stages = stages; + } + + public int getNbBuiltStages() { + return (int)stages.stream().filter(WonderStage::isBuilt).count(); } public String getImage() { @@ -55,4 +62,25 @@ public class Wonder { public void setImage(String image) { this.image = image; } + + public void buildLevel(CardBack cardBack) { + getNextStage().build(cardBack); + } + + private WonderStage getNextStage() { + int nextLevel = getNbBuiltStages(); + if (nextLevel == stages.size()) { + throw new IllegalStateException("This wonder has already reached its maximum level"); + } + return stages.get(nextLevel); + } + + public void activateLastBuiltStage(Table table, int playerIndex, List boughtResources) { + getLastBuiltStage().activate(table, playerIndex, boughtResources); + } + + private WonderStage getLastBuiltStage() { + int lastLevel = getNbBuiltStages() - 1; + return stages.get(lastLevel); + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java deleted file mode 100644 index 00b7d984..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.luxons.sevenwonders.game.wonders; - -import java.util.List; - -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.cards.Requirements; -import org.luxons.sevenwonders.game.effects.Effect; - -public class WonderLevel { - - private Requirements requirements; - - private List effects; - - public Requirements getRequirements() { - return requirements; - } - - public void setRequirements(Requirements requirements) { - this.requirements = requirements; - } - - public List getEffects() { - return effects; - } - - public void setEffects(List effects) { - this.effects = effects; - } - - public void activate(Table table, int playerIndex) { - effects.forEach(e -> e.apply(table, playerIndex)); - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java new file mode 100644 index 00000000..00a53fb1 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.wonders; + +import java.util.List; + +import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.CardBack; +import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.effects.Effect; + +public class WonderStage { + + private Requirements requirements; + + private List effects; + + private CardBack cardBack; + + public Requirements getRequirements() { + return requirements; + } + + public void setRequirements(Requirements requirements) { + this.requirements = requirements; + } + + public List getEffects() { + return effects; + } + + public void setEffects(List effects) { + this.effects = effects; + } + + public boolean isBuilt() { + return cardBack != null; + } + + public boolean isBuildable(Table table, int playerIndex, List boughtResources) { + return requirements.isAffordedBy(table, playerIndex, boughtResources); + } + + void build(CardBack cardBack) { + this.cardBack = cardBack; + } + + void activate(Table table, int playerIndex, List boughtResources) { + effects.forEach(e -> e.apply(table, playerIndex)); + } +} -- cgit