public void Spawn(Arena arena)
        {
            var topLeftY = _rng.Next(arena.Height - _sideLength + 1);
            var topLeftX = _rng.Next(arena.Width - _sideLength + 1);

            var cellsInSquare = CollectCells(topLeftY, topLeftX, arena);

            if (cellsInSquare.Any(c => c is ISnekBody))
            {
                return; // Spawn Failed
            }
            // Found suitable location, calculate bottom right point and collect all points that comprise the square.
            var bottomRight = new Point(topLeftX + _sideLength - 1, topLeftY + _sideLength - 1);
            var points      = new List <Point>();

            for (int y = topLeftY; y < bottomRight.Y; ++y)
            {
                for (int x = topLeftX; x < bottomRight.X; ++x)
                {
                    points.Add(new Point(x, y));
                }
            }

            foreach (var point in points)
            {
                arena.CreateFood(point.X, point.Y);
            }
        }
示例#2
0
        public void Spawn(Arena arena)
        {
            for (int i = 0; i < _spawnAmount; ++i)
            {
                // Generate random points in the arena until we find an unoccupied one.
                var y = _rng.Next(arena.Height);
                var x = _rng.Next(arena.Width);

                var cell = arena.GetCell(x, y);
                if (cell is ISnekBody)
                {
                    continue; // Spawn Failed
                }
                arena.CreateFood(x, y);
            }
        }
示例#3
0
        public void Spawn(Arena arena)
        {
            int spikeLength = _lineLength / 2;      // Lengths of the "spikes" that stick out of the center.

            var centerY = _rng.Next(spikeLength, arena.Height - spikeLength);
            var centerX = _rng.Next(spikeLength, arena.Width - spikeLength);

            var cellsInPlus = CollectCells(centerX, centerY, spikeLength, arena);

            if (cellsInPlus.Any(c => c is ISnekBody))
            {
                return; // Spawn Failed
            }
            // Collect points
            int minX = centerX - spikeLength;
            int maxX = centerX + spikeLength;
            int minY = centerY - spikeLength;
            int maxY = centerY + spikeLength;

            var points = new List <Point>();

            // Collect horizontal line first
            for (int x = minX; x <= maxX; ++x)
            {
                points.Add(new Point(x, centerY));
            }

            // Collect vertical line (skipping the center)
            for (int y = minY; y <= maxY; ++y)
            {
                if (y != centerY)
                {
                    points.Add(new Point(centerX, y));
                }
            }

            foreach (var point in points)
            {
                arena.CreateFood(point.X, point.Y);
            }
        }