diff options
Diffstat (limited to 'backend/src')
-rw-r--r-- | backend/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java | 35 | ||||
-rw-r--r-- | backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java | 82 |
2 files changed, 109 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; diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java new file mode 100644 index 00000000..d851ae30 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java @@ -0,0 +1,82 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.Collections; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class RequirementsTest { + + @DataPoints + public static int[] goldAmounts() { + return new int[] {0, 1, 2, 5}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Theory + public void goldRequirement(int boardGold, int requiredGold) { + Requirements requirements = new Requirements(); + requirements.setGold(requiredGold); + + Board board = TestUtils.createBoard(ResourceType.CLAY, boardGold); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(boardGold >= requiredGold, requirements.isAffordedBy(board)); + assertEquals(boardGold >= requiredGold, requirements.isAffordedBy(table, 0, Collections.emptyList())); + assertEquals(boardGold >= requiredGold, requirements.couldBeAffordedBy(table, 0)); + } + + @Theory + public void resourceRequirement_initialResource(ResourceType initialResource, ResourceType requiredResource) { + Resources resources = TestUtils.createResources(requiredResource); + Requirements requirements = new Requirements(); + requirements.setResources(resources); + + Board board = TestUtils.createBoard(initialResource, 0); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(initialResource == requiredResource, requirements.isAffordedBy(board)); + assertEquals(initialResource == requiredResource, requirements.isAffordedBy(table, 0, Collections.emptyList())); + + if (initialResource == requiredResource) { + assertTrue(requirements.couldBeAffordedBy(table, 0)); + } + } + + @Theory + public void resourceRequirement_ownProduction(ResourceType initialResource, ResourceType producedResource, ResourceType requiredResource) { + assumeTrue(initialResource != requiredResource); + + Resources resources = TestUtils.createResources(requiredResource); + Requirements requirements = new Requirements(); + requirements.setResources(resources); + + Board board = TestUtils.createBoard(initialResource, 0); + board.getProduction().addFixedResource(producedResource, 1); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(producedResource == requiredResource, requirements.isAffordedBy(board)); + assertEquals(producedResource == requiredResource, requirements.isAffordedBy(table, 0, Collections.emptyList())); + + if (producedResource == requiredResource) { + assertTrue(requirements.couldBeAffordedBy(table, 0)); + } + } + +}
\ No newline at end of file |