Ejemplo n.º 1
0
 //reads in the file and populates the script structure with it
 static Script parseFile(string fname)
 {
     Script script = new Script();
     script.sentences = new List<Sentence>();
     script.roomToSentenceNumber = new Dictionary<string, int>();
     script.currentPositions = new List<Coord>();
     script.done = false;
     script.lastInput = "";
     script.switches = new Dictionary<string, bool>();
     using (StreamReader reader = new StreamReader(fname))
     {
         string line;
         int lineno; //used for storing rooms
         for (line = reader.ReadLine(), lineno = 0; line != null; line = reader.ReadLine(), ++lineno)
         {
             if (line == "") //ignore empty lines
             {
                 lineno = lineno - 1;
                 continue;
             }
             if (line[0] == '>') //auto-replace the ">" "with println "
             {
                 line = line.Replace(' ', '+');
                 line = "println " + line.Substring(1) + "+++++"; //so that you can '>' an empty line
             }
             string[] linearray = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
             if(linearray.Length % 2 == 0) //make sure there are only valid sentences with valid phrases
             {
                 //proceed as normal, the sentence is well formed
                 Sentence s = new Sentence();
                 for(int i = 0; i < linearray.Length; i = i + 2) //add all the phrases to the sentence
                 {
                     Phrase p = new Phrase();
                     p.command = linearray[i];
                     p.info = linearray[i + 1].Replace('+', ' ');
                     s.Add(p);
                     if(p.command != "room") { ; }
                     else script.roomToSentenceNumber.Add(p.info, lineno); //add rooms to the rooms dictionary
                 }
                 script.sentences.Add(s);
             }
             else
             {
                 //oops, the sentence has an odd number of words.  Die gracefully...
                 InvalidSentenceException e = new InvalidSentenceException();
                 e.sentence = line;
                 throw e;
             }
         }
     }
     Coord c = new Coord();
     c.sentenceIndex = 0;
     c.phraseIndex = 0;
     script.currentPositions.Add(c);
     return script;
 }
Ejemplo n.º 2
0
        //reads in the file and populates the script structure with it
        static Script parseFile(string fname)
        {
            Script script = new Script();

            script.sentences            = new List <Sentence>();
            script.roomToSentenceNumber = new Dictionary <string, int>();
            script.currentPositions     = new List <Coord>();
            script.done      = false;
            script.lastInput = "";
            script.switches  = new Dictionary <string, bool>();
            using (StreamReader reader = new StreamReader(fname))
            {
                string line;
                int    lineno; //used for storing rooms
                for (line = reader.ReadLine(), lineno = 0; line != null; line = reader.ReadLine(), ++lineno)
                {
                    if (line == "") //ignore empty lines
                    {
                        lineno = lineno - 1;
                        continue;
                    }
                    if (line[0] == '>') //auto-replace the ">" "with println "
                    {
                        line = line.Replace(' ', '+');
                        line = "println " + line.Substring(1) + "+++++"; //so that you can '>' an empty line
                    }
                    string[] linearray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (linearray.Length % 2 == 0) //make sure there are only valid sentences with valid phrases
                    {
                        //proceed as normal, the sentence is well formed
                        Sentence s = new Sentence();
                        for (int i = 0; i < linearray.Length; i = i + 2) //add all the phrases to the sentence
                        {
                            Phrase p = new Phrase();
                            p.command = linearray[i];
                            p.info    = linearray[i + 1].Replace('+', ' ');
                            s.Add(p);
                            if (p.command != "room")
                            {
                                ;
                            }
                            else
                            {
                                script.roomToSentenceNumber.Add(p.info, lineno);  //add rooms to the rooms dictionary
                            }
                        }
                        script.sentences.Add(s);
                    }
                    else
                    {
                        //oops, the sentence has an odd number of words.  Die gracefully...
                        InvalidSentenceException e = new InvalidSentenceException();
                        e.sentence = line;
                        throw e;
                    }
                }
            }
            Coord c = new Coord();

            c.sentenceIndex = 0;
            c.phraseIndex   = 0;
            script.currentPositions.Add(c);
            return(script);
        }