【问题标题】:Array sorting efficiency... Beginner need advice数组排序效率...初学者需要建议
【发布时间】:2011-02-17 23:14:58
【问题描述】:

首先我要说我是一个初学者程序员,这基本上是我使用学习材料之外的第一个真正的项目。我一直在使用 C# 和 XNA 制作“Simon Says”风格的游戏(您重复计算机生成的模式的游戏),实际游戏已经完成并且运行良好,但在创建它时,我还想创建一个“前 10 名记分牌。记分牌会记录玩家姓名、级别(他们完成了多少“回合”)和组合(他们按下了多少按钮正确),然后记分牌将按组合得分排序。这让我第一次使用 XML,最终我得到了一个记录前 10 名分数的 XML 文件。 XML 文件在记分牌类中进行管理,该类还负责添加新分数和对分数进行排序。这让我明白了......我想要一些关于我对分数列表进行排序的方式以及我如何能做得更好的反馈,我没有其他方法可以获得反馈=(。我知道。 NET 特性 Array.Sort() 但我不太清楚如何使用它,因为它不仅仅是一个需要排序的数组。当需要在记分牌中输入新分数时,玩家姓名和级别也会必须添加。这些存储在“数组数组”中(10 = '前 10' 分数)

scoreboardComboData = new int[10]; // Combo

scoreboardTextData = new string[2][];
scoreboardTextData[0] = new string[10]; // Name
scoreboardTextData[1] = new string[10]; // Level as string

记分牌类的工作方式如下:

- 检查“scoreboard.xml”是否存在,如果不存在,则创建它
- 初始化上述数组并添加来自 scoreboard.xml 的任何玩家数据,来自上一次运行
- 当调用 AddScore(name, level, combo) 时排序开始
- 也可以调用另一种方法,用上述数组数据填充 XML 文件

排序检查新分数(组合)是否小于或等于 scoreboardComboData 数组中的任何记录分数(如果大于分数,则移动到下一个元素)。如果是这样,它将小于或等于分数的所有分数向下移动一个元素,实质上是删除最后一个分数,然后将新分数放在小于或等于分数的元素内。如果分数大于所有记录的分数,它将所有分数下移一个并将新分数插入第一个元素中。如果它是唯一的分数,它只是将它添加到第一个元素。添加新分数时,名称和级别数据也会以相同的方式添加到它们的相关数组中。什么绕口令。下面是 AddScore 方法,我添加了 cmets 希望它使事情更清楚 O_o。你可以得到实际的源文件HERE。下面的方法是添加分数以使用调试器执行的最快方法的示例。

    public static void AddScore(string name, string level, int combo)
    {
        // If the scoreboard has not yet been filled, this adds another 'active'
        // array element each time a new score is added. The actual array size is
        // defined within PopulateScoreBoard() (set to 10 - for 'top 10'
        if (totalScores < scoreboardComboData.Length)
            totalScores++;

        // Does the scoreboard even need sorting?
        if (totalScores > 1)
        {
            for (int i = totalScores - 1; i > - 1; i--)
            {
                // Check to see if score (combo) is greater than score stored in 
                // array
                if (combo > scoreboardComboData[i] && i != 0)
                {
                    // If so continue to next element
                    continue;
                }

                // Check to see if score (combo) is less or equal to element 'i'
                // score && that the element is not the last in the
                // array, if so the score does not need to be added to the scoreboard
                else if (combo <= scoreboardComboData[i] && i != scoreboardComboData.Length - 1)
                {
                    // If the score is lower than element 'i' and greater than the last
                    // element within the array, it needs to be added to the scoreboard. This is achieved
                    // by moving each element under element 'i' down an element. The new score is then inserted
                    // into the array under element 'i'
                    for (int j = totalScores - 1; j > i; j--)
                    {
                        // Name and level data are moved down in their relevant arrays
                        scoreboardTextData[0][j] = scoreboardTextData[0][j - 1];
                        scoreboardTextData[1][j] = scoreboardTextData[1][j - 1];
                        // Score (combo) data is moved down in relevant array
                        scoreboardComboData[j] = scoreboardComboData[j - 1];
                    }

                    // The new Name, level and score (combo) data is inserted into the relevant array under element 'i'
                    scoreboardTextData[0][i + 1] = name;
                    scoreboardTextData[1][i + 1] = level;
                    scoreboardComboData[i + 1] = combo;
                    break;
                }

                // If the method gets the this point, it means that the score is greater than all scores within
                // the array and therefore cannot be added in the above way. As it is not less than any score within
                // the array.
                else if (i == 0)
                {
                    // All Names, levels and scores are moved down within their relevant arrays
                    for (int j = totalScores - 1; j != 0; j--)
                    {
                        scoreboardTextData[0][j] = scoreboardTextData[0][j - 1];
                        scoreboardTextData[1][j] = scoreboardTextData[1][j - 1];
                        scoreboardComboData[j] = scoreboardComboData[j - 1];
                    }

                    // The new number 1 top name, level and score, are added into the first element
                    // within each of their relevant arrays.
                    scoreboardTextData[0][0] = name;
                    scoreboardTextData[1][0] = level;
                    scoreboardComboData[0] = combo;

                    break;
                }

                // If the methods get to this point, the combo score is not high enough
                // to be on the top10 score list and therefore needs to break
                break;
            }
        }

        // As totalScores < 1, the current score is the first to be added. Therefore no checks need to be made
        // and the Name, Level and combo data can be entered directly into the first element of their relevant
        // array.
        else
        {
            scoreboardTextData[0][0] = name;
            scoreboardTextData[1][0] = level;
            scoreboardComboData[0] = combo;
        }


    }
}

