【问题标题】:Scoreboard .txt scoreboard making for a tetris game为俄罗斯方块游戏制作记分牌 .txt 记分牌
【发布时间】:2015-02-01 20:55:03
【问题描述】:

我正在 c# 控制台中制作俄罗斯方块游戏。我已经完成了大部分游戏,但我被文件处理困住了。我还没有真正找到任何与此相关的东西,所以我想我可以试一试并询问它。 所以我要做的是将玩家的姓名和分数保存为 NAME:SCORE 的 txt 文件,然后以某种方式按分数对其进行排序,然后将前十名打印为记分牌。 这是我走了多远:

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

【问题讨论】:

    标签: c# .net linq sorting io


    【解决方案1】:

    与其编写然后读取/解析 .txt 文件,更简单的选择是创建一个类来保存您的球员姓名和分数,然后将其序列化为 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 文件的对象。

    【讨论】:

    • 好的,谢谢我还没教过 XML 我明天试试。
    • @HambalkóBence:这比创建和解析自己的格式要容易得多,而且肯定要容易得多。另请参阅我关于如何更好地管理您的姓名和分数的编辑。
    【解决方案2】:

    你可以通过下面的代码使用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);
    }
    

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

    【讨论】:

    • 谢谢。这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多