title: Creating a leaderboard?
body: <p>I'm in the process of building an algorithm for a leaderboard for my game, but now I'm stuck.</p>
  
  <ul>
  <li>It is a two-player game.</li>
  <li>The leaderboard is supposed to display the top 5 players.</li>
  <li>Scores will be saved to an external .txt file as a reference.</li>
  </ul>
  
  <p>Also, may I add that I still cannot decide if I should only consider the winner's score for the leaderboard, or also the loser's.</p>
  
  <p>For example:
  Player 1 is Juliet.
  Player 2 is Romeo.
  At the end of the match, Juliet's score is greater than Romeo's.</p>
  
  <p>Should I only evaluate Juliet's score for the leaderboard, or both of their scores? As of now my algorithm considers only the winner's score.</p>
  
  <p>As of the moment I'm also only thinking of using <code>JLabel</code> to display the scores, but I'm not sure if this is the most efficient way to do it. I'm considering <code>JTable</code>.</p>
  
  <p>So here's my current algorithm:</p>
  
  <p>1) When the user starts a new match/returns to menu/exits game: (I provided 4 options: new game (scores remain but the board is reset), new match (everything is reset), menu, and exit.)</p>
  
  <pre><code>public class Game 
  {
      //This class also holds the code for the operation of the game
  
      if (event.getSource() == newmatch)//or menu or exit
      {
          if (p1score != 0 || p2score != 0)
          {
              Leaderboard x; 
  
              if (p1score &gt; p2score)
              {
                  x = new Leaderboard (player1name, p1score);
              }
              else
              {
                  x = new Leaderboard (player2name, p2score);
              }
          }
  
          else
          {
              //some code
              //This check is whether the game ends up in a draw/or both the users' scores         are == 0
          }
      }
  }
  </code></pre>
  
  <p>2) Save the name &amp; score in <code>Leaderboard</code> class:</p>
  
  <pre><code>public class Leaderboard {
      //This class is where I originally planned to write the code that will perform the evaluation of the scores
  
      String playerid;
      int playerscore;
  
          public Leaderboard(String name, int score)
          {
              playerid = name;
              playerscore = score;
          }
  
  }
  </code></pre>
  
  <p>3) The next step which is I'm highly unsure of is to create an <code>ArrayList</code> for the scores (I'm missing the part about the player's name because well I'm not sure as well how I will approach it) and then sort it, but the problem after that would be how to make the score agree with the player's name, if you get what I mean.</p>
  
  <p>Like for example the scores are: </p>
  
  <p>Anna = 1</p>
  
  <p>Pamela = 5</p>
  
  <p>Gabby = 3</p>
  
  <p>Sorted: 5, 3, 1
  Pamela has the highest score, but how will I be able to display her name together with her score?</p>
  
  <p>4) I have a checker which determines if the <code>ArrayList</code> of scores is empty, and if it is, it will automatically display the player's name and score (this is for the first pair of players).</p>
  
  <p>It's something like this:</p>
  
  <pre><code>if (board.isEmpty())
  {
      label1.setText(winnername);
      scorelabel1.setText(winnerscore);
  }
  
  else
  {
      //
  }
  </code></pre>
  
  <p>There's a whole lot of buzz going on in my head and now I don't know how I should proceed.</p>
  
  <p>This is obviously a beginner's question and probably needs a 'big' answer, but I'm simply asking for a few hints/suggestions. </p>