加分示例:

    private static void Initialize()
    {
        scoreboardDoc = new XmlDocument();
        if (!File.Exists("Scoreboard.xml"))
            GenerateXML("Scoreboard.xml");

        PopulateScoreBoard("Scoreboard.xml");

        // ADD TEST SCORES HERE!
        AddScore("EXAMPLE", "10", 100);
        AddScore("EXAMPLE2", "24", 999);

        PopulateXML("Scoreboard.xml");
    }

在当前状态下,源文件仅用于测试,initialize 在 main 中调用,PopulateScoreBoard 处理大部分其他初始化,所以除了添加测试分数之外不需要其他任何东西。

感谢您的宝贵时间!

【问题讨论】:

    标签: c# arrays algorithm


    【解决方案1】:

    作为编程技巧,您应该将 3 个不同的数组替换为单个分数对象数组,这些对象中的每一个都将具有您提到的所有 3 个属性(名称、级别和分数)。然后,您可以为其创建一个类似比较器的函数(基于 score 属性),该函数可用于通过 Arrays.Sort() 方法对其进行排序。

    如果您想保留当前的 ​​3 个数组,那么您可以在此处查找排序算法:http://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms 并使其对 3 个数组同时进行任何更改,而不是一个一个地进行(因为您保留了数据在它们中按索引同步)。

    【讨论】:

    • 谢谢,我什至没有考虑创建一个对象来存储玩家属性。明天它会给我一些新的玩意:)。
    • @SoleSoft:没问题,确保对象实现了IComparable,就像@P.R.提到并创建比较器函数,否则您将无法使用 Arrays.Sort() 函数对包含它们的数组进行排序。
    【解决方案2】:

    您可以考虑将您的用户名、级别和分数放在一个单独的类(我们称之为 ScoreBoardEntry)中,该类继承自 IComparable,然后编写一个比较器。这将允许您使用 List 的内置排序功能,这将是一个比您自己编写更简洁的解决方案。

    您的代码应如下所示:

    1) 创建/加载列表 与所有以前的分数

    2) 将新分数推入其中

    3) 排序列表

    4) 仅打印前 10 个条目

    【讨论】:

      【解决方案3】:

      我为你写了一些代码。我一直在学习一些新的 C# 类型的东西,比如 linq、IEnumerable 和 yield,并且有点想把它们放在一起,所以希望它会有所帮助。

      您可能会喜欢我使用的 sn-p 编辑器 - linqpad - 非常适合学习/实验

      void Main()
      {
          string [] playerNames = {"Aaron", "Rick", "Josie"};
          var sim = new GameSimulator(playerNames, 1000000, 5000);
          var scores = sim.GetScores(5);
          var sorted = scores.OrderBy(c=>c.Score).Reverse();
          var top = sorted.Take(2);
          // uncomment if using linq pad
          //scores.Dump("scores");
          //sorted.Dump("sorted");
          //top.Dump("top");
      }
      class GameSimulator
      {
          public GameSimulator(string [] playerNames, int maxScore, int maxLevel)
          {
              this.playerNames = playerNames;
              this.maxScore = maxScore;
              this.maxLevel = maxLevel;
          }
          private string [] playerNames;
          private int maxScore;
          private int maxLevel;
          public IEnumerable<ScoreBoardEntry> GetScores(int numGames)
          {
              for (int i = 0; i < numGames; i++)
              {
                  string randomName = playerNames[random.Next(0, playerNames.Length-1)];
                  int randomScore = random.Next(1, maxScore);
                  int randomLevel = random.Next(1, maxLevel);
                  yield return new ScoreBoardEntry(randomName, randomLevel, randomScore);
              }
          }
          static private Random random = new Random();
      }
      class ScoreBoardEntry
      {
          public ScoreBoardEntry(string playerName, int levenNumber, int score)
          {
              PlayerName = playerName;
              LevelNumber = levenNumber;
              Score = score;
          }
          public string PlayerName { set; get; }
          public int LevelNumber { set; get; }
          public int Score { set; get; }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 2022-07-31
        • 2017-08-17
        • 1970-01-01
        • 1970-01-01
        • 2022-11-27
        • 1970-01-01
        相关资源
        最近更新 更多