Created
August 9, 2021 22:38
-
-
Save JariRoossien/b4ee0104322036ed37adb0fe9e7f7348 to your computer and use it in GitHub Desktop.
Tournament distribution simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package nl.jariroossien.aoc; | |
| import java.util.*; | |
| public class Main { | |
| private static final int ITERATIONS = 250; | |
| private static final int PLAYER_COUNT = 50000; | |
| public static void main(String[] args) { | |
| List<Rank> ranks = new ArrayList<>(); | |
| Rank starter = new Rank("Starter", 8d/24, 0); | |
| Rank gold = new Rank("Gold", 6d/24, 12d/24); | |
| Rank diamond = new Rank("Diamond", 6d/24, 12d/24); | |
| Rank master = new Rank("Master", 5d/24, 14d/24); | |
| Rank grandmaster = new Rank("Grandmaster", 0, 16d/24); | |
| starter.rankToGoUp = gold; | |
| gold.rankToGoDown = starter; | |
| gold.rankToGoUp = diamond; | |
| diamond.rankToGoDown = gold; | |
| diamond.rankToGoUp = master; | |
| master.rankToGoDown = diamond; | |
| master.rankToGoUp = grandmaster; | |
| grandmaster.rankToGoDown = master; | |
| ranks.add(starter); | |
| ranks.add(gold); | |
| ranks.add(diamond); | |
| ranks.add(master); | |
| ranks.add(grandmaster); | |
| Set<UUID> players = new HashSet<>(); | |
| for (int i = 0; i < PLAYER_COUNT; i++) { | |
| players.add(UUID.randomUUID()); | |
| } | |
| starter.stays.addAll(players); | |
| for (int i = 0; i < ITERATIONS; i++) { | |
| ranks.forEach(Rank::simulateRound); | |
| ranks.forEach(Rank::transferPlayers); | |
| } | |
| ranks.forEach(System.out::println); | |
| } | |
| private static class Rank { | |
| String rankName; | |
| Set<UUID> toGoUpSet = new HashSet<>(); | |
| Set<UUID> toGoDownSet = new HashSet<>(); | |
| Set<UUID> stays = new HashSet<>(); | |
| Rank rankToGoUp; | |
| Rank rankToGoDown; | |
| private double percentageGoesUp; | |
| private double percentageGoDown; | |
| public Rank(String rankName, double percentageGoesUp, double percentageGoDown) { | |
| this.rankName = rankName; | |
| this.percentageGoesUp = percentageGoesUp; | |
| this.percentageGoDown = percentageGoDown; | |
| } | |
| public void simulateRound() { | |
| int totalToGoUp = (int) (stays.size() * percentageGoesUp); | |
| int totalToGoDown = (int) (stays.size() * percentageGoDown); | |
| Iterator<UUID> stayIterator = stays.iterator(); | |
| for (int i = 0; i < totalToGoUp; i++) { | |
| toGoUpSet.add(stayIterator.next()); | |
| stayIterator.remove(); | |
| } | |
| for (int i = 0; i < totalToGoDown; i++) { | |
| toGoDownSet.add(stayIterator.next()); | |
| stayIterator.remove(); | |
| } | |
| } | |
| public void transferPlayers() { | |
| if (rankToGoUp != null) { | |
| rankToGoUp.stays.addAll(toGoUpSet); | |
| } else { | |
| stays.addAll(toGoUpSet); | |
| } | |
| toGoUpSet.clear(); | |
| if (rankToGoDown != null) { | |
| rankToGoDown.stays.addAll(toGoDownSet); | |
| } else { | |
| stays.addAll(toGoDownSet); | |
| } | |
| toGoDownSet.clear(); | |
| } | |
| @Override | |
| public String toString() { | |
| return String.format("%-20s: %-5s (%.1f%s)", rankName, stays.size(), stays.size() * 100d / PLAYER_COUNT, "%"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment