示例#1
0
        static Maze GetMaze(string fileName)
        {
            const string RECORD_DELIMITER = "-1";

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var sr = new StreamReader(fs, Encoding.Default))
                {
                    var dimensions = GetDimensions(sr.ReadLine());
                    var maze       = new Maze(dimensions.First(), dimensions.Last());

                    if (sr.ReadLine() != RECORD_DELIMITER)
                    {
                        throw new Exception("Invalid Definition File Format!!");
                    }

                    string line = sr.ReadLine();
                    while (line != RECORD_DELIMITER)
                    {
                        var mirrorRegex   = new Regex("(?:RR|RL|LR|LL|R|L)");
                        var mirrorMatch   = mirrorRegex.Match(line);
                        var mirrorDetails = mirrorRegex.Match(line).Value;

                        Lean           lean = mirrorDetails.Substring(0, 1) == "R" ? Lean.Right : Lean.Left;
                        ReflectiveSide side = mirrorDetails.Length == 2 ? mirrorDetails.Substring(1, 1) == "R" ? ReflectiveSide.Right : ReflectiveSide.Left : ReflectiveSide.Both;

                        var mirrorDimensions = GetDimensions(line.Substring(0, mirrorMatch.Index));
                        maze.AddMirror(new Mirror(lean, side), new Room(mirrorDimensions.First(), mirrorDimensions.Last()));

                        line = sr.ReadLine();
                    }

                    line = sr.ReadLine();
                    var entryRoomRegex      = new Regex("(?:V|H)");
                    var entryRoomMatch      = entryRoomRegex.Match(line);
                    var entryRoomDetails    = entryRoomRegex.Match(line).Value;
                    var entryRoomDimensions = GetDimensions(line.Substring(0, entryRoomMatch.Index));

                    maze.EntryRoom        = new Room(entryRoomDimensions.First(), entryRoomDimensions.Last());
                    maze.LazerOrientation = entryRoomDetails == "V" ? LaserOrientation.Vertical : LaserOrientation.Horizontal;

                    return(maze);
                }
        }