Beispiel #1
0
 public AntGrid(int numRows, int numCols)
 {
     _numRows = numRows;
     _numCols = numCols;
     _maxRow = numRows - 1;
     _maxCol = numCols - 1;
     _grid = new int[numRows, numCols];
     //_grid2 = new List<AntGridSquare>();
     _rnd = new Random(DateTime.Now.Millisecond);
     _ant = new Ant();
 }
Beispiel #2
0
        public void MoveAntOnGrid(Ant ant)
        {
            // the ant lives on a grid
            // we move the ant by generating a random number between 1 and 4 inclusive (passed as parameter).
            // Ant may only move to an adjacent cell - no diagonal moves.

            int oldRow = ant.AntRow;
            int oldCol = ant.AntCol;

            int nextCell = 0;

            // find the next cell, working clockwise, 1 = square above, 2 = square to right, etc.
            if (ant.AntRow > 0 && ant.AntCol > 0 && ant.AntRow < _maxRow && ant.AntCol < _maxCol)
            {
                nextCell = _rnd.Next(1, 4 + 1);
            }
            else if (ant.AntRow == 0 && ant.AntCol == 0)
            {
                nextCell = _rnd.Next(2, 3 + 1);
            }
            else if (ant.AntRow == 0 && ant.AntCol > 0 && ant.AntCol < _maxCol)
            {
                nextCell = _rnd.Next(2, 4 + 1);
            }
            else if (ant.AntRow == 0 && ant.AntCol == _maxCol)
            {
                nextCell = _rnd.Next(3, 4 + 1);
            }
            else if (ant.AntRow > 0 && ant.AntCol == _maxCol && ant.AntRow < _maxRow)
            {
                // can be 1, 3, or 4, not contiguous.
                nextCell = _rnd.Next(2, 4 + 1);
                if (nextCell == 2)
                {
                    nextCell = 1;
                }
            }
            else if (ant.AntRow == _maxRow && ant.AntCol == _maxCol)
            {
                // can be 1, or 4, not contiguous.
                nextCell = _rnd.Next(3, 4 + 1);
                if (nextCell == 3)
                {
                    nextCell = 1;
                }
            }
            else if (ant.AntRow == _maxRow && ant.AntCol > 0 && ant.AntCol < _maxCol)
            {
                nextCell = _rnd.Next(1, 3 + 1);
                // can be 1, 2, or 4, not contiguous.
                if (nextCell == 3)
                {
                    nextCell = 4;
                }
            }
            else if (ant.AntRow == _maxRow && ant.AntCol == 0)
            {
                nextCell = _rnd.Next(1, 2 + 1);
            }
            else
            {
                nextCell = _rnd.Next(1, 3 + 1);
            }

            switch (nextCell)
            {
                case 1:
                    ant.AntRow -= 1;
                    break;
                case 2:
                    ant.AntCol += 1;
                    break;
                case 3:
                    ant.AntRow += 1;
                    break;
                case 4:
                    ant.AntCol -= 1;
                    break;
                default:
                    break;
            }

            //Console.WriteLine("Move = {0}, ant now at cell ({1},{2})",nextCell,AntRow, AntCol);
        }