-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/utastudents/cse3310_sp24_gr…
- Loading branch information
Showing
1 changed file
with
49 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
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(); | ||
} | ||
} |