C#数组评分系统 [英] C# Array Scoring System

查看:103
本文介绍了C#数组评分系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的游戏选择计分系统时遇到问题.当我整理"HighScoresPointsLevel1"数组时,问题浮出水面,因为尽管该数组以正确的顺序显示,但它与"HighScoresNameLevel1"没有任何链接.换句话说,游戏的高分将分配给一个随机的玩家名称.

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". In other words a high score for the game would be assigned to a random players name.

可能的解决方案?我在想如果我能够在array.sort/array.reverse内部传递两个参数(HighScoresPointsLevel1,HighScoresNameLevel1),但是问题是array.reverse剂量接受两个参数.我认为这行得通的原因是,通过第一次调用array.sort,我能够在数组"HighScoresLevel1","HighScoresNameLevel1"之间建立关系.

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. 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".

此问题是否有任何可能的解决方案.任何帮助,将不胜感激.谢谢.

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);
        }
     }

在查看了"Kieran Devlin"的反馈后,我已经实现了更改,到目前为止一切都很好,但是在打印列表时出现了问题. 因此,在我尝试显示列表框内玩家列表内容的其他表单中,列表框仅显示Game.Player.

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. 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.

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);

这篇关于C#数组评分系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