Skip to content

Commit

Permalink
enhance algorithm to verify anchorages conflicts, fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed May 19, 2024
1 parent e8c70a5 commit 34e6620
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 164 deletions.
12 changes: 6 additions & 6 deletions src/main/java/armameeldoparti/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public static void main(String[] args) {
CommonFields.setActiveMonitor(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
CommonFields.setAnchoragesEnabled(false);
CommonFields.setControllerMap(new EnumMap<>(ProgramView.class));
CommonFields.setPlayersAmountMap(new EnumMap<>(Position.class));
CommonFields.setControllersMap(new EnumMap<>(ProgramView.class));
CommonFields.setPlayersLimitPerPosition(new EnumMap<>(Position.class));
CommonFields.setPlayersSets(new TreeMap<>());
CommonFields.setPositionsMap(Map.of(Position.CENTRAL_DEFENDER, Constants.POSITION_CENTRAL_DEFENDERS,
Position.LATERAL_DEFENDER, Constants.POSITION_LATERAL_DEFENDERS,
Expand All @@ -98,7 +98,7 @@ public static void main(String[] args) {
private static void populatePlayersSets() {
Arrays.stream(Position.values())
.forEach(position -> CommonFields.getPlayersSets()
.put(position, IntStream.range(0, CommonFields.getPlayersAmountMap()
.put(position, IntStream.range(0, CommonFields.getPlayersLimitPerPosition()
.get(position) * 2)
.mapToObj(_ -> new Player("", position))
.toList()));
Expand Down Expand Up @@ -134,10 +134,10 @@ private static void setPlayersDistribution() {
.toList();

IntStream.range(0, filteredLines.size())
.forEach(index -> CommonFields.getPlayersAmountMap()
.forEach(index -> CommonFields.getPlayersLimitPerPosition()
.put(Position.values()[index],
Integer.parseInt(filteredLines.get(index)
.replaceAll(Constants.REGEX_PLAYERS_AMOUNT, ""))));
.replaceAll(Constants.REGEX_PLAYERS_COUNT, ""))));
} catch (IOException _) {
CommonFunctions.exitProgram(Error.ERROR_FILES);
}
Expand All @@ -147,7 +147,7 @@ private static void setPlayersDistribution() {
* Creates the controllers and assigns their corresponding view to control.
*/
private static void setUpControllers() {
CommonFields.getControllerMap()
CommonFields.getControllersMap()
.putAll(Map.of(ProgramView.MAIN_MENU, new MainMenuController(new MainMenuView()),
ProgramView.HELP, new HelpController(new HelpView()),
ProgramView.NAMES_INPUT, new NamesInputController(new NamesInputView()),
Expand Down
164 changes: 72 additions & 92 deletions src/main/java/armameeldoparti/controllers/AnchoragesController.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/main/java/armameeldoparti/controllers/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ protected void showView() {
protected abstract void resetView();

/**
* Sets up the view's GUI components initial state if needed.
* Sets up the controller and GUI components initial state, if needed.
*/
protected abstract void setUpInitialState();

/**
* Sets up the view's GUI components event listeners.
* Sets up the GUI components event listeners.
*/
protected abstract void setUpListeners();

Expand Down
22 changes: 18 additions & 4 deletions src/main/java/armameeldoparti/models/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Team class.
Expand All @@ -27,6 +28,8 @@ public class Team {

/**
* Builds a basic team with empty position sets.
*
* @param teamNumber Integer identification for the team.
*/
public Team(int teamNumber) {
setTeamPlayers(new EnumMap<>(Position.class));
Expand All @@ -49,7 +52,7 @@ public void clear() {
}

/**
* @return The amount of players in the team.
* @return The number of players in the team.
*/
public int getPlayersCount() {
return teamPlayers.values()
Expand All @@ -58,6 +61,19 @@ public int getPlayersCount() {
.sum();
}

/**
* <p>The "java:S1190" and "java:S117" warnings are suppressed since JDK22 allows the use of unnamed variables.
*
* @return The number of players per position in the team.
*/
@SuppressWarnings({"java:S1190", "java:S117"})
public Map<Position, Integer> getPlayersCountPerPosition() {
return teamPlayers.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toMap(Player::getPosition, _ -> 1, Integer::sum, () -> new EnumMap<>(Position.class)));
}

/**
* @return The team skill points accumulated so far.
*/
Expand All @@ -70,15 +86,13 @@ public int getTeamSkill() {
}

/**
* Checks if a particular position set in the team is full.
*
* @param position The position of the set to check.
*
* @return Whether the specified position set in the team is full.
*/
public boolean isPositionFull(Position position) {
return teamPlayers.get(position)
.size() == CommonFields.getPlayersAmountMap()
.size() == CommonFields.getPlayersLimitPerPosition()
.get(position);
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/armameeldoparti/utils/common/CommonFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public final class CommonFields {

private static GraphicsDevice activeMonitor;

private static Map<Position, Integer> playersAmountMap;
private static Map<Position, Integer> playersLimitPerPosition;
private static Map<Position, List<Player>> playersSets;
private static Map<Position, String> positionsMap;
private static Map<ProgramView, Controller<? extends View>> controllerMap;
private static Map<ProgramView, Controller<? extends View>> controllersMap;

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

Expand All @@ -56,8 +56,8 @@ public static GraphicsDevice getActiveMonitor() {
return activeMonitor;
}

public static Map<Position, Integer> getPlayersAmountMap() {
return playersAmountMap;
public static Map<Position, Integer> getPlayersLimitPerPosition() {
return playersLimitPerPosition;
}

public static Map<Position, List<Player>> getPlayersSets() {
Expand All @@ -72,8 +72,8 @@ public static Map<Position, String> getPositionsMap() {
* The "java:S1452" warning is suppressed since the Java compiler can't know at runtime the type of the controlled view.
*/
@SuppressWarnings("java:S1452")
public static Map<ProgramView, Controller<? extends View>> getControllerMap() {
return controllerMap;
public static Map<ProgramView, Controller<? extends View>> getControllersMap() {
return controllersMap;
}

// ---------- Setters ------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -90,8 +90,8 @@ public static void setActiveMonitor(GraphicsDevice activeMonitor) {
CommonFields.activeMonitor = activeMonitor;
}

public static void setPlayersAmountMap(Map<Position, Integer> playersAmountMap) {
CommonFields.playersAmountMap = playersAmountMap;
public static void setPlayersLimitPerPosition(Map<Position, Integer> playersLimitPerPosition) {
CommonFields.playersLimitPerPosition = playersLimitPerPosition;
}

public static void setPlayersSets(Map<Position, List<Player>> playersSets) {
Expand All @@ -102,7 +102,7 @@ public static void setPositionsMap(Map<Position, String> positionsMap) {
CommonFields.positionsMap = positionsMap;
}

public static void setControllerMap(Map<ProgramView, Controller<? extends View>> controllerMap) {
CommonFields.controllerMap = controllerMap;
public static void setControllersMap(Map<ProgramView, Controller<? extends View>> controllerMap) {
CommonFields.controllersMap = controllerMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static ImageIcon scaleImageIcon(ImageIcon icon, int width, int height, in
*/
@SuppressWarnings("java:S1452")
public static Controller<? extends View> getController(ProgramView view) {
return CommonFields.getControllerMap()
return CommonFields.getControllersMap()
.get(view);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/armameeldoparti/utils/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public final class Constants {
public static final String PROGRAM_AUTHOR = "@" + PROGRAM_AUTHOR_GITHUB_USERNAME;
public static final String REGEX_NAMES_VALIDATION = "[a-z A-ZÁÉÍÓÚáéíóúñÑ]+";
public static final String REGEX_PDA_DATA_RETRIEVE = "[CLMFG].+>.+";
public static final String REGEX_PLAYERS_AMOUNT = "(?!(?<=" + PLAYERS_PER_TEAM + ")\\d).";
public static final String REGEX_PLAYERS_COUNT = "(?!(?<=" + PLAYERS_PER_TEAM + ")\\d).";
public static final String URL_CONTACT = "https://github.com/" + PROGRAM_AUTHOR_GITHUB_USERNAME;
public static final String URL_ISSUES = URL_CONTACT + "/" + PROGRAM_TITLE.replace(" ", "-") + "/issues";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public List<Team> withoutAnchorages(List<Team> teams) {
* with the sets with most anchored players in order to avoid inconsistencies.
*
* <p>Then, the players that are not anchored are distributed between the teams as fair as possible based on their skill points. They will be added
* to a team only if the players per position or the players per team amounts are not exceeded.
* to a team only if the players per position or the players per team limits are not exceeded.
*
* @param teams Teams where to distribute the players.
*
Expand Down
82 changes: 36 additions & 46 deletions src/main/java/armameeldoparti/utils/mixers/RandomMixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public List<Team> withoutAnchorages(List<Team> teams) {
* <p>First, the anchored players are grouped in different lists by their anchorage number, and they are distributed randomly starting with the sets
* with most anchored players in order to avoid inconsistencies. If a set of anchored players cannot be added to one team, it will be added to the
* other. Then, the players that are not anchored are distributed randomly. They will be added to a team only if the players per position or the
* players per team amounts are not exceeded.
* players per team limits are not exceeded.
*
* @param teams Teams where to distribute the players.
*
Expand Down Expand Up @@ -148,36 +148,6 @@ public List<Team> withAnchorages(List<Team> teams) {

// ---------- Private methods ----------------------------------------------------------------------------------------------------------------------

/**
* Checks which team a given player can be added to.
*
* @param teams The possible teams where to add the player.
* @param validationPredicate The predicate that will validate if the player can be added to a team, or not.
*
* @return The only available team index, a random team index if the player can be added in every team, or -1 if there's no available team for the
* player.
*/
private int getAvailableTeam(List<Team> teams, Predicate<Team> validationPredicate) {
shuffleTeamNumbers(teams.size());

boolean isRandomTeam1Available = validationPredicate.test(teams.get(randomTeam1));
boolean isRandomTeam2Available = validationPredicate.test(teams.get(randomTeam2));

if (isRandomTeam1Available && isRandomTeam2Available) {
return randomGenerator.nextInt(teams.size());
}

if (isRandomTeam1Available) {
return randomTeam1;
}

if (isRandomTeam2Available) {
return randomTeam2;
}

return -1;
}

/**
* Randomly shuffles the team numbers.
*
Expand All @@ -189,8 +159,6 @@ private void shuffleTeamNumbers(int range) {
}

/**
* Checks if the given player position in the given team is already complete and, therefore, if the player can be added to it.
*
* @param team Team where the player should be added.
* @param player The players to add.
*
Expand All @@ -217,25 +185,20 @@ private boolean anchorageCanBeAdded(Team team, List<Player> anchoredPlayers) {
}

/**
* Checks if the amount of anchored players to be added to a team would exceed the maximum allowed amount of players per team.
*
* @param team Team to check if the anchored players can be added.
* @param anchoredPlayers Anchored players to check.
*
* @return Whether the amount of anchored players to be added to a team would exceed the maximum allowed amount of players per team.
* @return Whether the number of anchored players to be added to a team would exceed the limit of players per team.
*/
private boolean anchorageOverflowsTeamSize(Team team, List<Player> anchoredPlayers) {
return team.getPlayersCount() + anchoredPlayers.size() > Constants.PLAYERS_PER_TEAM;
}

/**
* Checks if the amount of anchored players to be added to a team would exceed the maximum allowed amount of players per team in any position set.
*
* @param team Team to check if the anchored players can be added.
* @param anchoredPlayers Anchored players to check.
*
* @return Whether the amount of anchored players to be added to a team would exceed the maximum allowed amount of players per team in any position
* set.
* @return Whether the number of anchored players to be added to a team would exceed the limit of players per team in any position set.
*/
private boolean anchorageOverflowsAnyPositionSet(Team team, List<Player> anchoredPlayers) {
return anchoredPlayers.stream()
Expand All @@ -244,15 +207,12 @@ private boolean anchorageOverflowsAnyPositionSet(Team team, List<Player> anchore
}

/**
* Checks if the amount of anchored players to be added to a position set in a team would exceed the maximum allowed amount of players per team for
* that particular position.
*
* @param team Team to check if the anchored players can be added.
* @param anchoredPlayers Anchored players to check.
* @param position Anchored players position.
*
* @return Whether the amount of anchored players to be added to a position set in a team would exceed the maximum allowed amount of players per
* team for that particular position.
* @return Whether the number of anchored players to be added to a position set in a team would exceed the limit of players per team for that
* particular position.
*/
private boolean anchorageOverflowsPositionSet(Team team, List<Player> anchoredPlayers, Position position) {
return team.getTeamPlayers()
Expand All @@ -261,7 +221,37 @@ private boolean anchorageOverflowsPositionSet(Team team, List<Player> anchoredPl
+ anchoredPlayers.stream()
.filter(player -> player.getPosition() == position)
.count()
> CommonFields.getPlayersAmountMap()
> CommonFields.getPlayersLimitPerPosition()
.get(position);
}

/**
* Checks which team a given player can be added to.
*
* @param teams The possible teams where to add the player.
* @param validationPredicate The predicate that will validate if the player can be added to a team, or not.
*
* @return The only available team index, a random team index if the player can be added in every team, or -1 if there's no available team for the
* player.
*/
private int getAvailableTeam(List<Team> teams, Predicate<Team> validationPredicate) {
shuffleTeamNumbers(teams.size());

boolean isRandomTeam1Available = validationPredicate.test(teams.get(randomTeam1));
boolean isRandomTeam2Available = validationPredicate.test(teams.get(randomTeam2));

if (isRandomTeam1Available && isRandomTeam2Available) {
return randomGenerator.nextInt(teams.size());
}

if (isRandomTeam1Available) {
return randomTeam1;
}

if (isRandomTeam2Available) {
return randomTeam2;
}

return -1;
}
}
2 changes: 1 addition & 1 deletion src/main/java/armameeldoparti/views/NamesInputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void addAnchoragesCheckbox() {
private void addTextFields() {
for (Position position : Position.values()) {
textFieldsMap.get(position)
.addAll(IntStream.range(0, CommonFields.getPlayersAmountMap()
.addAll(IntStream.range(0, CommonFields.getPlayersLimitPerPosition()
.get(position) * 2)
.mapToObj(_ -> new CustomTextField())
.toList());
Expand Down

0 comments on commit 34e6620

Please sign in to comment.