Skip to content

Commit

Permalink
fixed some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andsam0 committed Feb 16, 2024
1 parent 8fc30a1 commit 866e784
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 69 deletions.
11 changes: 10 additions & 1 deletion src/main/java/it/unibo/controller/api/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ void tradeWithPlayer(String proposer, String accepter, Map<ResourceType, Integer
*/
Pair<Integer, Integer> rollDie();

void tradeWithBank(String proposer, Map<ResourceType, Integer> 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<ResourceType, Integer> proposedResources,
Map<ResourceType, Integer> wantedResources);
}
55 changes: 31 additions & 24 deletions src/main/java/it/unibo/controller/api/ResourceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public interface ResourceController {
boolean hasResources(String owner, Map<ResourceType, Integer> 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
Expand All @@ -42,13 +42,15 @@ void tradeWithPlayer(String proposer, String accepter, Map<ResourceType, Integer
Map<ResourceType, Integer> 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<ResourceType, Integer> proposedResouces,
void tradeWithBank(String proposer, Map<ResourceType, Integer> proposedResources,
Map<ResourceType, Integer> wantedResources);

/**
Expand All @@ -65,40 +67,45 @@ void tradeWithBank(String proposer, Map<ResourceType, Integer> 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<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> wantedResources);

/***
*
* @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<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> 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<ResourceType, Integer> discardResources);
boolean canDiscard(String player, Map<ResourceType, Integer> 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);
}
11 changes: 6 additions & 5 deletions src/main/java/it/unibo/controller/impl/MainControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -184,14 +185,14 @@ public boolean mustPlaceRobber() {
public Pair<Integer, Integer> rollDie() {
final Pair<Integer, Integer> 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<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> wantedResources) {
this.resourceController.tradeWithBank(proposer, proposedResouces, wantedResources);
public void tradeWithBank(final String proposer, final Map<ResourceType, Integer> proposedResources,
final Map<ResourceType, Integer> wantedResources) {
this.resourceController.tradeWithBank(proposer, proposedResources, wantedResources);
redrawResourcesView();

}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/it/unibo/controller/impl/ResourceControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public Map<ResourceType, Integer> getBankResources() {

@Override
public void tradeWithPlayer(final String proposer, final String accepter,
final Map<ResourceType, Integer> givingResouces,
final Map<ResourceType, Integer> recivingResources) {
final Map<ResourceType, Integer> proposedResources,
final Map<ResourceType, Integer> wantedResources) {
resourceManager.trade(getPlayerByName.apply(proposer), getPlayerByName.apply(accepter),
givingResouces,
recivingResources);
proposedResources,
wantedResources);
}

@Override
Expand All @@ -63,40 +63,40 @@ public int getResourcesAmount(final String owner) {
}

@Override
public boolean canTradeWithPlayer(String proposer, String accepter,
Map<ResourceType, Integer> proposedResouces, Map<ResourceType, Integer> wantedResources) {
public boolean canTradeWithPlayer(final String proposer, final String accepter,
final Map<ResourceType, Integer> proposedResources, final Map<ResourceType, Integer> wantedResources) {
return resourceManager.canTrade(getPlayerByName.apply(proposer), getPlayerByName.apply(accepter),
proposedResouces, wantedResources);
proposedResources, wantedResources);
}

@Override
public boolean canTradeWithBank(String proposer, Map<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> wantedResources) {
public boolean canTradeWithBank(final String proposer, final Map<ResourceType, Integer> proposedResources,
final Map<ResourceType, Integer> 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<ResourceType, Integer> discardResources) {
public boolean canDiscard(final String proposer, final Map<ResourceType, Integer> 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<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> wantedResources) {
resourceManager.trade(getPlayerByName.apply(proposer), resourceManager.getBank(), proposedResouces,
public void tradeWithBank(final String proposer, final Map<ResourceType, Integer> proposedResources,
final Map<ResourceType, Integer> wantedResources) {
resourceManager.trade(getPlayerByName.apply(proposer), resourceManager.getBank(), proposedResources,
wantedResources);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/it/unibo/model/api/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/it/unibo/model/impl/GameManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public GameManagerImpl(final GameMapGenerator generator, final List<String> play
* @see GameManagerImpl#GameManagerImpl(GameMapGenerator, List, int)
*/
public GameManagerImpl(final List<String> playersNames) {
this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, 19);
this(new RandomGameMapGenerator(), playersNames, DEFAULT_POINTS_TO_WIN, DEFAULT_BANK_RESOURCES);
}

@Override
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/it/unibo/model/impl/ResourceManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ public Map<ResourceType, Integer> getResources(final ResourceOwner owner) {
}

@Override
public boolean canTrade(ResourceOwner proposer, ResourceOwner accepter, Map<ResourceType, Integer> proposedResouces,
Map<ResourceType, Integer> wantedResources) {
public boolean canTrade(final ResourceOwner proposer, final ResourceOwner accepter,
final Map<ResourceType, Integer> proposedResouces,
final Map<ResourceType, Integer> wantedResources) {
/**
*
*/
Expand All @@ -120,26 +121,26 @@ private void initializeResourceMap(final Map<ResourceType, Integer> map) {
}

@Override
public int getResourcesToDiscard(int amount) {
public int getResourcesToDiscard(final int amount) {
if (amount <= DISCARD_THRESHOLD) {
return 0;
}
return amount / 2;
}

@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;
}

}
4 changes: 2 additions & 2 deletions src/main/java/it/unibo/model/impl/RoadManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int getLongestRoadLength(final Player player) {
.forEach(nearPosition -> graph.addEdge(propertyPos, nearPosition)));

final AllDirectedPaths<PropertyPosition, DefaultEdge> 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);
}

Expand All @@ -95,7 +95,7 @@ public Optional<Player> 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()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/it/unibo/view/ResourcesViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> listener) {
final VBox resourceBox = new VBox();
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 866e784

Please sign in to comment.