コード例 #1
0
        void RemoveNegativeDiagonal(int x, int y, HashSet <GridBall> removeSet)
        {
            var      list  = new List <GridBall>();
            BallData color = null;

            while (IsCell(x, y))
            {
                var ball = _ballsGrid[x, y];
                if (ball == null || ball.data != color)
                {
                    if (list.Count >= 5)
                    {
                        removeSet.UnionWith(list);
                    }

                    list.Clear();

                    if (ball == null)
                    {
                        color = null;
                    }
                    else
                    {
                        color = ball.data;
                        list.Add(ball);
                    }
                }
                else
                {
                    list.Add(ball);
                }

                if (y % 2 != 0)
                {
                    y++;
                }
                else
                {
                    x--;
                    y++;
                }
            }

            if (list.Count >= 5)
            {
                removeSet.UnionWith(list);
            }
        }
コード例 #2
0
        bool RemoveLines()
        {
            var removeSet = new HashSet <GridBall>();

            // horizontal lines
            for (int y = 0; y < grid.rowsCount; y++)
            {
                int      count = 0;
                BallData color = null;
                for (int x = 0; x < grid.colsCount; x++)
                {
                    var ball = _ballsGrid[x, y];
                    if (ball == null || ball.data != color)
                    {
                        if (count >= 5)
                        {
                            for (int i = x - count; i < x; i++)
                            {
                                removeSet.Add(_ballsGrid[i, y]);
                            }
                        }

                        if (ball == null)
                        {
                            color = null;
                            count = 0;
                        }
                        else
                        {
                            color = ball.data;
                            count = 1;
                        }
                    }
                    else
                    {
                        count++;
                    }
                }

                if (count >= 5)
                {
                    for (int i = grid.colsCount - count; i < grid.colsCount; i++)
                    {
                        removeSet.Add(_ballsGrid[i, y]);
                    }
                }
            }

            // positive diagonal
            for (int x = 0; x < grid.colsCount; x++)
            {
                RemovePositiveDiagonal(x, 0, removeSet);
            }
            for (int y = 2; y < grid.rowsCount; y += 2)
            {
                RemovePositiveDiagonal(0, y, removeSet);
            }

            // negative diagonal
            for (int x = 0; x < grid.colsCount; x++)
            {
                RemoveNegativeDiagonal(x, 0, removeSet);
            }
            for (int y = 1; y < grid.rowsCount; y += 2)
            {
                RemoveNegativeDiagonal(grid.colsCount - 1, y, removeSet);
            }

            if (removeSet.Count == 0)
            {
                return(false);
            }

            int N = removeSet.Count;

            _scoreCount += (N * N - 7 * N + 20);
            UpdateScore();

            foreach (var ball in removeSet)
            {
                var pos = ball.position;
                _freeCells.Add(pos);
                _ballsGrid[pos.x, pos.y] = null;
                ball.Hide();
                Destroy(ball.gameObject, 1f);
            }

            return(true);
        }