Пример #1
0
 public CellWorld Add(CellWorld toAdd)
 {
     if (toAdd == null)
     {
         return(this);
     }
     foreach (var rowToAdd in toAdd._rows)
     {
         if (_rows.ContainsKey(rowToAdd.Key))
         {
             var mergeRow = _rows[rowToAdd.Key];
             foreach (var colToAdd in rowToAdd.Value)
             {
                 if (mergeRow.ContainsKey(colToAdd.Key))
                 {
                     mergeRow[colToAdd.Key] = mergeRow[colToAdd.Key] | colToAdd.Value;
                 }
                 else
                 {
                     mergeRow.Add(colToAdd.Key, colToAdd.Value);
                 }
             }
         }
         else
         {
             _rows.Add(rowToAdd.Key, rowToAdd.Value);
         }
     }
     return(this);
 }
Пример #2
0
 internal SparseArrayEnumerator(CellWorld world)
 {
     _world = world;
     _rows  = world._rows;
     if (_rows.Count > 0)
     {
         row      = _rows.Values[0];
         rowIndex = 0;
     }
     _version = world._version;
 }
Пример #3
0
        private void LoadPattern()
        {
            if (lstPatterns.SelectedItem == null)
            {
                return;
            }
            var sr    = ((FileInfo)lstPatterns.SelectedItem).OpenText();
            var world = CellWorld.FromRLE(sr);

            StopGame();
            _game.Initialize(new Generation <CellWorld>
            {
                Live = world
            });
            StartGame();
        }
Пример #4
0
 public CellWorld(CellWorld existing)
 {
     _rows = new SortedList <int, SortedList <int, ulong> >(existing._rows.Count);
     foreach (var row in existing._rows)
     {
         var existingRow = row.Value;
         var newRow      = new SortedList <int, ulong>(existingRow.Count);
         _rows[row.Key] = newRow;
         foreach (var key in existingRow.Keys)
         {
             newRow[key] = existingRow[key];
         }
     }
     StartRow    = existing.StartRow;
     StartColumn = existing.StartColumn;
     EndRow      = existing.EndRow;
     EndColumn   = existing.EndColumn;
 }
Пример #5
0
        public CellWorld Remove(CellWorld toRemove)
        {
            if (toRemove == null)
            {
                return(this);
            }
            foreach (var rowToDel in toRemove._rows)
            {
                if (!_rows.ContainsKey(rowToDel.Key))
                {
                    continue;
                }

                var mergeRow = _rows[rowToDel.Key];
                foreach (var colToDel in rowToDel.Value)
                {
                    if (mergeRow.ContainsKey(colToDel.Key))
                    {
                        mergeRow[colToDel.Key] = mergeRow[colToDel.Key] & ~colToDel.Value;
                    }
                }
            }
            return(this);
        }
Пример #6
0
        public static CellWorld FromRLE(StreamReader f)
        {
            var world = new CellWorld();

            var s = f.ReadLine();

            if (s == null)
            {
                return(world);
            }

            while (s != null && s.StartsWith("#"))
            {
                s = f.ReadLine();
            }
            if (s != null && s.StartsWith("x"))
            {
                s = f.ReadLine();
            }

            var row   = 0;
            var col   = 0;
            var count = 0;

            while (s != null)
            {
                foreach (var c in s)
                {
                    if (char.IsDigit(c))
                    {
                        count = (int)(count * 10 + char.GetNumericValue(c)); continue;
                    }

                    switch (c)
                    {
                    case '$':
                        row  += count == 0 ? 1 : count;
                        count = 0;
                        col   = 0;
                        continue;

                    case 'b':
                        col  += count == 0 ? 1 : count;
                        count = 0;
                        continue;

                    case 'o':
                        for (var i = 1; i <= (count == 0 ? 1 : count); i++)
                        {
                            world[col, row] = true;
                            col            += 1;
                        }
                        count = 0;
                        continue;

                    default:
                        break;
                    }
                }

                s = f.ReadLine();
            }

            return(world);
        }