Beispiel #1
0
        public IEnumerable <IMinefield> Load()
        {
            IList <IMinefield> minefields = new List <IMinefield>();

            var regex = new Regex(@"^(?<rows>\d+),(?<cols>\d+);(?<data>[*.]+)$", RegexOptions.Compiled);

            string[] lines = File.ReadAllLines(m_path);

            // Each line in the field yields a unique minefield
            foreach (string line in lines)
            {
                var match = regex.Match(line);

                if (match.Success)
                {
                    int    rows = int.Parse(match.Groups["rows"].Value);
                    int    cols = int.Parse(match.Groups["cols"].Value);
                    string data = match.Groups["data"].Value;

                    Minefield field = new Minefield(cols, rows);

                    // Enumerate mine areas and mine data
                    IEnumerator <IMineArea> areaEnum = field.AreasByRow().GetEnumerator();
                    IEnumerator <char>      dataEnum = data.GetEnumerator();

                    while (areaEnum.MoveNext() && dataEnum.MoveNext())
                    {
                        areaEnum.Current.ContainsMine = HasMine(dataEnum.Current);
                    }

                    minefields.Add(field);
                }
            }

            return(minefields);
        }