Пример #1
0
        public bool HasConnectionTo(GridAtom atom)
        {
            if (atom == null)
            {
                throw new ArgumentNullException(nameof(atom));
            }

            return
                (LeftConnection == atom ||
                 RightConnection == atom ||
                 TopConnection == atom ||
                 BottomConnection == atom);
        }
Пример #2
0
        private void ConnectAtoms(GridAtom source, GridAtom target)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (source == target || source.Electrons <= 0 || target.Electrons <= 0)
            {
                return;
            }

            if (source.RightAtom == target && source.RightConnection == null)
            {
                source.Electrons--;
                source.RightConnection = target;

                target.Electrons--;
                target.LeftConnection = source;
            }
            else if (source.LeftAtom == target && source.LeftConnection == null)
            {
                source.Electrons--;
                source.LeftConnection = target;

                target.Electrons--;
                target.RightConnection = source;
            }
            else if (source.TopAtom == target && source.TopConnection == null)
            {
                source.Electrons--;
                source.TopConnection = target;

                target.Electrons--;
                target.BottomConnection = source;
            }
            else if (source.BottomAtom == target && source.BottomConnection == null)
            {
                source.Electrons--;
                source.BottomConnection = target;

                target.Electrons--;
                target.TopConnection = source;
            }
        }
Пример #3
0
        /// <summary>
        /// Tries to set the given atom to the given grid position.
        /// </summary>
        public bool SetAtom(int gridX, int gridY, Atom atom)
        {
            if (atom == null)
            {
                throw new ArgumentNullException(nameof(atom));
            }

            if (CanSet(gridX, gridY, atom))
            {
                _atoms[gridX, gridY] = new GridAtom(this, gridX, gridY, atom.Electrons);
                AtomAdded(_atoms[gridX, gridY]);
                return(true);
            }

            return(false);
        }
Пример #4
0
        /// <summary>
        /// Checks if a molecule is complete (has no more electrons) and can be removed.
        /// </summary>
        private bool CheckMolecule(GridAtom startAtom)
        {
            if (startAtom.Electrons > 0)
            {
                return(false);
            }

            var moleculeAtoms = new HashSet <Point>();
            var openList      = new Stack <Point>();
            var closedList    = new HashSet <Point>();

            openList.Push(startAtom.GridPos);
            while (openList.Count > 0)
            {
                var currentPos  = openList.Pop();
                var currentAtom = GetAtom(currentPos.X, currentPos.Y);

                if (currentAtom.Electrons > 0)
                {
                    return(false);
                }

                closedList.Add(currentPos);
                moleculeAtoms.Add(currentPos);

                if (currentAtom.LeftConnection != null && !closedList.Contains(currentAtom.LeftConnection.GridPos))
                {
                    openList.Push(currentAtom.LeftConnection.GridPos);
                }
                if (currentAtom.RightConnection != null && !closedList.Contains(currentAtom.RightConnection.GridPos))
                {
                    openList.Push(currentAtom.RightConnection.GridPos);
                }
                if (currentAtom.TopConnection != null && !closedList.Contains(currentAtom.TopConnection.GridPos))
                {
                    openList.Push(currentAtom.TopConnection.GridPos);
                }
                if (currentAtom.BottomConnection != null && !closedList.Contains(currentAtom.BottomConnection.GridPos))
                {
                    openList.Push(currentAtom.BottomConnection.GridPos);
                }
            }

            RemoveMoleculeAtoms(moleculeAtoms);
            return(true);
        }
Пример #5
0
        private bool CanConnectTo(GridAtom target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (Electrons <= 0)
            {
                return(false);
            }

            if (target.Electrons <= 0)
            {
                return(false);
            }

            var differenceX = target.GridX - GridX;
            var differenceY = target.GridY - GridY;

            if (MathI.Abs(differenceX) + MathI.Abs(differenceY) != 1)
            {
                return(false);
            }

            if (differenceX == -1)
            {
                return(LeftConnection == null);
            }
            else if (differenceX == 1)
            {
                return(RightConnection == null);
            }
            else if (differenceY == -1)
            {
                return(TopConnection == null);
            }
            else if (differenceY == 1)
            {
                return(BottomConnection == null);
            }

            return(false);
        }
Пример #6
0
 private void RenderConnections(SpriteBatch batch, Vector2 pos, GridAtom atom, float layerDepth)
 {
     if (atom.LeftConnection != null)
     {
         batch.DrawTextureAtlasRegion(_grid.Contents.HConnection,
                                      pos: new Vector2(
                                          pos.X + atom.GridX * _grid.TileSize,
                                          pos.Y + atom.GridY * _grid.TileSize + _grid.TileSize / 2),
                                      layerDepth: layerDepth);
     }
     if (atom.TopConnection != null)
     {
         batch.DrawTextureAtlasRegion(_grid.Contents.VConnection,
                                      pos: new Vector2(
                                          pos.X + atom.GridX * _grid.TileSize + _grid.TileSize / 2,
                                          pos.Y + atom.GridY * _grid.TileSize),
                                      layerDepth: layerDepth);
     }
 }
Пример #7
0
        private void AtomAdded(GridAtom addedAtom)
        {
            if (addedAtom == null)
            {
                throw new ArgumentNullException(nameof(addedAtom));
            }

            addedAtom.ConnectToNeighbours();

            for (int gridX = addedAtom.GridX - 1; gridX < addedAtom.GridX + 1; gridX++)
            {
                for (int gridY = addedAtom.GridY - 1; gridY < addedAtom.GridY + 1; gridY++)
                {
                    var atom = GetAtom(gridX, gridY);
                    if (atom != null && atom != addedAtom)
                    {
                        atom.ConnectToNeighbours();
                    }
                }
            }

            CheckMolecule(addedAtom);
        }
Пример #8
0
        public void FromSaveGame(SaveGameGridData[,] gridData)
        {
            if (gridData == null)
            {
                throw new ArgumentNullException(nameof(gridData));
            }

            var width  = gridData.GetLength(0);
            var height = gridData.GetLength(1);

            if (width != _width || height != _height)
            {
                throw new ArgumentException("Given grid data array has invalid dimensions.");
            }

            Clear();
            for (int gridX = 0; gridX < width; gridX++)
            {
                for (int gridY = 0; gridY < height; gridY++)
                {
                    var data = gridData[gridX, gridY];
                    if (data != null)
                    {
                        _atoms[gridX, gridY] = new GridAtom(this, gridX, gridY, data.Electrons);
                    }
                }
            }

            for (int gridX = 0; gridX < width; gridX++)
            {
                for (int gridY = 0; gridY < height; gridY++)
                {
                    var data = gridData[gridX, gridY];
                    if (data != null)
                    {
                        var atom = _atoms[gridX, gridY];

                        if (data.ConnectedLeft)
                        {
                            atom.LeftConnection           = atom.LeftAtom;
                            atom.LeftAtom.RightConnection = atom;
                        }
                        if (data.ConnectedTop)
                        {
                            atom.TopConnection            = atom.TopAtom;
                            atom.TopAtom.BottomConnection = atom;
                        }
                        if (data.ConnectedRight)
                        {
                            atom.RightConnection          = atom.RightAtom;
                            atom.RightAtom.LeftConnection = atom;
                        }
                        if (data.ConnectedBottom)
                        {
                            atom.BottomConnection         = atom.BottomAtom;
                            atom.BottomAtom.TopConnection = atom;
                        }
                    }
                }
            }
        }