-1

Ive got a problem trying to maintaining to update the content of a list. After completing the game the users details will be stored within the 1st position within the list however if another user plays the game the previous users details would be replaced with the new details.I think the problem arises when the highscores form button is clicked as it opens up a new form causing the content of the list to be set to null.

1st User: https://cdn.discordapp.com/attachments/176014540268371968/556088180466647061/unknown.png 2nd User: https://cdn.discordapp.com/attachments/176014540268371968/556088457311682609/unknown.png

Is there any possible solutions to this issue. Any Help would be appreciated.

public partial class MainMenu : Form
{
    private void HighScoresButton_Click(object sender, EventArgs e)
    {
        HighScoresMenu HighScoresMenu = new HighScoresMenu(newScore, newPoints, PlayersName);
        HighScoresMenu.Show();
    }

    public static List<Player> GetPlayers(float newScore, float newPoints, string PlayersName)
    {
        var players = new List<Player>();
        var newPlayer = new Player
        {
            Name = PlayersName,
            Points = newPoints,
            Timer = newScore
        };
        players.Add(newPlayer);
        var TopTenLevel1 = players.OrderByDescending(x => x.Timer).Take(10);
        return players;
    }  


public partial class HighScoresMenu : Form
{
    private void HighScoresMenu_Load(object sender, EventArgs e)
    {
        Debug.Print(county.ToString());

        foreach (Player players in MainMenu.GetPlayers(TempnewScore, 
        TempnewPoints, TempPlayersName))
        {
            string TemyPlayersName = "Player's Name";
            float point = 0;
            float time = 0.0f;
            List<string> Allplayers = new List<string>();
            for(int count = 1; count < 6;count++)
            {
             Allplayers.Add(string.Format("{0,-2:0}: {1,-40} : {2,9:0} Points : {3,9:0.0} secs", count, TemyPlayersName, point, time));
            }
            Allplayers[0] = (string.Format("{0,-2:0}: {1,-40} : {2,9:0} Points : {3,9:0.0} secs", 1, players.Name, players.Points, players.Timer));
            ListBoxLevel1.DataSource = Allplayers;
        }
     }
}
  • Are those 2 instance of the game? How do you expect the score to be save when game is close and where? 100% of your question is unrealted to your issue that is Where do I store data so i remember them next time i launch the application.. – xdtTransform Mar 15 '19 at 12:47
  • and we have a dupe for that too https://stackoverflow.com/questions/1941928/best-way-to-store-data-locally-in-net-c – xdtTransform Mar 15 '19 at 12:49

1 Answers1

0

Okay, just to make sure I understood you correctly: you have a list of objects representing high-scores, and you want to update it.

What I suggest is:

  1. Create new high-score object for the player that has completed the game.
  2. Use List<>.Add() method to add it to the list (instead of replacing the object on index 0).
  3. Use List<>.Sort() method to sort the list by points.
  4. If list's length exceeds the desired maximum (say, if you want the high-scores table to have only 10 rows), remove the last elements in the list.
Vindicar
  • 354
  • 2
  • 9
  • Op has `players.OrderByDescending(x => x.Timer).Take(10);` in his code could you explain what it does without using your current answer? – xdtTransform Mar 15 '19 at 12:46
  • An this boils down to what he was told yesterday https://stackoverflow.com/questions/55160165/c-sharp-array-scoring-system – xdtTransform Mar 15 '19 at 12:46
  • It takes 10 values from the players list and orders them in descending order according to the time. – Mohammed Danyal Mar 15 '19 at 13:15
  • the thing is i have used what he told me yesterday and that was based on what type of scoring system should i used however when implementing it now im experiencing some problems – Mohammed Danyal Mar 15 '19 at 13:18