diff --git a/src/main/java/it/unibo/controller/api/MainController.java b/src/main/java/it/unibo/controller/api/MainController.java index b73b12d..5ae4a19 100644 --- a/src/main/java/it/unibo/controller/api/MainController.java +++ b/src/main/java/it/unibo/controller/api/MainController.java @@ -162,6 +162,15 @@ void tradeWithPlayer(String proposer, String accepter, Map rollDie(); - void tradeWithBank(String proposer, Map proposedResouces, + /** + * Modify the resources of the owners into the trade (player and bank). + * + * @param proposer is the player that propose the trade + * @param proposedResources are the resources that the proposer give to the + * bank + * @param wantedResources are the resources that the bank give to the + * proposer + */ + void tradeWithBank(String proposer, Map proposedResources, Map wantedResources); } diff --git a/src/main/java/it/unibo/controller/api/ResourceController.java b/src/main/java/it/unibo/controller/api/ResourceController.java index 811b82b..9b63920 100644 --- a/src/main/java/it/unibo/controller/api/ResourceController.java +++ b/src/main/java/it/unibo/controller/api/ResourceController.java @@ -29,10 +29,10 @@ public interface ResourceController { boolean hasResources(String owner, Map resources); /** - * Modify the resources of the owners into the trade. + * Modify the resources of the players into the trade. * - * @param proposer is the owner that propose the trade - * @param accepter is the owner that accept the trade + * @param proposer is the player that propose the trade + * @param accepter is the player that accept the trade * @param proposedResouces are the resources that the proposer give to the * accepter * @param wantedResources are the resources that the accepter give to the @@ -42,13 +42,15 @@ void tradeWithPlayer(String proposer, String accepter, Map wantedResources); /** + * Modify the resources of the owners into the trade (player and bank). * - * @param proposer - * @param accepter - * @param proposedResouces - * @param wantedResources + * @param proposer is the player that propose the trade + * @param proposedResources are the resources that the proposer give to the + * bank + * @param wantedResources are the resources that the bank give to the + * proposer */ - void tradeWithBank(String proposer, Map proposedResouces, + void tradeWithBank(String proposer, Map proposedResources, Map wantedResources); /** @@ -65,13 +67,15 @@ void tradeWithBank(String proposer, Map proposedResouces, */ int getResourcesToDiscard(int amount); - /*** + /** * - * @param proposer - * @param accepter - * @param proposedResouces - * @param wantedResources - * @return + * @param proposer is the player that propose the trade + * @param accepter is the player that accept the trade + * @param proposedResouces are the resources that the proposer give to the + * accepter + * @param wantedResources are the resources that the accepter give to the + * proposer + * @return wheter the two player involved in the trade have the resources. */ boolean canTradeWithPlayer(String proposer, String accepter, Map proposedResouces, @@ -79,26 +83,29 @@ boolean canTradeWithPlayer(String proposer, String accepter, /*** * - * @param proposer - * @param proposedResouces - * @param wantedResources - * @return + * @param proposer is the player that propose the trade + * @param proposedResouces are the resources that the proposer want to give to + * the + * bank + * @param wantedResources are the resources that the bank want to give to the + * proposer + * @return wheter the proposer and the bank have the resources. */ boolean canTradeWithBank(String proposer, Map proposedResouces, Map wantedResources); /** * - * @param proposer - * @param amount + * @param player player that have to discard + * @param discardResources map of the resources * @return whether the player proposer can discard the amount. */ - boolean canDiscard(String proposer, Map discardResources); + boolean canDiscard(String player, Map discardResources); /** * - * @param playerName - * @return whether the player shold discard cards. + * @param player + * @return whether the player should discard cards. */ - boolean shouldDiscard(String playerName); + boolean shouldDiscard(String player); } diff --git a/src/main/java/it/unibo/controller/impl/MainControllerImpl.java b/src/main/java/it/unibo/controller/impl/MainControllerImpl.java index 478ca7a..d5db1ed 100644 --- a/src/main/java/it/unibo/controller/impl/MainControllerImpl.java +++ b/src/main/java/it/unibo/controller/impl/MainControllerImpl.java @@ -30,6 +30,7 @@ public final class MainControllerImpl implements MainController { private final TurnController turnController; private final AppView appView; private boolean mustPlaceRobber; + private static final int ROBBER_NUMBER = 7; /** * Constructor of the controller. @@ -99,7 +100,7 @@ public void buildRoad(final RoadPosition position) { @Override public void buyCard() { - CardType card = gameManager.buyCard(turnController.getCurrentPlayerTurn()); + final CardType card = gameManager.buyCard(turnController.getCurrentPlayerTurn()); if (card.equals(CardType.KNIGHT)) { mustPlaceRobber = true; } @@ -184,14 +185,14 @@ public boolean mustPlaceRobber() { public Pair rollDie() { final Pair rolledDies = turnController.rollDie(); produceResources(rolledDies.getLeft() + rolledDies.getRight()); - mustPlaceRobber = (rolledDies.getLeft() + rolledDies.getRight()) == 7; + mustPlaceRobber = rolledDies.getLeft() + rolledDies.getRight() == ROBBER_NUMBER; return rolledDies; } @Override - public void tradeWithBank(String proposer, Map proposedResouces, - Map wantedResources) { - this.resourceController.tradeWithBank(proposer, proposedResouces, wantedResources); + public void tradeWithBank(final String proposer, final Map proposedResources, + final Map wantedResources) { + this.resourceController.tradeWithBank(proposer, proposedResources, wantedResources); redrawResourcesView(); } diff --git a/src/main/java/it/unibo/controller/impl/ResourceControllerImpl.java b/src/main/java/it/unibo/controller/impl/ResourceControllerImpl.java index 40925c9..2a20c20 100644 --- a/src/main/java/it/unibo/controller/impl/ResourceControllerImpl.java +++ b/src/main/java/it/unibo/controller/impl/ResourceControllerImpl.java @@ -50,11 +50,11 @@ public Map getBankResources() { @Override public void tradeWithPlayer(final String proposer, final String accepter, - final Map givingResouces, - final Map recivingResources) { + final Map proposedResources, + final Map wantedResources) { resourceManager.trade(getPlayerByName.apply(proposer), getPlayerByName.apply(accepter), - givingResouces, - recivingResources); + proposedResources, + wantedResources); } @Override @@ -63,40 +63,40 @@ public int getResourcesAmount(final String owner) { } @Override - public boolean canTradeWithPlayer(String proposer, String accepter, - Map proposedResouces, Map wantedResources) { + public boolean canTradeWithPlayer(final String proposer, final String accepter, + final Map proposedResources, final Map wantedResources) { return resourceManager.canTrade(getPlayerByName.apply(proposer), getPlayerByName.apply(accepter), - proposedResouces, wantedResources); + proposedResources, wantedResources); } @Override - public boolean canTradeWithBank(String proposer, Map proposedResouces, - Map wantedResources) { + public boolean canTradeWithBank(final String proposer, final Map proposedResources, + final Map wantedResources) { return resourceManager.canTrade(getPlayerByName.apply(proposer), resourceManager.getBank(), - proposedResouces, + proposedResources, wantedResources); } @Override - public int getResourcesToDiscard(int amount) { + public int getResourcesToDiscard(final int amount) { return resourceManager.getResourcesToDiscard(amount); } @Override - public boolean canDiscard(String proposer, Map discardResources) { + public boolean canDiscard(final String proposer, final Map discardResources) { return resourceManager.canDiscard(getPlayerByName.apply(proposer), discardResources.values().stream().mapToInt(Integer::intValue).sum()); } @Override - public boolean shouldDiscard(String playerName) { + public boolean shouldDiscard(final String playerName) { return resourceManager.shouldDiscard(getPlayerByName.apply(playerName)); } @Override - public void tradeWithBank(String proposer, Map proposedResouces, - Map wantedResources) { - resourceManager.trade(getPlayerByName.apply(proposer), resourceManager.getBank(), proposedResouces, + public void tradeWithBank(final String proposer, final Map proposedResources, + final Map wantedResources) { + resourceManager.trade(getPlayerByName.apply(proposer), resourceManager.getBank(), proposedResources, wantedResources); } diff --git a/src/main/java/it/unibo/model/api/GameManager.java b/src/main/java/it/unibo/model/api/GameManager.java index 87fd423..ab66dfe 100644 --- a/src/main/java/it/unibo/model/api/GameManager.java +++ b/src/main/java/it/unibo/model/api/GameManager.java @@ -45,6 +45,7 @@ public interface GameManager { * Player {@code player} buys a development card. * * @param player the player who wants to buy a card + * @return return the card */ CardType buyCard(Player player); diff --git a/src/main/java/it/unibo/model/impl/GameManagerImpl.java b/src/main/java/it/unibo/model/impl/GameManagerImpl.java index 13dce04..2ad247f 100644 --- a/src/main/java/it/unibo/model/impl/GameManagerImpl.java +++ b/src/main/java/it/unibo/model/impl/GameManagerImpl.java @@ -28,6 +28,7 @@ */ public final class GameManagerImpl implements GameManager { private static final int DEFAULT_POINTS_TO_WIN = 10; + private static final int DEFAULT_BANK_RESOURCES = 19; private final DevelopmentCards developmentCards; private final PropertyManager propertyManager; @@ -64,7 +65,7 @@ public GameManagerImpl(final GameMapGenerator generator, final List play * @see GameManagerImpl#GameManagerImpl(GameMapGenerator, List, int) */ public GameManagerImpl(final List playersNames) { - this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, 19); + this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, DEFAULT_BANK_RESOURCES); } @Override @@ -164,8 +165,8 @@ public CardType buyCard(final Player player) { .forEach((resource, amount) -> resourceManager.addResources(player, resource, amount)); break; case MONOPOLY: - Random random = new Random(); - ResourceType selectedType = List.of(ResourceType.values()) + final Random random = new Random(); + final ResourceType selectedType = List.of(ResourceType.values()) .get(random.nextInt(ResourceType.values().length)); this.players.stream().filter(p -> !p.equals(player)).forEach(p -> { resourceManager.addResources(player, selectedType, resourceManager.getResource(p, selectedType)); @@ -201,13 +202,12 @@ public boolean canBuildCity(final PropertyPosition position, final Player player @Override public boolean canBuildRoad(final RoadPosition position, final Player player) { - final int cycle = turnManager.getCycle(); if (!roadManager.canBuildRoad(position)) { return false; } - if (cycle <= 2) { + if (turnManager.getCycle() <= 2) { return isRoadNearToAnyPlayerProperty(position, player) - && this.roadManager.getPlayerRoads(player).size() < cycle; + && this.roadManager.getPlayerRoads(player).size() < turnManager.getCycle(); } return (isRoadNearToAnyPlayerRoad(position, player) || isRoadNearToAnyPlayerProperty(position, player)) && resourceManager.hasResources(player, Recipes.getRoadResources()); diff --git a/src/main/java/it/unibo/model/impl/ResourceManagerImpl.java b/src/main/java/it/unibo/model/impl/ResourceManagerImpl.java index 33d7650..6ab6b55 100644 --- a/src/main/java/it/unibo/model/impl/ResourceManagerImpl.java +++ b/src/main/java/it/unibo/model/impl/ResourceManagerImpl.java @@ -92,8 +92,9 @@ public Map getResources(final ResourceOwner owner) { } @Override - public boolean canTrade(ResourceOwner proposer, ResourceOwner accepter, Map proposedResouces, - Map wantedResources) { + public boolean canTrade(final ResourceOwner proposer, final ResourceOwner accepter, + final Map proposedResouces, + final Map wantedResources) { /** * */ @@ -120,7 +121,7 @@ private void initializeResourceMap(final Map map) { } @Override - public int getResourcesToDiscard(int amount) { + public int getResourcesToDiscard(final int amount) { if (amount <= DISCARD_THRESHOLD) { return 0; } @@ -128,18 +129,18 @@ public int getResourcesToDiscard(int amount) { } @Override - public boolean canDiscard(ResourceOwner proposer, int amount) { + public boolean canDiscard(final ResourceOwner proposer, final int amount) { return amount == getResourcesToDiscard(getResourcesAmount(proposer)); } @Override - public int getResourcesAmount(ResourceOwner owner) { + public int getResourcesAmount(final ResourceOwner owner) { return getResources(owner).values().stream().mapToInt(Integer::intValue).sum(); } @Override - public boolean shouldDiscard(ResourceOwner playerName) { - return getResourcesAmount(playerName) > DISCARD_THRESHOLD; + public boolean shouldDiscard(final ResourceOwner player) { + return getResourcesAmount(player) > DISCARD_THRESHOLD; } } diff --git a/src/main/java/it/unibo/model/impl/RoadManagerImpl.java b/src/main/java/it/unibo/model/impl/RoadManagerImpl.java index cdbb125..4f37038 100644 --- a/src/main/java/it/unibo/model/impl/RoadManagerImpl.java +++ b/src/main/java/it/unibo/model/impl/RoadManagerImpl.java @@ -76,7 +76,7 @@ public int getLongestRoadLength(final Player player) { .forEach(nearPosition -> graph.addEdge(propertyPos, nearPosition))); final AllDirectedPaths allPaths = new AllDirectedPaths<>(graph); - return allPaths.getAllPaths(propertyPositions, propertyPositions, true, 20).stream() + return allPaths.getAllPaths(propertyPositions, propertyPositions, true, null).stream() .mapToInt(graphPath -> graphPath.getLength()).reduce(0, Integer::max); } @@ -95,7 +95,7 @@ public Optional getLongestRoadOwner() { private void checkLongestRoadOwner(final Player player) { final int minimumLongestRoadLength = 5; final int longestRoadVictoryPoints = 2; - if ((longestRoadOwner.isEmpty() && getLongestRoadLength(player) >= minimumLongestRoadLength) + if (longestRoadOwner.isEmpty() && getLongestRoadLength(player) >= minimumLongestRoadLength || (longestRoadOwner.isPresent() && !player.equals(longestRoadOwner.get()) && getLongestRoadLength(player) > getLongestRoadLength(longestRoadOwner.get()))) { if (longestRoadOwner.isPresent()) { diff --git a/src/main/java/it/unibo/view/ResourcesViewFactory.java b/src/main/java/it/unibo/view/ResourcesViewFactory.java index 84884bb..f3fc4a5 100644 --- a/src/main/java/it/unibo/view/ResourcesViewFactory.java +++ b/src/main/java/it/unibo/view/ResourcesViewFactory.java @@ -56,6 +56,14 @@ public static VBox getResourceLabelAmount(final ResourceType resource, final int return resourceAndAmount; } + /** + * + * @param resource resource type. + * @param amount amount of resource. + * @param listener action to perform on change. + * @return the image view of the needed resource with a combobox representing + * the amout. + */ public static VBox resourceAndComboBox(final ResourceType resource, final int amount, final ChangeListener listener) { final VBox resourceBox = new VBox(); @@ -68,6 +76,11 @@ public static VBox resourceAndComboBox(final ResourceType resource, final int am return resourceBox; } + /** + * + * @param text text to put on the header. + * @return an alert with the text on the header. + */ public static Alert getAlert(final String text) { final Alert alert = new Alert(AlertType.CONFIRMATION); alert.setHeaderText(text); diff --git a/src/main/java/it/unibo/view/TradeView.java b/src/main/java/it/unibo/view/TradeView.java index 00bdcab..7181a62 100644 --- a/src/main/java/it/unibo/view/TradeView.java +++ b/src/main/java/it/unibo/view/TradeView.java @@ -63,8 +63,8 @@ private void showTradeStage() { List.of(ResourceType.values()).forEach(resource -> wantedResources.put(resource, 0)); final Map proposedResources = new HashMap<>(); List.of(ResourceType.values()).forEach(resource -> proposedResources.put(resource, 0)); - Map buttonToAction = new HashMap<>(); - + final Map buttonToAction = new HashMap<>(); + final Runnable reloadBankTradeButton = getBankTradeButtonAction(tradeBank, proposedResources, wantedResources); final Map playerToButton = new HashMap<>(); controller.getPlayerNames().stream().filter(playerName -> !playerName.equals(controller.getCurrentPlayer())) .forEach(playerName -> { @@ -76,13 +76,10 @@ private void showTradeStage() { }); playerToButton.put(playerName, acceptTradeButton); buttonToAction.put(acceptTradeButton, - setTradePlayerButton(acceptTradeButton, playerName, proposedResources, wantedResources)); + getPlayerTradeButtonAction(acceptTradeButton, playerName, proposedResources, + wantedResources)); }); - final Runnable reloadBankTradeButton = () -> { - tradeBank.setDisable(!controller.getResourceController().canTradeWithBank(controller.getCurrentPlayer(), - proposedResources, wantedResources)); - }; reloadBankTradeButton.run(); buttonToAction.forEach((button, action) -> action.run()); Stream.of(ResourceType.values()).forEach(resource -> { @@ -130,7 +127,7 @@ private void showTradeStage() { stage.show(); } - private Runnable setTradePlayerButton(final Button button, final String playerName, + private Runnable getPlayerTradeButtonAction(final Button button, final String playerName, final Map proposedResources, final Map wantedResources) { return () -> { @@ -138,4 +135,13 @@ private Runnable setTradePlayerButton(final Button button, final String playerNa controller.getCurrentPlayer(), playerName, proposedResources, wantedResources)); }; } + + private Runnable getBankTradeButtonAction(final Button tradeBank, + final Map proposedResources, + final Map wantedResources) { + return () -> { + tradeBank.setDisable(!controller.getResourceController().canTradeWithBank(controller.getCurrentPlayer(), + proposedResources, wantedResources)); + }; + } }