1

私はビジュアル ベーシックの初心者で、空き時間に小さなプロジェクト用のテキスト ベースのゲームを作成しています。ゲームにはスコアリング システムがあり、ゲーム終了時にユーザーのスコアがテキスト ファイルに保存されます。テキストを追加するのは難しくないと確信していますが、ファイルに書き込むコードを書いていません。私が抱えている問題は、ハイスコアを表示することです。それらを読み込むことができ、Split(",") を使用できます。結果を素敵なテーブルに表示することもできます。私が抱えている問題は、実際のスコアの順にハイスコアを表示することです。これは、スコア テーブルを作成するために必要なコードです。出力))

    Dim FStrm As FileStream
    Dim StrmR As StreamReader
    FStrm = New FileStream("HighScores.txt", FileMode.Open)
    StrmR = New StreamReader(FStrm)
    Dim highScores As New List(Of String)

    While StrmR.Peek <> -1
        highScores.Add(StrmR.ReadLine)
    End While

    FStrm.Close()

    Console.WriteLine("       __________________________________________________________________ ")
    Console.WriteLine("      |       Score       |       Name                                   |")
    Console.WriteLine("      |-------------------|----------------------------------------------|")
    Dim Scores() As String
    For Each score As String In highScores
        Scores = score.Split(",")
        Console.WriteLine("      |  {0}  |  {1}    |", Pad(Scores(0), 15), Pad(Scores(1), 40))
    Next
    Console.WriteLine("      |___________________|______________________________________________| ")

以下はテキストファイルの例です。

2,Zak
10000,Charlie
9999,Shane
90019,Rebecca

誰かがスコアで行を並べ替える方法を見つけるのを手伝ってくれませんか? まったく別のアプローチを取る必要があるのでしょうか? どうもありがとうございました!

-チャーリー

4

1 に答える 1

0

私はC#の人ですが、ここに行きます:

Dim scores As List(Of UserScore)
Dim lines As String()
'Read in all lines in one hit.
lines = File.ReadAllLines("HighScores.txt")
scores = New List(Of UserScore)

For Each line As String In lines
    Dim tokens As String()
    'Split each line on the comma character.
    tokens = line.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

    'Create a new UserScore class and assign the properties.
    Dim userScore As UserScore
    userScore = New UserScore()
    userScore.Name = tokens(1)
    userScore.Score = Int32.Parse(tokens(0))

    'Add to the list of UserScore objects.
    scores.Add(userScore)
Next

'Sort them by descending order of score. To sort in the other
'direction, remove the Descending keyword.
scores = (From s In scores Order By s.Score Descending).ToList()

値を保持するには、このクラスが必要です。私は、Scoreが常に整数であると想定しました。それが他の何かである場合、このフィールドとInt32.Parse呼び出しはそれに合わせて調整する必要があります。

Class UserScore
    Property Name As String
    Property Score As Int32
End Class

これがどれだけ堅牢である必要があるかに応じて、ファイルが正常に開かれること、Int32.Parse呼び出しが機能すること(TryParseこの場合はメソッドの方が良い)、およびline.Split呼び出しが2つの値を持つ配列を返すことを確認することもできます。そうでなければ、それはトリックを行う必要があります。

于 2012-10-15T01:07:03.773 に答える