如何正确排序我的xml文件?

时间:2017-05-09 14:31:02

标签: c# xml visual-studio sorting xna

我的游戏制作有问题。我试图排序高分榜,但它没有按预期工作。当我保存我在比赛中得到的分数时,我想通过高分榜查看并检查它是否高于列表中的分数,如果是,则将新的高分放在列表中并将其他分数下移到列表中。 代码如下所示:

public void SaveHighScore(int score)
    {
        // Create the data that needs to be saved
        HighScore data = LoadHighScores(HighScoresFilename);
        int scoreIndex = 0;
        for (int i = 4; i >= 0; i--)
        {
            if (score >= data.Score[i])
            {
                scoreIndex = i;
                break;
            }
        }

        if (scoreIndex >= 0)
        {
            int[] tempScore = new int[5];
            for (int i = 4; i >= scoreIndex; i--)
            {
                tempScore[i] = data.Score[i];
            }

            tempScore[scoreIndex] = score;

            for (int i = scoreIndex; i > 0; i--)
            {
                tempScore[i - 1] = data.Score[i];
            }
            for(int i = 0; i < data.Score.Length; i++)
                data.Score[i] = tempScore[i];


            SaveHighScores(data, HighScoresFilename);
        }
    }

我的班级看起来像这样:https://github.com/PalmerVincent/Game/projects

问题是如果将新的高分数放入数组中,则高分列表无法正确排序。

2 个答案:

答案 0 :(得分:0)

如果你的xml格式不是问题,我建议你可以创建一个简单的Player类并使用List ...

public class Player
{
        public string Name { get; set; }
        public int Score { get; set; }
}

public int Count
public List<Player> Players

public HighScore()
{  
  Players = new List<Player>();
}

//your initialize
public static void Initialize()
{
   string fullpath = "";
   HighScore data;

   if (!File.Exists(fullpath))
   {

            Player player = new Player { Name = "Neil", Score = 20 };
            Player player1 = new Player { Name = "Shawn", Score = 15 };
            Player player2 = new Player { Name = "Mark", Score = 10};
            Player player3 = new Player { Name = "Cindy", Score = 5 };
            Player player4 = new Player { Name = "Sam", Score = 1 };
            data.Players.Add(player);
            data.Players.Add(player1);
            data.Players.Add(player2);
            data.Players.Add(player3);
            data.Players.Add(player4);

            SaveHighScores(data, fullpath);
   }
}

 public static void SaveHighScore(int score, string name)
 {
        HighScore data = LoadHighScores(HighScoresFilename);

        var player = data.Players.FirstOrDefault(x => x.Name == "Vincent");

        // new player in town
        if(player == null)
        {
            Player newPlayer = new Player { Name = "Vincent", Score = score };

            data.Players.Add(newPlayer);

            data.Players = data.Players.OrderByDescending(o=>o.Score).ToList();
        }
        else
        {
            //set new score
            player.Score = score;
            data.Players = data.Players.OrderByDescending(o => o.Score).ToList();
        }

        data.Count = data.Players.Count;
        SaveHighScores(data, HighScoresFilename);
}

并检查你的xml

HighScore.SaveHighScore(12, "Vincent");

答案 1 :(得分:0)

我建议你在分拣时不要重新发明轮子。 C#提供了一些排序方法,还有一些开源项目,您可以使用它们。

但是,在你的情况下,你不是排序。您有一个简单的列表,列表顶部的得分最高。其余的按分数值的降序排列。

因此,MaxScore将从canar's Player类借用。

public int MaxScore { return list[0].Score; }
public int MaxScorer { return list[0].Name; }

借用创建的方法定义canar

public static void SaveHighScore(int score, string name) 
{
    var list = LoadHighScores();
    // verify score is > first score to make sure list stays sorted
    if (list.Count > 0 && score <= list[0].Score)
        return; // invalid high score

    Player player = list.FirstOrDefault(p => p.Name == name);
    if (player != null)
    {
        list.Remove(player);
        player.Score = score;
    }
    else
        player = new Player { Name = name, Score = score };
    list.InsertAt(0, player);
    SaveHighScores(list);
}