diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-31 01:44:19 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-31 01:44:35 +0100 |
commit | 1511fdbec64329ab1563fab73be4eb09d7885ab5 (patch) | |
tree | e0ed972e03241cce1eb09829dcf9c78273d67c87 /backend/src/main/java | |
parent | Use specific seed for randomness in tests for determinism (diff) | |
download | seven-wonders-1511fdbec64329ab1563fab73be4eb09d7885ab5.tar.gz seven-wonders-1511fdbec64329ab1563fab73be4eb09d7885ab5.tar.bz2 seven-wonders-1511fdbec64329ab1563fab73be4eb09d7885ab5.zip |
Add first tests for Requirements
Diffstat (limited to 'backend/src/main/java')
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java index f6d7934c..942b64b7 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java @@ -31,15 +31,15 @@ public class Requirements { } boolean isAffordedBy(Board board) { - return board.getGold() >= gold && board.getProduction().contains(resources); + return hasRequiredGold(board) && producesRequiredResources(board); } - public boolean couldBeAffordedBy(Table table, int playerIndex) { + boolean couldBeAffordedBy(Table table, int playerIndex) { Board board = table.getBoard(playerIndex); - if (board.getGold() < gold) { + if (!hasRequiredGold(board)) { return false; } - if (board.getProduction().contains(resources)) { + if (producesRequiredResources(board)) { return true; } Resources leftToPay = resources.minus(board.getProduction().getFixedResources()); @@ -49,19 +49,38 @@ public class Requirements { public boolean isAffordedBy(Table table, int playerIndex, List<BoughtResources> boughtResources) { Board board = table.getBoard(playerIndex); - if (isAffordedBy(board)) { + if (!hasRequiredGold(board)) { + return false; + } + if (producesRequiredResources(board)) { return true; } - int totalPrice = board.getTradingRules().computeCost(boughtResources); - if (board.getGold() < totalPrice) { + if (!canAffordBoughtResources(board, boughtResources)) { return false; } + return producesRequiredResourcesWithHelp(board, boughtResources); + } + + private boolean hasRequiredGold(Board board) { + return board.getGold() >= gold; + } + + private boolean producesRequiredResources(Board board) { + return board.getProduction().contains(resources); + } + + private boolean canAffordBoughtResources(Board board, List<BoughtResources> boughtResources) { + int resourcesPrice = board.getTradingRules().computeCost(boughtResources); + return board.getGold() - gold >= resourcesPrice; + } + + private boolean producesRequiredResourcesWithHelp(Board board, List<BoughtResources> boughtResources) { Resources totalBoughtResources = getTotalResources(boughtResources); Resources remainingResources = this.resources.minus(totalBoughtResources); return board.getProduction().contains(remainingResources); } - private Resources getTotalResources(List<BoughtResources> boughtResources) { + private static Resources getTotalResources(List<BoughtResources> boughtResources) { return boughtResources.stream().map(BoughtResources::getResources).reduce(new Resources(), (r1, r2) -> { r1.addAll(r2); return r1; |