Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerrero96 committed Apr 16, 2024
2 parents 3396457 + 9602fc6 commit 7fef1cc
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions src/main/java/uta/cse3310/Leaderboard.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
package uta.cse3310;

import java.util.*;

public class Leaderboard {

List<PlayerType> players;
int finalGameStats;

public Leaderboard(int finalGameStats) {
players = new ArrayList<>();
finalGameStats = finalGameStats;
}

public void display() {
}

public void reset() {
}

}
package uta.cse3310;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Leaderboard {

private Map<PlayerType, Integer> playerScores; // Map players to their scores

public Leaderboard() {
this.playerScores = new HashMap<>();
}

// Method to add or update the score for a player
public void addOrUpdatePlayer(PlayerType player, int score) {
playerScores.put(player, score); // Update the map with the new score
}

// Sorts players by score in descending order and returns a sorted list of players
public List<PlayerType> getSortedPlayers() {
List<Map.Entry<PlayerType, Integer>> sortedEntries = new ArrayList<>(playerScores.entrySet());
sortedEntries.sort(Map.Entry.<PlayerType, Integer>comparingByValue().reversed());

//Extract players for returning
List<PlayerType> sortedPlayers = new ArrayList<>();
for (Map.Entry<PlayerType, Integer> entry : sortedEntries) {
sortedPlayers.add(entry.getKey());
}
return sortedPlayers;

}

// Displays the leaderboard
public void display() {
List<PlayerType> sortedPlayers = getSortedPlayers();
System.out.println("Leaderboard:");
for (PlayerType player : sortedPlayers) {
System.out.println(player.getNickname() + " - Score: " + playerScores.get(player));
}
}

// Resets the leaderboard for a new game
public void reset() {
playerScores.clear();
}
}

0 comments on commit 7fef1cc

Please sign in to comment.