[英]C# Array Scoring System
Im having problems with sorting out the scoring system for my game. 我在为我的游戏挑选计分系统时遇到问题。 The problem arrises when i sort out the "HighScoresPointsLevel1" array because despite the array being displayed in the correct order it has no link with the "HighScoresNameLevel1".
当我整理“ HighScoresPointsLevel1”数组时,问题浮出水面,因为尽管该数组以正确的顺序显示,但它与“ HighScoresNameLevel1”没有链接。 In other words a high score for the game would be assigned to a random players name.
换句话说,游戏的高分将分配给一个随机的玩家名称。
Possible solution? 可能的解决方案? I was thinking that if i was able to pass two parameters (HighScoresPointsLevel1 , HighScoresNameLevel1) inside of the array.sort/array.reverse but the problem is that array.reverse dosent accept two parameters.
我在想如果我能够在array.sort / array.reverse内部传递两个参数(HighScoresPointsLevel1,HighScoresNameLevel1),但是问题是array.reverse剂量接受两个参数。 The reason why i thought this would work is because by calling array.sort the first time i was able to establish a relationship between the arrays "HighScoresLevel1", "HighScoresNameLevel1".
我认为这行得通的原因是,通过第一次调用array.sort,我能够在数组“ HighScoresLevel1”,“ HighScoresNameLevel1”之间建立关系。
Is there any possible solutions to this issue. 有没有可能解决此问题的方法。 Any Help would be appreciated.
任何帮助,将不胜感激。 Thanks.
谢谢。
string PlayersName = "Player's Name";
float[] HighScoresLevel1 = new float[5];
float[] HighScoresPointsLevel1 = new float[5];
string[] HighScoresNameLevel1 = new string[5];
public static void addLastScoreLevel1(
float newScore,
float newPoints,
float[] HighScoresLevel1,
float[] HighScoresPointsLevel1,
string[] HighScoresNameLevel1,
string PlayersName)
{
if (newScore < HighScoresLevel1[4])
{
HighScoresLevel1[4] = newScore;
HighScoresPointsLevel1[4] = newPoints;
HighScoresNameLevel1[4] = PlayersName;
Array.Sort(HighScoresLevel1, HighScoresNameLevel1);
Array.Sort(HighScoresPointsLevel1);
Array.Reverse(HighScoresPointsLevel1);
}
}
Edit: After looking at the feedback from "Kieran Devlin", i have implemented the changes and so far so good , but im having problems printing the list. 编辑:查看来自“ Kieran Devlin”的反馈后,我已经实现了更改,到目前为止一切都很好,但是我在打印列表时遇到了问题。 So within my other form when trying to display the content of the players list within a the list box, the list box only displays Game.Player.
因此,在我尝试显示列表框中的玩家列表内容的其他表单中,列表框仅显示Game.Player。
public partial class MainMenu : Form
{
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
{
foreach (var Player in MainMenu.GetPlayers(newScore, newPoints,
PlayersName))
{
ListBoxLevel1.Items.Add(Player);
}
}
Use objects to group data that will give you more context 使用对象对数据进行分组,这将为您提供更多上下文
public class Player {
public string Name { get; set; }
public int Points { get; set; }
public int Level { get; set; }
}
Then you can use it like so: 然后,您可以像这样使用它:
var players = new List<Player>();
var newPlayer = new Player {
Name = "Some name",
Points = 10,
Level = 3
};
highscore.Add(newPlayer);
And if you want to get the top ten players by a field: 如果您想按某个领域获得前十名的球员:
var topTenByLevel = players
.OrderByDecending(x => x.Level)
.Take(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.