Skip to content

Commit

Permalink
Added options for special conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Foulest committed Dec 6, 2024
1 parent 8ee95f6 commit 4ea021e
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/main/java/net/foulest/swiss/brackets/StandardBracket.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class StandardBracket implements Bracket {
private List<Team> teams;
private long startingTime;

// For special conditions, like a team reaching a certain round
int specialReached;
int specialSuccess;

public StandardBracket(@NotNull List<Team> teams) {
this.teams = teams;
startingTime = System.currentTimeMillis();
Expand Down Expand Up @@ -85,7 +89,7 @@ public void simulateMultipleBrackets(int numSimulations) {
}

// Analyze and print the results after all simulations are complete
printResults(results, numSimulations, startingTime);
printResults(results, numSimulations, startingTime, specialReached, specialSuccess);
}

/**
Expand Down Expand Up @@ -130,7 +134,7 @@ private void simulateBracket(Map<Team, Map<String, Integer>> results) {
updateTeamRatings(team1, team2, winner, match.getMaxRounds() == 3);

// Update records
updateRecords(records, winner, loser);
Bracket.updateRecords(records, winner, loser);

// Update past opponents
updatePastOpponents(pastOpponents, winner, loser);
Expand Down Expand Up @@ -288,7 +292,7 @@ private void simulateBracket(Map<Team, Map<String, Integer>> results) {
updateTeamRatings(team1, team2, winner, match.getMaxRounds() == 3);

// Update records
updateRecords(records, winner, loser);
Bracket.updateRecords(records, winner, loser);

// Update past opponents
updatePastOpponents(pastOpponents, winner, loser);
Expand Down Expand Up @@ -468,25 +472,18 @@ private static void sortTeams(@NotNull List<Team> group, Map<Team, Integer> buch

// Sort the teams by Buchholz score, then by seeding
group.sort((team1, team2) -> {
int buchholzComparison = Integer.compare(buchholzScores.get(team2), buchholzScores.get(team1));
Integer t2Score = buchholzScores.get(team2);
Integer t1Score = buchholzScores.get(team1);
int buchholzComparison = Integer.compare(t2Score, t1Score);

if (buchholzComparison != 0) {
return buchholzComparison;
}
return Integer.compare(team1.getSeeding(), team2.getSeeding());
});
}

/**
* Update the records for each team.
*
* @param records The records of each team.
* @param winner The winning team.
* @param loser The losing team.
*/
private static void updateRecords(@NotNull Map<Team, int[]> records, Team winner, Team loser) {
records.get(winner)[0] += 1;
records.get(loser)[1] += 1;
int t1Seeding = team1.getSeeding();
int t2Seeding = team2.getSeeding();
return Integer.compare(t1Seeding, t2Seeding);
});
}

/**
Expand Down Expand Up @@ -548,11 +545,12 @@ private static boolean allTeamsDecided(@NotNull List<Team> activeTeams, Map<Team
/**
* Print the results of the simulations.
*
* @param results The results of the simulations.
* @param numSimulations The number of simulations.
* @param results The results of the simulations.
* @param numSimulations The number of simulations.
*/
private static void printResults(@NotNull Map<Team, Map<String, Integer>> results,
int numSimulations, long startingTime) {
int numSimulations, long startingTime,
int specialReached, int specialSuccess) {
// Print the header
Bracket.printHeader(numSimulations, startingTime);

Expand All @@ -576,7 +574,17 @@ private static void printResults(@NotNull Map<Team, Map<String, Integer>> result
Bracket.appendProbability(resultString, "0-3", recordCounts, numSimulations);

// Print the team's result
System.out.println(resultString.toString().trim());
String result = resultString.toString();
String trimmed = result.trim();
System.out.println(trimmed);
}

// Print special conditions
if (specialReached > 0) {
System.out.println();
System.out.println("Special Reached: " + specialReached);
System.out.println("Special Success: " + specialSuccess);
System.out.println("Special Odds: " + (specialSuccess * 100.0 / specialReached) + "%");
}
}
}

0 comments on commit 4ea021e

Please sign in to comment.