游戏的高分文件

时间:2018-01-10 01:29:59

标签: c#

对于我的游戏,我有一个班级"高分"它包含执行以下操作所需的代码: 检查用户是否具有高分,如果他们当前的游戏内得分超过此值则替换它。 如果他们没有高分,那就写下他们刚刚取得的成绩。 这将全部采用格式名称| highscore。

我目前已经做过(不要判断新编码)

    public void WriteAllHS()
    {
        if (File.Exists("AllHighscores.txt")) //Check if file exists
        {
            if (Player.score > highscore)    //Check if the new score exceeds the current High score                                                
            {
                string[] FileContents = 
                File.ReadAllLines(@"AllHighscores.txt");
                string[] NS = { "" };       //Removes the |

                for (int l = 0; l < FileContents.Length; l++)
                {
                    NS = FileContents[l].Split('|'); 
                }
                int k = 0;        //check what place in the array the logged in users' highscore is and saves it and checks if they have a highscore at all
                for (int i = 0; i < NS.Length; i = i + 2)
                {
                    if (Player.name == NS[i])
                    {
                        k = i;
                        check = true;
                    }
                }

                //If they have a highscore and their new score is bigger it will: 1)delete the file 2)replace the old high score with the new bigger one 3)Writes all high scores to the new file

                if (check == true && Convert.ToInt32(NS[k+1]) < Player.score)
                {
                    File.Delete("AllHighscores.txt");
                    NS[k+1] = Convert.ToString(Player.score);
                    for (int a = 0; a < NS.Length; a = a + 2)
                    {
                        string[] holder = new string[2];
                        holder[0] = NS[a];
                        holder[1] = NS[a + 1];
                        using (StreamWriter writer = new StreamWriter(@"AllHighscores.txt", true))
                        {
                            writer.WriteLine(holder[0] + "|" + holder[1]);
                        }
                    }
                }

            }
            if (check == false)
            {
                using (StreamWriter writer = new StreamWriter(@"AllHighscores.txt", true))
                {
                    writer.WriteLine(Player.name + "|" + Player.score);
                }
            }

        }
        else
        {
            using (StreamWriter writer = new StreamWriter(@"AllHighscores.txt", true))
            {
                writer.WriteLine(Player.name + "|" + Player.score);
            }
        }

    }
}

目前它将替换1个用户的文件中的分数,但如果为另一个用户尝试,它将删除除新编写的用户之外的所有用户高分。 在此先感谢。

0 个答案:

没有答案
相关问题