View Single Post
Old 2013-08-23, 03:13   Link #5
Jinto
Asuki-tan Kairin ↓
 
 
Join Date: Feb 2004
Location: Fürth (GER)
Age: 43
Ah scratch my idea with the loop variant, because you already have a predefined currentCard array. Just go for the first thing with the stride length of two.

Besides, I would have written a line parser class that does all the magic of reading one line and used this to read files line by line in a file reader method in the main class.

Because you cannot optimize the way you read the file, but the line parsing is kind of a different concern (see design patter: "Seperation of Concerns")

edit: one more methodical thing I wanted to add

When you parse a line you should not constrain its length in the result array. But the length should be constraint by the actual input line. Something like

Code:
public List<Card> GetAllCardsInLine(string line)
{
 List<Card> allCardsInLine = new List<Card>();
 string[] parts = line.Split(' ');
 if(parts.Length%2 != 0)
 {
   Console.WriteLine("The line " + line + " cannot be parsed.");
   return allCardsInLine;
 }
 for(int i=0; i<parts.Length; i++)
 {
   if(i%2 == 0)
   {
      Card newEntry;
      newEntry.firstSuit = parts[i][0];  //this is already a substring simply access the char at index 0 (or does it use ASCII encoding or smth like that?)
      allCardsInLine.Add(newEntry);
   }
   else
   {
     try
     {
      allCardsInLine[i/2].firstFace = Convert.ToInt32(parts[i]);
     }
     catch(ConversionException ex)
     {
        Console.WriteLine("The line " + line + " cannot be parsed.");
        Console.WriteLine("Parsing the number: " + parts[i] + "at position " + i.ToString() + " threw an exception.";
        return allCardsInLine;
     }
   }
 }
 return allCardsInLine;
}
edit: I replaced the wrong code with parts[i][0]
__________________
Folding@Home, Team Animesuki

Last edited by Jinto; 2013-08-23 at 11:26.
Jinto is offline   Reply With Quote