summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-22 18:45:48 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-22 18:45:48 +0100
commit6a5b40ff51e480c1204551074aa738fe593ada3d (patch)
tree3988adb2240f0fa8f4a12df08f7371d6eec3961b /src/main/java/org
parentAdd Hands.createHand() test (diff)
downloadseven-wonders-6a5b40ff51e480c1204551074aa738fe593ada3d.tar.gz
seven-wonders-6a5b40ff51e480c1204551074aa738fe593ada3d.tar.bz2
seven-wonders-6a5b40ff51e480c1204551074aa738fe593ada3d.zip
Add CardBack for wonder stages display in the UI
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java11
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/api/Table.java13
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Board.java21
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/BoardElementType.java4
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/cards/Card.java22
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java14
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java14
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java46
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java8
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java20
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderStageDefinition.java20
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java42
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java (renamed from src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java)20
13 files changed, 167 insertions, 88 deletions
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> boughtResources) {
+ card.applyTo(this, playerIndex, boughtResources);
}
- public void activateCurrentWonderStage(int playerIndex) {
+ public void activateCurrentWonderStage(int playerIndex, List<BoughtResources> 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> 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<Color> 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<Effect> effects, String chainParent,
List<String> 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> 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> 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<Decks> {
@@ -19,58 +20,55 @@ public class DecksDefinition implements Definition<Decks> {
private List<CardDefinition> age3;
- private List<CardDefinition> guildCards;
+ private String age1Back;
- public List<CardDefinition> getAge1() {
- return age1;
- }
+ private String age2Back;
- public List<CardDefinition> getAge2() {
- return age2;
- }
+ private String age3Back;
- public List<CardDefinition> getAge3() {
- return age3;
- }
-
- public List<CardDefinition> getGuildCards() {
- return guildCards;
- }
+ private List<CardDefinition> guildCards;
@Override
public Decks create(Settings settings) {
Map<Integer, List<Card>> 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<Card> prepareStandardDeck(List<CardDefinition> defs, Settings settings) {
- List<Card> cards = createDeck(defs, settings);
+ private static List<Card> prepareStandardDeck(List<CardDefinition> defs, Settings settings, String backImage) {
+ CardBack back = new CardBack(backImage);
+ List<Card> cards = createDeck(defs, settings, back);
Collections.shuffle(cards, settings.getRandom());
return cards;
}
private List<Card> prepareAge3Deck(Settings settings) {
- List<Card> age3deck = createDeck(age3, settings);
- age3deck.addAll(createGuildCards(guildCards, settings));
+ CardBack back = new CardBack(age3Back);
+ List<Card> age3deck = createDeck(age3, settings, back);
+ age3deck.addAll(createGuildCards(settings, back));
Collections.shuffle(age3deck, settings.getRandom());
return age3deck;
}
- private static List<Card> createDeck(List<CardDefinition> defs, Settings settings) {
+ private static List<Card> createDeck(List<CardDefinition> defs, Settings settings, CardBack back) {
List<Card> 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<Card> createGuildCards(List<CardDefinition> defs, Settings settings) {
- List<Card> guild = defs.stream().map((def) -> def.create(settings)).collect(Collectors.toList());
+ private List<Card> createGuildCards(Settings settings, CardBack back) {
+ List<Card> 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<Wonder> {
@@ -23,7 +23,7 @@ public class WonderDefinition implements Definition<Wonder> {
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<Wonder> {
private ResourceType initialResource;
- private List<WonderLevelDefinition> stages;
+ private List<WonderStageDefinition> stages;
private String image;
@@ -50,7 +50,7 @@ public class WonderDefinition implements Definition<Wonder> {
return initialResource;
}
- List<WonderLevel> createStages(Settings settings) {
+ List<WonderStage> 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<WonderLevel> {
-
- 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<WonderStage> {
+
+ 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<WonderLevel> levels;
+ private List<WonderStage> 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<WonderLevel> getLevels() {
- return levels;
+ public List<WonderStage> getStages() {
+ return stages;
}
- public void setLevels(List<WonderLevel> levels) {
- this.levels = levels;
+ public void setStages(List<WonderStage> 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> 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/WonderStage.java
index 00b7d984..00a53fb1 100644
--- a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java
+++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java
@@ -2,16 +2,20 @@ 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 WonderLevel {
+public class WonderStage {
private Requirements requirements;
private List<Effect> effects;
+ private CardBack cardBack;
+
public Requirements getRequirements() {
return requirements;
}
@@ -28,7 +32,19 @@ public class WonderLevel {
this.effects = effects;
}
- public void activate(Table table, int playerIndex) {
+ public boolean isBuilt() {
+ return cardBack != null;
+ }
+
+ public boolean isBuildable(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ return requirements.isAffordedBy(table, playerIndex, boughtResources);
+ }
+
+ void build(CardBack cardBack) {
+ this.cardBack = cardBack;
+ }
+
+ void activate(Table table, int playerIndex, List<BoughtResources> boughtResources) {
effects.forEach(e -> e.apply(table, playerIndex));
}
}
bgstack15