From 5c937e0fb48332f0a3e5114423359ccc9869154b Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Thu, 15 Dec 2016 00:41:18 +0100 Subject: Improve card requirements management - Add chain-related fields to the Card - Add helpers methods isAffordable, isPlayable and the likes - Remove the card addition to the board as it is supposed to be separately from applying the effect of the card --- .../org/luxons/sevenwonders/game/boards/Board.java | 4 +++ .../org/luxons/sevenwonders/game/cards/Card.java | 42 ++++++++++++++++++---- .../sevenwonders/game/cards/Requirements.java | 12 +++++-- .../game/data/definitions/CardDefinition.java | 2 +- .../luxons/sevenwonders/game/cards/CardTest.java | 6 ++-- 5 files changed, 52 insertions(+), 14 deletions(-) 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 d2fe5c7d..08bedb89 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -53,6 +53,10 @@ public class Board { return (int) playedCards.stream().filter(c -> colorFilter.contains(c.getColor())).count(); } + public boolean isPlayed(String cardName) { + return getPlayedCards().stream().map(Card::getName).filter(name -> name.equals(cardName)).count() > 0; + } + public Production getProduction() { return production; } 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 9840e260..cc2275a2 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -13,13 +13,20 @@ public class Card { private final Requirements requirements; + private final String chainParent; + private final List effects; - public Card(String name, Color color, Requirements requirements, List effects) { + private final List chainChildren; + + public Card(String name, Color color, Requirements requirements, String chainParent, List effects, + List chainChildren) { this.name = name; this.color = color; this.requirements = requirements; + this.chainParent = chainParent; this.effects = effects; + this.chainChildren = chainChildren; } public String getName() { @@ -30,6 +37,10 @@ public class Card { return color; } + public String getChainParent() { + return chainParent; + } + public Requirements getRequirements() { return requirements; } @@ -38,12 +49,29 @@ public class Card { return effects; } - public void play(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { - // adding the card must be done first, as some effects count the number of cards - // FIXME this is actually broken as ALL cards in the turn need to be added to the board before any effect - board.addCard(this); - requirements.pay(board); - effects.forEach(e -> e.apply(board, leftNeighbourBoard, rightNeighbourBoard)); + public List getChainChildren() { + return chainChildren; + } + + public boolean isChainableOn(Board board) { + return board.isPlayed(chainParent); + } + + public boolean isAffordedBy(Board board) { + 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 void applyTo(Board board, Board left, Board right) { + // TODO add paid resources cost deduction + if (!isChainableOn(board)) { + // TODO add paid resources exemption + requirements.pay(board); + } + effects.forEach(e -> e.apply(board, left, right)); } @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 99b8da69..644eefd6 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java @@ -25,11 +25,19 @@ public class Requirements { this.resources = resources; } - public boolean isAffordedBy(Board board) { + boolean isAffordedBy(Board board) { return board.getGold() >= gold && board.getProduction().contains(resources); } - public void pay(Board board) { + boolean isAffordedBy(Board board, Board left, Board right) { + if (isAffordedBy(board)) { + return true; + } + // TODO take into account resources buyable from neighbours + return false; + } + + void pay(Board board) { board.setGold(board.getGold() - gold); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java index a23db2fc..f46e4b4f 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java @@ -28,7 +28,7 @@ public class CardDefinition implements Definition { @Override public Card create(Settings settings) { - return new Card(name, color, requirements, effect.create(settings)); + return new Card(name, color, requirements, chainParent, effect.create(settings), chainChildren); } public String getName() { 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 572a7ee4..62bf376d 100644 --- a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -12,7 +12,6 @@ import org.luxons.sevenwonders.game.effects.ProductionIncrease; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.wonders.Wonder; -import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; public class CardTest { @@ -40,7 +39,7 @@ public class CardTest { List effects = Collections.singletonList(treeFarmEffect); - treeFarmCard = new Card("Tree Farm", Color.BROWN, treeFarmRequirements, effects); + treeFarmCard = new Card("Tree Farm", Color.BROWN, treeFarmRequirements, "", effects, null); } @Test @@ -51,9 +50,8 @@ public class CardTest { @Test public void playCardCostingMoney() { board.setGold(3); - treeFarmCard.play(board, leftBoard, rightBoard); + treeFarmCard.applyTo(board, leftBoard, rightBoard); assertEquals(2, board.getGold()); - assertTrue(board.getPlayedCards().contains(treeFarmCard)); } } -- cgit