有没有更简单的方法可以制作一个计分表,然后为其位置打分?

时间:2019-06-26 16:27:50

标签: c# unity3d

我正在做一个像《马里奥派对》这样的游戏,在该游戏中,您有几个迷你游戏,然后根据您的位置,您会获得一定数量的积分。我建立了一个系统,在该系统中,您可以按列表对分数进行排序,然后根据您的标点符号或某人的分数相同而获得某些分数。

私人列表highScoreEntryList; // scoretable列表

private class HighscoreEntry //class for the list
{
    public int score;
    public string name;
}

public void AddToList()
{
    //adding puntuations
    highScoreEntryList = new List<HighscoreEntry>() {
         new HighscoreEntry{score = player1score, name = "Player1" },
         new HighscoreEntry{score = player2score, name = "Player2" },
         new HighscoreEntry{score = player3score, name = "Player3" },
         new HighscoreEntry{score = player4score, name = "Player4" }
     };

    //order the puntuation
    for (int i = 0; i < highScoreEntryList.Count; i++)
    {
        for (int j = i + 1; j < highScoreEntryList.Count; j++)
        {
            if (highScoreEntryList[j].score > highScoreEntryList[i].score)
            {
                HighscoreEntry tmp = highScoreEntryList[i];
                highScoreEntryList[i] = highScoreEntryList[j];
                highScoreEntryList[j] = tmp;
            }
        }
    }


    //check player with same puntuations and giving points, the points are directly changed in the score

    for (int i = 0, tmp = 0; i < highScoreEntryList.Count; i++)
    {
        for (int j = i + 1; highScoreEntryList[i].score == highScoreEntryList[j].score; j++)
        {
            tmp++;
            iqual = true;
        }
        if (iqual == true)
        {
            for (int j = i; j <= tmp; j++)
            {
                if (tmp == 1)
                {
                    highScoreEntryList[j].score = 75;
                }
                else if (tmp == 2)
                {
                    highScoreEntryList[j].score = 50;
                }
                else if (tmp == 3)
                {
                    highScoreEntryList[j].score = 25;
                }
                iqual = false;
            }
        }
        else if (iqual == false)
        {
            if (i == 0)
            {
                highScoreEntryList[0].score = 125;
            }
            else if (i == 1)
            {
                highScoreEntryList[1].score = 75;
            }
            else if (i == 2)
            {
                highScoreEntryList[2].score = 50;
            }
            else if (i == 3)
            {
                highScoreEntryList[3].score = 25;
            }

            tmp++;
        }
        i = tmp - 1;

    }

添加了检查功能后,脚本不再起作用。 你可以帮帮我吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Linq及其OrderBy方法。

using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class HighscoreEntry
{
    public int score;
    public string name;
}
public class OrderScores : MonoBehaviour
{
    public List<HighscoreEntry> highScoreEntryList;

    public void Start()
    {
        highScoreEntryList = new List<HighscoreEntry>() {
            new HighscoreEntry{score = 1000, name = "Player5" },
            new HighscoreEntry{score = 1000, name = "Player1" },
            new HighscoreEntry{score = 2222, name = "Player4" },
            new HighscoreEntry{score = 2222, name = "Player2" },
            new HighscoreEntry{score = 1111, name = "Player3" },
            new HighscoreEntry{score = 3333, name = "Player6" }
        };

        List<HighscoreEntry> orderedRoundScores = OrderList(highScoreEntryList);

        foreach (HighscoreEntry entry in orderedRoundScores)
        {
            Debug.Log("Name: " + entry.name + ", Score: " + entry.score);
        }

        List<HighscoreEntry> scores = ScoresBasedOnPosition(orderedRoundScores);


        foreach (HighscoreEntry entry in scores)
        {
            Debug.Log("Name: " + entry.name + ", Score: " + entry.score);
        }
    }

    public List<HighscoreEntry> OrderList(List<HighscoreEntry> list)
    {
        list = list.OrderBy(o => o.name).ToList();
        list = list.OrderByDescending(o => o.score).ToList();
        return list;
    }

    public List<HighscoreEntry> ScoresBasedOnPosition(List<HighscoreEntry> allPlayers)
    {
        int[] positionRelatedScores = new int[] { 125, 75, 50, 25, 10 }; 
        int currentPositionRelatedScoresIndex = 0;

        List<HighscoreEntry> scoresBasedOnPositionList = new List<HighscoreEntry>(); 

        int previousScore = -1; 

        for (int x = 0; x < allPlayers.Count; x++)
        {
            int playerScore = allPlayers[x].score; 

            if (x > 0 && previousScore != playerScore)
            {
                currentPositionRelatedScoresIndex++;
                if (currentPositionRelatedScoresIndex >= positionRelatedScores.Length)
                {
                    break;
                }
            }

            scoresBasedOnPositionList.Add( new HighscoreEntry { name = allPlayers[x].name, score = positionRelatedScores[currentPositionRelatedScoresIndex]});

            previousScore = playerScore; 
        }

        return scoresBasedOnPositionList;
    }

}

在该示例中,它首先按名称对其进行排序,以便现在按字母顺序对匹配分数进行排序,然后按分数降序对它们进行重新排序,从而使匹配名称保持字母顺序。

编辑:添加了新方法ScoresBasedOnPosition来循环显示有序列表,然后根据其在有序列表中的位置以及他们的回合得分是否与先前的玩家回合得分相匹配来分配分数

输出

Name: Player6, Score: 3333
Name: Player2, Score: 2222
Name: Player4, Score: 2222
Name: Player3, Score: 1111
Name: Player1, Score: 1000
Name: Player5, Score: 1000

Name: Player6, Score: 125
Name: Player2, Score: 75
Name: Player4, Score: 75
Name: Player3, Score: 50
Name: Player1, Score: 25
Name: Player5, Score: 25

更多信息,请访问Documentation