diff options
6 files changed, 46 insertions, 38 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java b/backend/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java index dbaaad58..c6464da8 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/api/HandCard.java @@ -21,7 +21,7 @@ public class HandCard { Board board = table.getBoard(playerIndex); this.card = card; this.chainable = card.isChainableOn(board); - this.free = card.isAffordedBy(board) && card.getRequirements().getGold() == 0; + this.free = card.isFreeFor(board); this.playable = card.isPlayable(table, playerIndex); } diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index f0ed278e..da877f30 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -77,8 +77,8 @@ public class Card { return board.isPlayed(chainParent); } - public boolean isAffordedBy(Board board) { - return requirements.isAffordedBy(board); + public boolean isFreeFor(Board board) { + return requirements.areMetWithoutNeighboursBy(board) && requirements.getGold() == 0; } public boolean isPlayable(Table table, int playerIndex) { @@ -86,7 +86,7 @@ public class Card { if (board.isPlayed(name)) { return false; // cannot play twice the same card } - return isChainableOn(board) || requirements.couldBeAffordedBy(table, playerIndex); + return isChainableOn(board) || requirements.couldBeMetBy(table, playerIndex); } public void applyTo(Table table, int playerIndex, List<BoughtResources> boughtResources) { 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 e2a03767..93683ff8 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 @@ -32,62 +32,70 @@ public class Requirements { } /** - * Returns whether the given board can pay for these requirements on its own. + * Returns whether the given board meets these requirements on its own. * * @param board * the board to check * - * @return true if the given board fulfills these requirements without any transaction with its neighbours + * @return true if the given board meets these requirements without any transaction with its neighbours */ - boolean isAffordedBy(Board board) { + boolean areMetWithoutNeighboursBy(Board board) { return hasRequiredGold(board) && producesRequiredResources(board); } - boolean couldBeAffordedBy(Table table, int playerIndex) { - Board board = table.getBoard(playerIndex); - if (!hasRequiredGold(board)) { + /** + * Returns whether the given board meets these requirements, if the specified resources are bought from neighbours. + * + * @param board + * the board to check + * @param boughtResources + * the resources the player intends to buy + * + * @return true if the given board meets these requirements + */ + public boolean areMetWithHelpBy(Board board, List<BoughtResources> boughtResources) { + if (!hasRequiredGold(board, boughtResources)) { return false; } if (producesRequiredResources(board)) { return true; } - return BestPriceCalculator.bestPrice(resources, table, playerIndex) <= board.getGold(); + return producesRequiredResourcesWithHelp(board, boughtResources); } /** - * Returns whether the given player can pay for these requirements, if he buys the specified resources. + * Returns whether the given player's board could meet these requirements, on its own or by buying resources to + * neighbours. * - * @param board - * the board to check - * @param boughtResources - * the resources the player intends to buy + * @param table + * the current game table + * @param playerIndex + * the index of the player to check * - * @return true if the given board fulfills these requirements + * @return true if the given player's board could meet these requirements */ - public boolean isAffordedBy(Board board, List<BoughtResources> boughtResources) { + boolean couldBeMetBy(Table table, int playerIndex) { + Board board = table.getBoard(playerIndex); if (!hasRequiredGold(board)) { return false; } if (producesRequiredResources(board)) { return true; } - if (!canAffordBoughtResources(board, boughtResources)) { - return false; - } - return producesRequiredResourcesWithHelp(board, boughtResources); + return BestPriceCalculator.bestPrice(resources, table, playerIndex) <= board.getGold() - gold; } private boolean hasRequiredGold(Board board) { return board.getGold() >= gold; } - private boolean producesRequiredResources(Board board) { - return board.getProduction().contains(resources); + private boolean hasRequiredGold(Board board, List<BoughtResources> boughtResources) { + int resourcesPrice = board.getTradingRules().computeCost(boughtResources); + return board.getGold() >= gold + resourcesPrice; } - private boolean canAffordBoughtResources(Board board, List<BoughtResources> boughtResources) { - int resourcesPrice = board.getTradingRules().computeCost(boughtResources); - return board.getGold() - gold >= resourcesPrice; + private boolean producesRequiredResources(Board board) { + return board.getProduction().contains(resources); } private boolean producesRequiredResourcesWithHelp(Board board, List<BoughtResources> boughtResources) { diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java index 07022564..05f707c7 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java @@ -20,7 +20,7 @@ public class PlayCardMove extends CardFromHandMove { return false; } Board board = table.getBoard(getPlayerIndex()); - return getCard().getRequirements().isAffordedBy(board, getBoughtResources()); + return getCard().getRequirements().areMetWithHelpBy(board, getBoughtResources()); } @Override diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java b/backend/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java index af93d909..565b60d3 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java @@ -39,7 +39,7 @@ public class WonderStage { public boolean isBuildable(Table table, int playerIndex, List<BoughtResources> boughtResources) { Board board = table.getBoard(playerIndex); - return requirements.isAffordedBy(board, boughtResources); + return requirements.areMetWithHelpBy(board, boughtResources); } void build(CardBack cardBack) { 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 index b46a5bb9..797f14d3 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java @@ -36,9 +36,9 @@ public class RequirementsTest { 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(board, Collections.emptyList())); - assertEquals(boardGold >= requiredGold, requirements.couldBeAffordedBy(table, 0)); + assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, Collections.emptyList())); + assertEquals(boardGold >= requiredGold, requirements.couldBeMetBy(table, 0)); } @Theory @@ -48,11 +48,11 @@ public class RequirementsTest { Board board = TestUtils.createBoard(initialResource, 0); Table table = new Table(Collections.singletonList(board)); - assertEquals(initialResource == requiredResource, requirements.isAffordedBy(board)); - assertEquals(initialResource == requiredResource, requirements.isAffordedBy(board, Collections.emptyList())); + assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(initialResource == requiredResource, requirements.areMetWithHelpBy(board, Collections.emptyList())); if (initialResource == requiredResource) { - assertTrue(requirements.couldBeAffordedBy(table, 0)); + assertTrue(requirements.couldBeMetBy(table, 0)); } } @@ -67,11 +67,11 @@ public class RequirementsTest { board.getProduction().addFixedResource(producedResource, 1); Table table = new Table(Collections.singletonList(board)); - assertEquals(producedResource == requiredResource, requirements.isAffordedBy(board)); - assertEquals(producedResource == requiredResource, requirements.isAffordedBy(board, Collections.emptyList())); + assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(producedResource == requiredResource, requirements.areMetWithHelpBy(board, Collections.emptyList())); if (producedResource == requiredResource) { - assertTrue(requirements.couldBeAffordedBy(table, 0)); + assertTrue(requirements.couldBeMetBy(table, 0)); } } |