From 624a81b87ba9cdb21b603575bcf1e58137a0960a Mon Sep 17 00:00:00 2001 From: Mattia Ronchi Date: Fri, 16 Feb 2024 17:33:56 +0100 Subject: [PATCH 1/3] added private methods for improving readability in MainControllerImpl --- .../controller/main/MainControllerImpl.java | 161 ++++++++++-------- 1 file changed, 94 insertions(+), 67 deletions(-) diff --git a/src/main/java/it/unibo/controller/main/MainControllerImpl.java b/src/main/java/it/unibo/controller/main/MainControllerImpl.java index 242684d..5da1868 100644 --- a/src/main/java/it/unibo/controller/main/MainControllerImpl.java +++ b/src/main/java/it/unibo/controller/main/MainControllerImpl.java @@ -57,35 +57,17 @@ public MainControllerImpl(final AppView appView, final List players) { } @Override - public BoardController getBoardController() { - return this.boardController; - } - - @Override - public ResourceController getResourceController() { - return this.resourceController; - } - - @Override - public List getPlayerNames() { - return gameManager.getPlayers().stream().map(p -> p.getName()).toList(); - } - - @Override - public String getCurrentPlayerName() { - return this.turnController.getCurrentPlayer().getName(); - } - - @Override - public void buildSettlement(final PropertyPosition position) { - this.gameManager.buildSettlement(position, turnController.getCurrentPlayer()); + public void buildCity(final PropertyPosition position) { + gameManager.buildCity(position, turnController.getCurrentPlayer()); + checkGameOver(); redrawResourcesView(); this.appView.redrawPlayers(); } @Override - public void buildCity(final PropertyPosition position) { - gameManager.buildCity(position, turnController.getCurrentPlayer()); + public void buildSettlement(final PropertyPosition position) { + this.gameManager.buildSettlement(position, turnController.getCurrentPlayer()); + checkGameOver(); redrawResourcesView(); this.appView.redrawPlayers(); } @@ -93,6 +75,7 @@ public void buildCity(final PropertyPosition position) { @Override public void buildRoad(final RoadPosition position) { gameManager.buildRoad(position, turnController.getCurrentPlayer()); + checkGameOver(); redrawResourcesView(); this.appView.redrawPlayers(); } @@ -100,7 +83,8 @@ public void buildRoad(final RoadPosition position) { @Override public void buyCard() { final CardType card = gameManager.buyCard(turnController.getCurrentPlayer()); - appView.updateLog(getCurrentPlayerName(), createCardMessage(card)); + logCard(card); + checkGameOver(); if (card.equals(CardType.KNIGHT)) { mustPlaceRobber = true; } @@ -109,13 +93,13 @@ public void buyCard() { } @Override - public boolean canBuildSettlement(final PropertyPosition position) { - return !mustPlaceRobber() && gameManager.canBuildSettlement(position, turnController.getCurrentPlayer()); + public boolean canBuildCity(final PropertyPosition position) { + return !mustPlaceRobber() && gameManager.canBuildCity(position, turnController.getCurrentPlayer()); } @Override - public boolean canBuildCity(final PropertyPosition position) { - return !mustPlaceRobber() && gameManager.canBuildCity(position, turnController.getCurrentPlayer()); + public boolean canBuildSettlement(final PropertyPosition position) { + return !mustPlaceRobber() && gameManager.canBuildSettlement(position, turnController.getCurrentPlayer()); } @Override @@ -123,6 +107,12 @@ public boolean canBuildRoad(final RoadPosition position) { return !mustPlaceRobber() && gameManager.canBuildRoad(position, turnController.getCurrentPlayer()); } + @Override + public boolean canBuyCard() { + return turnController.hasRolled() && !mustPlaceRobber() + && this.gameManager.canBuyCard(turnController.getCurrentPlayer()); + } + @Override public int getPlayerPoints(final String player) { return getPlayerByName.apply(player).getVictoryPoints(); @@ -134,28 +124,28 @@ public boolean canEndTurn() { } @Override - public boolean canBuyCard() { - return turnController.hasRolled() && !mustPlaceRobber() - && this.gameManager.canBuyCard(turnController.getCurrentPlayer()); + public void setRobberPosition(final TilePosition coordinates) { + this.boardController.setRobberPosition(coordinates); + this.mustPlaceRobber = false; + this.appView.redrawCurrentPlayer(); } @Override - public boolean canRollDie() { - return !turnController.hasRolled() && turnController.getCycle() > 2; // + public boolean mustPlaceRobber() { + return mustPlaceRobber; } - /** - * give the resources produced by the tiles with the given number. - * - * @param rollSum sum of the 2 dices. - */ - private void produceResources(final int number) { - final Map> producedResources = gameManager.produceResources(number); - producedResources.forEach((player, resources) -> { - resources.entrySet().stream().filter(entry -> entry.getValue() > 0) - .forEach(e -> appView.updateLog(player.getName(), createResourceMessage(e.getKey(), e.getValue()))); - }); - redrawResourcesView(); + @Override + public Pair rollDie() { + final Pair rolledDies = turnController.rollDie(); + produceResources(rolledDies.getLeft() + rolledDies.getRight()); + mustPlaceRobber = rolledDies.getLeft() + rolledDies.getRight() == ROBBER_NUMBER; + return rolledDies; + } + + @Override + public boolean canRollDie() { + return !turnController.hasRolled() && turnController.getCycle() > 2; // } @Override @@ -167,54 +157,91 @@ public void tradeWithPlayer(final String proposer, final String accepter, redrawResourcesView(); } + @Override + public void tradeWithBank(final String proposer, final Map proposedResources, + final Map wantedResources) { + this.resourceController.tradeWithBank(proposer, proposedResources, wantedResources); + redrawResourcesView(); + } + @Override public boolean canStartTrade() { return !mustPlaceRobber() && turnController.getCycle() > 2 && turnController.hasRolled(); } @Override - public void setRobberPosition(final TilePosition coordinates) { - this.boardController.setRobberPosition(coordinates); - this.mustPlaceRobber = false; - this.appView.redrawCurrentPlayer(); + public void endTurn() { + turnController.endTurn(); } @Override - public boolean mustPlaceRobber() { - return mustPlaceRobber; + public BoardController getBoardController() { + return this.boardController; } @Override - public Pair rollDie() { - final Pair rolledDies = turnController.rollDie(); - produceResources(rolledDies.getLeft() + rolledDies.getRight()); - mustPlaceRobber = rolledDies.getLeft() + rolledDies.getRight() == ROBBER_NUMBER; - return rolledDies; + public ResourceController getResourceController() { + return this.resourceController; } @Override - public void tradeWithBank(final String proposer, final Map proposedResources, - final Map wantedResources) { - this.resourceController.tradeWithBank(proposer, proposedResources, wantedResources); - redrawResourcesView(); - + public List getPlayerNames() { + return gameManager.getPlayers().stream().map(p -> p.getName()).toList(); } @Override - public void endTurn() { - turnController.endTurn(); + public String getCurrentPlayerName() { + return this.turnController.getCurrentPlayer().getName(); } + /** + * Redraw the view of the resources. + */ private void redrawResourcesView() { this.appView.redrawCurrentPlayer(); this.appView.redrawBank(); } - private String createCardMessage(CardType card) { - return "used " + card.toString().toLowerCase(Locale.US).replace("_", " ") + " card"; + /** + * Checks whether the game is over. + * If so, updates the view and show the end of + * the game. + */ + private void checkGameOver() { + if (gameManager.isGameOver()) { + // TODO: draw end game + } } - private String createResourceMessage(ResourceType resource, int amount) { + /** + * Makes each tile with number {@code number} produce its resources and log the + * results. + * + * @param number number rolled + */ + private void produceResources(final int number) { + final Map> producedResources = gameManager.produceResources(number); + logResources(producedResources); + redrawResourcesView(); + } + + private void logResources(final Map> producedResources) { + producedResources.forEach((player, resources) -> { + resources.entrySet().stream().filter(entry -> entry.getValue() > 0) + .forEach(e -> appView.updateLog(player.getName(), + getResourcesLogMessage(e.getKey(), e.getValue()))); + }); + } + + private String getResourcesLogMessage(final ResourceType resource, final int amount) { return "got " + amount + " " + resource.toString().toLowerCase(Locale.US); } + + private void logCard(final CardType card) { + appView.updateLog(getCurrentPlayerName(), getCardLogMessage(card)); + } + + private String getCardLogMessage(final CardType card) { + return "used " + card.toString().toLowerCase(Locale.US).replace("_", " ") + " card"; + } } From dd9a700bd1f164d6802e6cc29b620a583160e89d Mon Sep 17 00:00:00 2001 From: Mattia Ronchi Date: Fri, 16 Feb 2024 17:35:04 +0100 Subject: [PATCH 2/3] added @throws to javadoc --- src/main/java/it/unibo/model/GameManager.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/it/unibo/model/GameManager.java b/src/main/java/it/unibo/model/GameManager.java index aa86ae9..2cc995d 100644 --- a/src/main/java/it/unibo/model/GameManager.java +++ b/src/main/java/it/unibo/model/GameManager.java @@ -28,8 +28,9 @@ public interface GameManager { /** * Build a settlement of player {@code player} at position {@code position}. * - * @param position where to build the settlement. - * @param player the player who wants to build the settlement. + * @param position + * @param player + * @throws IllegalArgumentException if {@link #canBuildSettlement} fails */ void buildSettlement(PropertyPosition position, Player player); @@ -38,14 +39,16 @@ public interface GameManager { * * @param player * @param position + * @throws IllegalArgumentException if {@link #canBuildCity} fails */ void buildCity(PropertyPosition position, Player player); /** * Build a road of player {@code player} at position {@code position}. * - * @param position where to build the road - * @param player the player who wants to build the road + * @param position + * @param player + * @throws IllegalArgumentException if {@link #canBuildRoad} fails */ void buildRoad(RoadPosition position, Player player); @@ -93,10 +96,12 @@ public interface GameManager { boolean canEndTurn(); /** - * Make each tile with number {@code number} produce its resource. + * Makes each tile with number {@code number} produce its resource. * It automatically updates the resources of each player. * * @param number + * @return a map from each player to the resources produced by the tiles near + * its properties */ Map> produceResources(int number); From eec0b9aca87e679b1d2e10acf3b3989b0d836a53 Mon Sep 17 00:00:00 2001 From: Mattia Ronchi Date: Fri, 16 Feb 2024 17:39:47 +0100 Subject: [PATCH 3/3] reordered methods order in MainController --- .../unibo/controller/main/MainController.java | 105 +++++++++--------- .../controller/main/MainControllerImpl.java | 10 +- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/src/main/java/it/unibo/controller/main/MainController.java b/src/main/java/it/unibo/controller/main/MainController.java index a1ddf00..90b9dc6 100644 --- a/src/main/java/it/unibo/controller/main/MainController.java +++ b/src/main/java/it/unibo/controller/main/MainController.java @@ -16,20 +16,13 @@ * Main controller. */ public interface MainController { - /** - * @return the board controller - */ - BoardController getBoardController(); - - /** - * @return the resource controller - */ - ResourceController getResourceController(); /** - * @return the list of the players' names + * Build a city in the given position. + * + * @param position where to build the city. */ - List getPlayerNames(); + void buildCity(PropertyPosition position); /** * Build a settlement in the given position. @@ -38,13 +31,6 @@ public interface MainController { */ void buildSettlement(PropertyPosition position); - /** - * Build a city in the given position. - * - * @param position where to build the city. - */ - void buildCity(PropertyPosition position); - /** * Build a road in the given position. * @@ -57,18 +43,18 @@ public interface MainController { */ void buyCard(); - /** - * @return whether the current player can build a settlement - * @param position where to build the settlement - */ - boolean canBuildSettlement(PropertyPosition position); - /** * @return whether the current player can build a city * @param position where to build the city */ boolean canBuildCity(PropertyPosition position); + /** + * @return whether the current player can build a settlement + * @param position where to build the settlement + */ + boolean canBuildSettlement(PropertyPosition position); + /** * @return whether the current player can build a road * @param position where to build the rode @@ -81,27 +67,29 @@ public interface MainController { boolean canBuyCard(); /** - * @return get the name of the player currently playing. + * @return whether the current player can end the turn */ - String getCurrentPlayerName(); + boolean canEndTurn(); /** - * get the points of the specified player. + * Set the robber in the specified position, removing it from the previous + * location. * - * @param player the player - * @return the points of the player + * @param coordinates coordinates of the new robber's position */ - int getPlayerPoints(String player); + void setRobberPosition(TilePosition coordinates); /** - * @return whether the current player can end the turn + * + * @return true if the player must place the robber. */ - boolean canEndTurn(); + boolean mustPlaceRobber(); /** - * @return whether the current player can start a trade. + * + * @return the dice roll. */ - boolean canStartTrade(); + Pair rollDie(); /** * @@ -123,18 +111,21 @@ void tradeWithPlayer(String proposer, String accepter, Map wantedResources); /** - * Set the robber in the specified position, removing it from the previous - * location. + * Modify the resources of the owners into the trade (player and bank). * - * @param coordinates coordinates of the new robber's position + * @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 setRobberPosition(TilePosition coordinates); + void tradeWithBank(String proposer, Map proposedResources, + Map wantedResources); /** - * - * @return true if the player must place the robber. + * @return whether the current player can start a trade. */ - boolean mustPlaceRobber(); + boolean canStartTrade(); /** * end the current turn and updates the current player. @@ -142,20 +133,30 @@ void tradeWithPlayer(String proposer, String accepter, Map rollDie(); + BoardController getBoardController(); /** - * Modify the resources of the owners into the trade (player and bank). + * @return the resource controller + */ + ResourceController getResourceController(); + + /** + * @return the list of the players' names + */ + List getPlayerNames(); + + /** + * @return get the name of the player currently playing. + */ + String getCurrentPlayerName(); + + /** + * get the points of the specified player. * - * @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 + * @param player the player + * @return the points of the player */ - void tradeWithBank(String proposer, Map proposedResources, - Map wantedResources); + int getPlayerPoints(String player); } diff --git a/src/main/java/it/unibo/controller/main/MainControllerImpl.java b/src/main/java/it/unibo/controller/main/MainControllerImpl.java index 5da1868..41f58a8 100644 --- a/src/main/java/it/unibo/controller/main/MainControllerImpl.java +++ b/src/main/java/it/unibo/controller/main/MainControllerImpl.java @@ -113,11 +113,6 @@ public boolean canBuyCard() { && this.gameManager.canBuyCard(turnController.getCurrentPlayer()); } - @Override - public int getPlayerPoints(final String player) { - return getPlayerByName.apply(player).getVictoryPoints(); - } - @Override public boolean canEndTurn() { return !mustPlaceRobber() && gameManager.canEndTurn(); @@ -194,6 +189,11 @@ public String getCurrentPlayerName() { return this.turnController.getCurrentPlayer().getName(); } + @Override + public int getPlayerPoints(final String player) { + return getPlayerByName.apply(player).getVictoryPoints(); + } + /** * Redraw the view of the resources. */