Skip to content

Commit

Permalink
add first working approach to recursive anchorages combination valida…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
akmsw committed May 18, 2024
1 parent 1003fa2 commit bc29eac
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 14 deletions.
109 changes: 104 additions & 5 deletions src/main/java/armameeldoparti/controllers/AnchoragesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import armameeldoparti.models.Player;
import armameeldoparti.models.Position;
import armameeldoparti.models.ProgramView;
import armameeldoparti.models.Team;
import armameeldoparti.utils.common.CommonFields;
import armameeldoparti.utils.common.CommonFunctions;
import armameeldoparti.utils.common.Constants;
import armameeldoparti.views.AnchoragesView;
import java.awt.Component;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
Expand All @@ -29,6 +34,7 @@ public class AnchoragesController extends Controller<AnchoragesView> {

private int anchoragesAmount;
private int anchoredPlayersAmount;
private int recursiveVerificationIndex;

// ---------- Constructor --------------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -72,8 +78,8 @@ public void updateCheckboxesText() {
* @param parentComponent Graphical component where the dialogs associated with the event should be displayed.
*/
public void finishButtonEvent(Component parentComponent) {
if (!validAnchoragesCombination()) {
CommonFunctions.showErrorMessage("Error message", parentComponent);
if (!validAnchoragesCombination(Arrays.asList(new Team(0), new Team(1)))) {
CommonFunctions.showErrorMessage("Existen conflictos entre anclajes", parentComponent);

return;
}
Expand Down Expand Up @@ -200,6 +206,7 @@ protected void resetView() {
protected void setUpInitialState() {
anchoragesAmount = 0;
anchoredPlayersAmount = 0;
recursiveVerificationIndex = 0;

view.getFinishButton()
.setEnabled(false);
Expand Down Expand Up @@ -488,12 +495,104 @@ private boolean validAnchoredPlayersAmount(int playersToAnchorAmount) {
return anchoredPlayersAmount + playersToAnchorAmount <= Constants.MAX_ANCHORED_PLAYERS;
}

private boolean validAnchoragesCombination(List<Team> teams) {
if (recursiveVerificationIndex == CommonFunctions.getAnchoredPlayers()
.size()) {
return validTeams(teams);
}

List<Player> anchorage = CommonFunctions.getAnchoredPlayers()
.get(recursiveVerificationIndex);

for (int teamIndex = 0; teamIndex < teams.size(); teamIndex++) {
Team team = teams.get(teamIndex);

if (!anchoragesConflictExists(team, anchorage)) {
anchorage.forEach(player -> team.getTeamPlayers()
.get(player.getPosition())
.add(player));

recursiveVerificationIndex++;

if (validAnchoragesCombination(teams)) {
recursiveVerificationIndex = 0;

return true;
}

team.clear();
}
}

recursiveVerificationIndex = 0;

return false;
}

/**
* TODO.
* The "java:S1190" and "java:S117" warnings are suppressed since JDK22 allows the use of unnamed variables.
*
* @return WIP.
* @param team
* @param anchorage
*
* @return
*/
private boolean validAnchoragesCombination() {
@SuppressWarnings({"java:S1190", "java:S117"})
private boolean anchoragesConflictExists(Team team, List<Player> anchorage) {
Map<Position, Integer> playersPerPosition = team.getTeamPlayers()
.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toMap(
Player::getPosition,
_ -> 1,
Integer::sum,
() -> new EnumMap<>(Position.class))
);

for (Player player : anchorage) {
int newCount = playersPerPosition.getOrDefault(player.getPosition(), 0) + 1;

if (newCount > CommonFields.getPlayersAmountMap()
.get(player.getPosition())) {
return true;
}

playersPerPosition.put(player.getPosition(), newCount);
}

return false;
}

/**
* The "java:S1190" and "java:S117" warnings are suppressed since JDK22 allows the use of unnamed variables.
*
* @param teams
*
* @return
*/
@SuppressWarnings({"java:S1190", "java:S117"})
private boolean validTeams(List<Team> teams) {
for (Team team : teams) {
Map<Position, Integer> playersPerPosition = team.getTeamPlayers()
.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toMap(
Player::getPosition,
_ -> 1,
Integer::sum,
() -> new EnumMap<>(Position.class))
);

for (Map.Entry<Position, Integer> entry : CommonFields.getPlayersAmountMap()
.entrySet()) {
if (playersPerPosition.getOrDefault(entry.getKey(), 0) > entry.getValue()) {
return false;
}
}
}

return true;
}
}
18 changes: 9 additions & 9 deletions src/main/java/armameeldoparti/controllers/ResultsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public void updateTable() {
};

teams.forEach(
team -> {
Arrays.stream(Position.values())
.forEach(position -> team.getTeamPlayers()
.get(position)
.forEach(player -> table.setValueAt(player.getName(), wrapper.row++, wrapper.column)));

wrapper.column++;
wrapper.row = 1;
}
team -> {
Arrays.stream(Position.values())
.forEach(position -> team.getTeamPlayers()
.get(position)
.forEach(player -> table.setValueAt(player.getName(), wrapper.row++, wrapper.column)));

wrapper.column++;
wrapper.row = 1;
}
);

if (CommonFields.getDistribution() == Constants.MIX_BY_SKILL_POINTS) {
Expand Down

0 comments on commit bc29eac

Please sign in to comment.