俄罗斯方块游戏的记分牌 .txt 记分牌

Scoreboard .txt scoreboard making for a tetris game

我正在用 C# 控制台制作俄罗斯方块游戏。我已经完成了游戏的大部分内容,但在文件处理方面遇到了困难。我还没有真正找到任何与此相关的东西,所以我想我可以试一试并问问它。 所以我想做的是将玩家的名字和分数保存到一个 txt 文件中作为 NAME:SCORE 然后以某种方式按分数排序然后打印前十名作为记分牌。 这是我的进展情况:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace scoreb
{
    class Program
{
     private static Random _random = new Random();
        private static ConsoleColor GetRandomConsoleColor()
        {
            var consoleColors = Enum.GetValues(typeof(ConsoleColor));
             return (ConsoleColor)consoleColors.GetValue(_random.Next(consoleColors.Length));
        }
    static void Main(string[] args)
    {
        int n = 10;
        string[] names;
        string[] name = new string[n];
        string[] score = new string[n];
        int i = 0;
        while(name[i] != "*"){
            Console.WriteLine("Supply your name please!!");
            name[i] = Convert.ToString(Console.ReadLine());
            Console.WriteLine("Give your score!");
            score[i] = Convert.ToString(Console.ReadLine());
            Console.WriteLine(name[i] + score[i]);
            names = new string[] { name[i] + "            " + score[i] };
            i++;
        }

        Console.ReadLine();
        //Printout
        Console.Clear();
        Console.WriteLine();
        Console.ForegroundColor = GetRandomConsoleColor();
        Console.WriteLine("                   *****************************");
        Console.ForegroundColor = GetRandomConsoleColor();
        Console.WriteLine("                              HIGH SCORES");
        Console.ForegroundColor = GetRandomConsoleColor();
        Console.WriteLine("                   *****************************");
        Console.WriteLine();
        string[] lines = File.ReadAllLines("C:\asd.txt");
        Array.Sort(lines);
        string read = null;
        StreamReader b = File.OpenText("C:/asd.txt");
        Console.WriteLine("                   Név              Pont");
        while ((read = b.ReadLine()) != null)
        {
            int j = 0;
            Console.WriteLine();
            Console.ForegroundColor = GetRandomConsoleColor();
            Console.WriteLine("                   " + (j + 1) + ". " + lines[j]);
            Console.WriteLine();
            j++;
        }
        b.Close();
        Console.ReadLine();
    }
  }
}   

与其编写然后 reading/parsing 一个 .txt 文件,一个更简单的选择是创建一个 class 来保存您的玩家姓名和分数,然后将其序列化为 XML 和然后将其保存到文件中。看这里:How to write object data to XML file

要读取 XML 文件并将其反序列化为对象,see this article(与上述相关)。

此外,要管理您的姓名和分数,请不要这样做:

    string[] name = new string[n];
    string[] score = new string[n];

做这样的事情:

public class NameAndScore
{
    public string Name { get; set; }
    public int Score { get; set; }
}

private List<NameAndScore> _namesAndScores = new List<NameAndScore>();

List<> 将是您要序列化和反序列化到 XML 文件的对象。

你可以使用 linq 来实现你想要的,使用下面的代码

var scores = File.ReadAllLines("C:\asd.txt")
         .Select(x => x.Split(":".ToCharArray())) // split name and score
         .Select(x => new
         {
             Name = x[0],
             Score = int.Parse(x[1])
         }) // create an object contains name and score for each line
         .OrderByDescending(x => x.Score) // sort result by score
         .Take(10); //get top 10

然后在 foreach 循环中打印结果

foreach (var score in scores)
{
    Console.WriteLine(score.Score + "  " + score.Name);
}

但它有一些限制。如果用户名包含 : 那么这将失败,因为拆分部分将失败。