From 05cf38c26df3896e121c5d00daef10ce647db0d6 Mon Sep 17 00:00:00 2001 From: vincenttrinh3336 <158768889+vincenttrinh3336@users.noreply.github.com> Date: Mon, 29 Apr 2024 22:14:59 +0000 Subject: [PATCH] trying new method --- src/main/java/uta/cse3310/Leaderboard.java | 51 ++++++++++++---------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/main/java/uta/cse3310/Leaderboard.java b/src/main/java/uta/cse3310/Leaderboard.java index d7e0821..e01a826 100644 --- a/src/main/java/uta/cse3310/Leaderboard.java +++ b/src/main/java/uta/cse3310/Leaderboard.java @@ -5,39 +5,45 @@ public class Leaderboard { public TreeMap LB; - public SortedSet sortedScore; - - static extends ComparableSortedSet> sortingScore(Map map) { - SortedSet> sortedEntries = new TreeSet>( - new Comparator>() { - @Override public int compare(Map.Entry e1, Map.Entry e2) { - int res = e1.getValue().compareTo(e2.getValue()); - return res != 0 ? res : 1; // Special fix to preserve items with equal values - } - } - ); - sortedEntries.addAll(map.entrySet()); - return sortedEntries; - } - + public List scores; + public List names; public Leaderboard() { // Initialize the TreeMap with a custom comparator to sort by score in descending order LB = new TreeMap<>(); - sortedScore = new SortedSet(); + score = new Arraylist(); + names = new Arraylist(); } // Method to add a score to the leaderboard public void add(String handle, int score) { LB.put(handle, score); - sortedScore.clear(); - sortedScore = sortingScore(LB); + scores.add(score); + Collections.sort(scores); + if(scores.indexOf(score) > names.length()) + { + names.add(handle); + } + else + { + names.add(scores.indexOf(score), handle); + } } public void update(String handle, int score) { int new_score = score + LB.get(handle); LB.put(handle, new_score); - sortedScore.clear(); - sortedScore = sortingScores(LB); + scores.remove(score); + names.remove(handle); + scores.add(new_score) + Collections.sort(scores); + if(scores.indexOf(new_score) > names.length()) + { + names.add(handle); + } + else + { + names.add(scores.indexOf(new_score), handle); + } } public void remove(String handle) @@ -45,9 +51,10 @@ public void remove(String handle) if (handle!= null) { LB.remove(handle); - sortedScore.clear(); - sortedScore = sortingScores(LB); } + int i = names.indexOf(handle); + names.remove(i); + scores.remove(i); } }