Ejemplo n.º 1
0
        /// <summary>
        /// Вернуть случайную соседнюю-добычу
        /// </summary>
        /// <param name="c"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool GetPreyNeighbour(ICell c, out Coordinate result)
        {
            bool success = false;

            result = new Coordinate()
            {
                X = -1, Y = -1
            };
            int neighbourAreaSize = 3;

            // (left, top) = верхний левый угол анализируемой области
            int left = c.Position.X - 1;
            int top  = c.Position.Y - 1;

            // Чтобы вернуть случайного соседа, начинаем перебор со случайного смещения (dX, dY)
            int dX = RandomGenerator.GetIntRandom(0, neighbourAreaSize);
            int dY = RandomGenerator.GetIntRandom(0, neighbourAreaSize);

            for (int xStep = 0; xStep < neighbourAreaSize; xStep++)
            {
                for (int yStep = 0; yStep < neighbourAreaSize; yStep++)
                {
                    int testX = left + dX;
                    int testY = top + dY;

                    if (!(dX == 1 && dY == 1 || // сама себе ячейка не "сосед"
                          testX < 0 ||      // дальше - проверки индексов на выход за пределы массива
                          testY < 0 ||
                          testX >= _columns ||
                          testY >= _rows) &&
                        _cells[testX, testY] != null &&
                        c.CanDisplace(_cells[testX, testY])     // переданная ячейка может захватить выбранного соседа
                        )
                    {
                        result  = _cells[testX, testY].Position;
                        success = true;
                        break;
                    }

                    dY = (dY + 1) % neighbourAreaSize;
                }

                if (success)
                {
                    break;
                }
                dX = (dX + 1) % neighbourAreaSize;
            }

            return(success);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Заместить одну ячейку другой. Например, хищник поедает добычу
        /// </summary>
        /// <param name="oldPosition"></param>
        /// <param name="newPosition"></param>
        /// <returns>Операция прошла удачно</returns>
        public bool DislaceAtNewPosition(Coordinate oldPosition, Coordinate newPosition)
        {
            ICell displacer = _cells[oldPosition.X, oldPosition.Y];

            if (!displacer.CanDisplace(_cells[newPosition.X, newPosition.Y]))
            {
                return(false);
            }

            _cells[oldPosition.X, oldPosition.Y] = null;
            displacer.Position = newPosition;
            _cells[newPosition.X, newPosition.Y] = displacer;

            return(true);
        }