【发布时间】: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 处理大部分其他初始化,所以除了添加测试分数之外不需要其他任何东西。
感谢您的宝贵时间!
【问题讨论】: