Question

I have a file containing scores in the following format:

000001,1
000002,2
000012,1
232124,1

I build an array containing the score as the first element and the difficulty as the second element. I then add the array to the list like this:

// load highscores
public void LoadScores()
{
    StreamReader file = new StreamReader("highScores.txt");

    // loop through each line in the file
    while ((line = file.ReadLine()) != null)
    {
        // seperate the score from the difficulty
        lineData = line.Split(',');

        // add each line to the list
        list.Add(lineData);
    }
    OrderScores();

}

Next I order the list using an IEnumerable:

IEnumerable<string[]> scoresDesNormal, scoresDesHard;

// order the list of highscores
protected void OrderScores()
{
    // order the list by descending order with normal difficulty at the top
    scoresDesNormal = list.OrderByDescending(ld => lineData[0]).Where(ld => lineData[1].Contains("1"));
    // order the list by descending order with hard difficulty at the top
    scoresDesHard = list.OrderByDescending(ld => lineData[0]).Where(ld => lineData[1].Contains("2")).Take(3);
}

Now I want to print the two IEnumerable lists to the screen using spriteBatch.DrawString(). How do I correctly iterate through the list elements and the arrays in side of them? I want to print out both elements of each array stored in the lists.

Was it helpful?

Solution

First of all, your question.

If you do know how to draw 1 string, and you want to draw 2 strings combined with a separator between them ",", simply do this:

...
string firstString = ...
string secondString = ...

string toBeDrawn = firstString + "," + secondString;
DrawThisFellah ( toBeDrawn );

...  

So, you have a string[] ? No problem:

...
string[] currentElement = ...

string firstString = currentElement[0];
string secondString = currentElement[1];
// etc
...

Secondly, you don't need to add the leading zeros just for the alphabetical OrderByDescending to work. You could parse (and thus validate) the strings and store them in elements of type int[] instead of string[].

THIRD, and this might interest you !

It is dangerous to use LINQ without knowing you're using it. Are you aware that your IEnumerable<string[]> are actually queries, right ?

One could say that your IEnumerable<string[]> objects are actually "CODE AS DATA". They contain not the result for your OrderByDescending but rather the logic itself.

That means that if you have say, 3 string[] elements in your lineData:

{ "1", "122" }, { "3", "42" }, { "5", "162" }

and you call OrderScores, then you will notice the results when enumerating scoresDesNormal (for instance). The following code:

foreach (var element in scoresDesNormal)
    Console.WriteLine("{0}, {1}", element[0], element[1]);

would print out to the console:

1, 122
5, 162

(because the 2nd element has no '1' characters on the second subelement)

but if you go on and modify the lineData collection either by inserting new elements or removing elements (should it be a List<string[]>) or simply by modifying the values of existing elements (should it be a primitive array), then you will observe new results when enumerating scoresDesNormal without calling OrderScores !

So for instance the following code's comments are true (given you have a List<string[]> lineData):

List<string[]> lineData = new List<string[]>(new[] {
    new[] { "1", "122" }, // contains 1
    new[] { "3", "42" },  // does not contain 1
    new[] { "5", "162" }  // contains 1
});

OrderScores();

foreach (var element in scoresDesNormal)
    Console.WriteLine(element[1]);
// the previous foreach prints out
//    162
//    122

lineData[1][1] = "421"; // make the "42" in the list become "421"

foreach (var element in scoresDesNormal)
    Console.WriteLine(element[1]);
// the previous foreach prints out - even though we haven't called OrderScores again
//    162
//    421
//    122

OTHER TIPS

If i were you, i would do it like this,

If the user clicks or presses a button to view scores, open a method, like this one.

public void DisplayScores()
{
Rectangle[] PositionOfText = new Rectangle[scoresDesNormal];
for (int i = 0; i< scoresDesNormal.Count; i++)
{
PositionOfText = new Rectangle(xPos, yPos, Width, Height);
}
//do the same for the other difficulties, this sets the position of the texts.

import a fontbatch that you would like to use. and in your draw method have something like this,

for (int i = 0; i< scoresDesNormal.Count; i++)
spritebatch.drawString(MyFontBatch, scoresDesNormal[i], PositionOfText, Color.//your preffered color.

Now i can understand that their may be errors in that, but i'm not a fluent c# programmer, But thats more or less what you can do

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top