diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-04-16 19:39:57 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-04-16 19:39:57 +0200 |
commit | d697ab4facb295e3555d884250d010ceb7b8fc74 (patch) | |
tree | 3222cd5751e431280bd6e0509f52563ac834af67 | |
parent | Reword confusing exception message (diff) | |
download | seven-wonders-d697ab4facb295e3555d884250d010ceb7b8fc74.tar.gz seven-wonders-d697ab4facb295e3555d884250d010ceb7b8fc74.tar.bz2 seven-wonders-d697ab4facb295e3555d884250d010ceb7b8fc74.zip |
Simplify Requirements.isAffordedBy() signature
4 files changed, 28 insertions, 9 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 942b64b7..d0444233 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 @@ -30,6 +30,14 @@ public class Requirements { this.resources = resources; } + /** + * Returns whether the given board can pay for 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 + */ boolean isAffordedBy(Board board) { return hasRequiredGold(board) && producesRequiredResources(board); } @@ -44,11 +52,20 @@ public class Requirements { } Resources leftToPay = resources.minus(board.getProduction().getFixedResources()); // TODO take into account resources buyable from neighbours - return true; + return false; } - public boolean isAffordedBy(Table table, int playerIndex, List<BoughtResources> boughtResources) { - Board board = table.getBoard(playerIndex); + /** + * Returns whether the given player can pay for these requirements, if he buys the specified resources. + * + * @param board + * the board to check + * @param boughtResources + * the resources the player intends to buy + * + * @return true if the given board fulfills these requirements + */ + public boolean isAffordedBy(Board board, List<BoughtResources> boughtResources) { if (!hasRequiredGold(board)) { return false; } 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 affebc4a..07022564 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 @@ -19,7 +19,8 @@ public class PlayCardMove extends CardFromHandMove { if (!super.isValid(table, playerHand)) { return false; } - return getCard().getRequirements().isAffordedBy(table, getPlayerIndex(), getBoughtResources()); + Board board = table.getBoard(getPlayerIndex()); + return getCard().getRequirements().isAffordedBy(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 fc3fc573..af93d909 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 @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.game.wonders; import java.util.List; import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.cards.Requirements; import org.luxons.sevenwonders.game.effects.Effect; @@ -37,7 +38,8 @@ public class WonderStage { } public boolean isBuildable(Table table, int playerIndex, List<BoughtResources> boughtResources) { - return requirements.isAffordedBy(table, playerIndex, boughtResources); + Board board = table.getBoard(playerIndex); + return requirements.isAffordedBy(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 a6e5151d..db8a016a 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 @@ -38,7 +38,7 @@ public class RequirementsTest { 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.isAffordedBy(board, Collections.emptyList())); assertEquals(boardGold >= requiredGold, requirements.couldBeAffordedBy(table, 0)); } @@ -52,7 +52,7 @@ public class RequirementsTest { Table table = new Table(Collections.singletonList(board)); assertEquals(initialResource == requiredResource, requirements.isAffordedBy(board)); - assertEquals(initialResource == requiredResource, requirements.isAffordedBy(table, 0, Collections.emptyList())); + assertEquals(initialResource == requiredResource, requirements.isAffordedBy(board, Collections.emptyList())); if (initialResource == requiredResource) { assertTrue(requirements.couldBeAffordedBy(table, 0)); @@ -73,8 +73,7 @@ public class RequirementsTest { Table table = new Table(Collections.singletonList(board)); assertEquals(producedResource == requiredResource, requirements.isAffordedBy(board)); - assertEquals(producedResource == requiredResource, - requirements.isAffordedBy(table, 0, Collections.emptyList())); + assertEquals(producedResource == requiredResource, requirements.isAffordedBy(board, Collections.emptyList())); if (producedResource == requiredResource) { assertTrue(requirements.couldBeAffordedBy(table, 0)); |