Пример #1
0
        bool CheckForEnclosed(Point p)
        {
            bool  _ret      = false;
            Point left      = p;
            Point right     = p;
            bool  rightWall = false;
            bool  leftWall  = false;
            bool  falling   = false;

            //if (this.CurrentBoard.Get(p) == '~')
            //return true;

            while (!leftWall && left.X > this.CurrentBoard.GetMinX() && !falling)
            {
                leftWall = (this.CurrentBoard.Get(left) == '#');
                if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1))
                {
                    falling = (this.CurrentBoard.Get(left.Down()) == '.');
                }
                left = left.Left();
            }
            while (!rightWall && right.X < this.CurrentBoard.GetMaxX() && !falling)
            {
                rightWall = (this.CurrentBoard.Get(right) == '#');
                if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1))
                {
                    falling = (this.CurrentBoard.Get(right.Down()) == '.');
                }
                right = right.Right();
            }

            if (rightWall && leftWall)
            {
                _ret = true;
            }

            return(_ret);
        }
Пример #2
0
        void DoWork(ref BackgroundWorker bg)
        {
            // If there are no more cursors, we should be done.
            if (Cursors.Count < 1)
            {
                this.complete = true;
                return;
            }

            List <Point> newCursors = new List <Point>();

            foreach (Point p in Cursors)
            {
                Point down  = p.Down();
                Point up    = p.Up();
                Point left  = p.Left();
                Point right = p.Right();

                bool enclosed = CheckForEnclosed(p);
                bool skip     = false;

                // If the cursor has fallen below MaxY, drop that cursor path
                if (down.Y > this.CurrentBoard.GetMaxY() - 1)
                {
                    this.CurrentBoard.Set(p, '|');
                    skip = true;
                }

                // If there's already water here
                else if (this.CurrentBoard.Get(down) == '|')
                {
                    this.CurrentBoard.Set(p, '|');
                    skip = true;
                }

                // If it's falling through sand unobstructed
                else if (this.CurrentBoard.Get(down) == '.')
                {
                    this.CurrentBoard.Set(p, '|');
                    if (!newCursors.Contains(down) && !Cursors.Contains(down))
                    {
                        newCursors.Add(down);
                    }
                }

                // It hits clay
                else if ((this.CurrentBoard.Get(down) == '#' || this.CurrentBoard.Get(down).IsWet()))
                {
                    this.CurrentBoard.Set(p, '~');

                    while ((this.CurrentBoard.Get(left.Down()) == '#' || this.CurrentBoard.Get(left.Down()).IsWet()) && this.CurrentBoard.Get(left) != '#')
                    {
                        this.CurrentBoard.Set(left, '~');
                        if (this.CurrentBoard.Get(left.Left()) == '.' || this.CurrentBoard.Get(left.Left()) == '|')
                        {
                            left = left.Left();
                        }
                        else
                        {
                            break;
                        }
                    }

                    while ((this.CurrentBoard.Get(right.Down()) == '#' || this.CurrentBoard.Get(right.Down()).IsWet()) && this.CurrentBoard.Get(right) != '#')
                    {
                        this.CurrentBoard.Set(right, '~');
                        if (this.CurrentBoard.Get(right.Right()) == '.' || this.CurrentBoard.Get(right.Right()) == '|')
                        {
                            right = right.Right();
                        }
                        else
                        {
                            break;
                        }
                    }


                    if (this.CurrentBoard.Get(right.Down()) == '.')
                    {
                        Point tmpLeft = right.Left();
                        this.CurrentBoard.Set(p, '|');
                        if (!newCursors.Contains(right))
                        {
                            newCursors.Add(right);
                        }
                        while (this.CurrentBoard.Get(tmpLeft).IsWet())
                        {
                            this.CurrentBoard.Set(tmpLeft, '|');
                            tmpLeft = tmpLeft.Left();
                        }
                    }

                    if (this.CurrentBoard.Get(left.Down()) == '.')
                    {
                        Point tmpRight = left.Right();
                        this.CurrentBoard.Set(p, '|');
                        if (!newCursors.Contains(left))
                        {
                            newCursors.Add(left);
                        }
                        while (this.CurrentBoard.Get(tmpRight).IsWet())
                        {
                            this.CurrentBoard.Set(tmpRight, '|');
                            tmpRight = tmpRight.Right();
                        }
                    }

                    if (enclosed && !skip)
                    {
                        if (!newCursors.Contains(up))
                        {
                            newCursors.Add(up);
                        }
                    }
                }
            }

            Cursors.Clear();
            Cursors = new List <Point>(newCursors);
            if (Cursors.Count() > 0)
            {
                CurrentLine = Cursors.OrderByDescending(o => o.Y).First().Y;
            }

            if (Visualization)
            {
                SetupBoardVisual();
                //Thread.Sleep(1);
            }
        }