Reputation: 33
I know there are a lot of similar questions on this topic but I couldn't find any answers that helped me. I have created a game and in it you have to state your player number (1, 2, 3 etc). Once you finish the game it will display your player number and your score in a scoreboard. I'm trying to make the scoreboard work so that it will sort out the scores from high to low and give the correct player number beside each. I have both of these saved as lists. I know I can use .Sort() and .Reverse() to sort out the scores from high to low but how can I make sure they are still associated with the correct player number? I came up with the below code that sorts the scores slightly but not completely. For example, if player 1 scores 2, player 2 scores 3 and player 3 scores 5 it sorts from high to low as; Player 2 scored 3, Player 3 scored 5, Player 1 scored 2. I get why my code doesn't completely work but any way I've tried to fix it just results in errors.
The lists are initialised like this:
public static List<int> score = new List<int>();
public static List<int> player = new List<int>();
This is the part of the code where if you've failed the game it stores your player number and score then opens up the scoreboard form.
else
{
MessageBox.Show("Final Score: " + sequence.Count, "Fail!");
score.Add(sequence.Count);
player.Add(Convert.ToInt32(PlayerNo.Text));
for (int i = 0; i < (player.Count - 1); i++)
{
if (score[i + 1] > score[i])
{
int hi = score[i];
int hi1 = player[i];
score[i] = score[i + 1];
player[i] = player[i + 1];
score[i + 1] = hi;
player[i + 1] = hi1;
}
}
number = 0;
sequence = new List<int>();
Score s = new Score();
this.Hide();
s.ShowDialog();
this.Close();
}
The scoreboard form looks like this (I know it's quite long-winded but I don't know of a better way to do it- I've not been coding with C# long).
public Score()
{
InitializeComponent();
switch (Form5.player.Count)
{
case 1:
textBox1.Text = "Player " + Form5.player[0] + " scored " + Form5.score[0];
break;
case 2:
textBox1.Text = "Player " + Form5.player[0] + " scored " + Form5.score[0];
textBox2.Text = "Player " + Form5.player[1] + " scored " + Form5.score[1];
break;
case 3:
textBox1.Text = "Player " + Form5.player[0] + " scored " + Form5.score[0];
textBox2.Text = "Player " + Form5.player[1] + " scored " + Form5.score[1];
textBox3.Text = "Player " + Form5.player[2] + " scored " + Form5.score[2];
break;
case 4:
textBox1.Text = "Player " + Form5.player[0] + " scored " + Form5.score[0];
textBox2.Text = "Player " + Form5.player[1] + " scored " + Form5.score[1];
textBox3.Text = "Player " + Form5.player[2] + " scored " + Form5.score[2];
textBox4.Text = "Player " + Form5.player[3] + " scored " + Form5.score[3];
break;
case 5:
textBox1.Text = "Player " + Form5.player[0] + " scored " + Form5.score[0];
textBox2.Text = "Player " + Form5.player[1] + " scored " + Form5.score[1];
textBox3.Text = "Player " + Form5.player[2] + " scored " + Form5.score[2];
textBox4.Text = "Player " + Form5.player[3] + " scored " + Form5.score[3];
textBox5.Text = "Player " + Form5.player[4] + " scored " + Form5.score[4];
break;
The scoreboard form itself is just 5 text boxes. Any ideas on how to fix/complete this or suggestions of a new way to do it? I have seen people mention XML files but I have no idea how to use those. Thanks in advance for any advice.
Upvotes: 1
Views: 6720
Reputation: 204
You could improve the Leaderboard code by using a loop and finding the elements by id like below. You maybe have to change Controls to the container that hosts your textboxes.
for (int i = 0; i < Form5.player.Count; i++)
{
var control = Controls.Find($"textBox{i+1}", true).First();
control.Text = $"Player {Form5.player[i]} scored {Form5.score[i]}";
}
Upvotes: 0
Reputation: 892
If I understand you correctly, the problem you have is in the "sorting" part. You can sort this easily using linq.
Let's say that you have a collection named "PlayerScores" with all the pairs player/score as Joshua said (or a dictionary if you prefer, it could work here too), then you can do something like:
var orderedScores = PlayerScores.OrderByDescending(x => x.Score);
Additionally, if you prefer to work with an array, you chain that with ToArray():
var orderedScores = PlayerScores.OrderByDescending(x => x.Score).ToArray();
Upvotes: 0
Reputation: 512
Ideally you would want to create an object, lets call it PlayerScore, that would hold both the player number and the score.
Then you could sort the list of PlayerScores by the score and the player id would still have that association to the sorted list of scores.
public class PlayerScore
{
public int PlayerId
{
get;
set;
}
public int Score
{
get;
set;
}
public PlayerScore(){
}
}
Upvotes: 2