Reputation: 133
I'm in the process of building an algorithm for a leaderboard for my game, but now I'm stuck.
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.
For example: Player 1 is Juliet. Player 2 is Romeo. At the end of the match, Juliet's score is greater than Romeo's.
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.
As of the moment I'm also only thinking of using JLabel
to display the scores, but I'm not sure if this is the most efficient way to do it. I'm considering JTable
.
So here's my current algorithm:
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.)
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 > 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
}
}
}
2) Save the name & score in Leaderboard
class:
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;
}
}
3) The next step which is I'm highly unsure of is to create an ArrayList
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.
Like for example the scores are:
Anna = 1
Pamela = 5
Gabby = 3
Sorted: 5, 3, 1 Pamela has the highest score, but how will I be able to display her name together with her score?
4) I have a checker which determines if the ArrayList
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).
It's something like this:
if (board.isEmpty())
{
label1.setText(winnername);
scorelabel1.setText(winnerscore);
}
else
{
//
}
There's a whole lot of buzz going on in my head and now I don't know how I should proceed.
This is obviously a beginner's question and probably needs a 'big' answer, but I'm simply asking for a few hints/suggestions.
Upvotes: 0
Views: 10367
Reputation: 3126
It's a bad idea to store all the scores of all the players in the text file. At any point of time your text file should contain only the top 5 players and their scores.
let LeaderBoard be the current LeaderBoard and it already contains the top 5 scores. When you have to add a new score, you can just use this approach.
min = minimum scorer in the leaderBoard
if(player1 > min){
remove min from leaderBoard
add player1 to the leaderBoard
}
min = minimum scorer in the leaderBoard
if(player2 > min){
remove min from leaderBoard
add player2 to the leaderBoard
}
By doing so, at any point of time, you will have the top 5 scorers in the leaderBoard. You will also include the scores of both players.
Upvotes: 2