Store int scores and getting top values

Discussion in 'Plugin Development' started by TumbaBit, Feb 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    I'm making a small minigame and I would like to be able to store the total amount of points the players have gotten and then later getting the top 3 players and their score. I've been thinking of how to do this for some time and the best way would seem to be either a hashmap or a list. Unfortunately, I have, with my current knowledge of Bukkit and Java, a problem with both of these methods.

    Firstly, I don't know a way to sort hashmaps by value, in a way where I can still get both key and value. If anyone knows a good way to do this, please let me know. My problem with lists are, that I don't see any good way to store both the players' name and points in a way where I can sort the points and still get the right player from the other list. So I'm wondering, is there another way to do this? Any help would be greatly appreciated. Thanks!
  2. Offline


    I'm not very good with HashMaps, so I usually end up using an array list and making another class that stores the player's name and their score together. For example:

    Show Spoiler
    1. public class PlayerScore{
    2. priavte player player;
    3. private int score;
    5. public PlayerScore(Player player, int score){
    6. this.player = player;
    7. this.score = score;
    8. }
    10. public Player getPlayer(){
    11. return this.player;
    12. }
    14. public int getScore(){
    15. return this.score;
    16. }
    17. }

    After I have that, I make a new ArrayList that stores all of the PlayerScores and use a for loop to cycle through them all for the top scores. I've gotta go, if you need more help I'll check in again later. ;)
  3. an object :)
  4. Offline


    I still feel like I would have the problem with sorting it if I made a new object for it.
  5. Offline


    You could just do something like this:

    Show Spoiler
    1. ArrayList<PlayerScore> scores = new ArrayList<PlayerScore>();
    3. ArrayList<PlayerScore> topThree = new ArrayList<PlayerScore();
    4. for(int i=0;i<3;i++){
    5. PlayerScore topScore = null;
    6. for(PlayerScore score : scores){
    7. if(topScore != null){
    8. if(score.getScore > topScore.getScore() && !topThree.contains(score)){
    9. topScore = score;
    10. }
    11. }
    12. }
    13. topThree.add(score);
    14. }

    I'm assuming that the scores array contains all of the PlayerScores that you are using. ;)
  6. Offline


    Ah yes of course. Thank you for the help :)
    Jakeob22 likes this.
  7. Offline


    I forgot, if you use that, you need to make sure that you set the current score your top score if topScore is null.
  8. Offline


    Could also use Collections.sort().
Thread Status:
Not open for further replies.

Share This Page