예제 #1
0
        public uint SetNumberOfObstacles(uint n)
        {
            List <Cell> availableCells = new List <Cell>(_availableCells);

            ListExt.Shuffle(availableCells);
            Queue <Cell> obstacleQueue = new Queue <Cell>(availableCells);

            Obstacles = new List <Cell>();
            uint obstacles = 0;
            Cell lastCell  = null;

            while (obstacles < n)
            {
                Cell obstacleCell = obstacleQueue.Dequeue();
                obstacleCell.SetCell(true);
                bool validGrid = ValidGrid();
                if (validGrid)
                {
                    ++obstacles;
                    Obstacles.Add(obstacleCell);
                }
                else
                {
                    obstacleCell.SetCell(false);
                    obstacleQueue.Enqueue(obstacleCell);
                    if (lastCell == null)
                    {
                        lastCell = obstacleCell;
                    }
                    else if (lastCell == obstacleCell)
                    {
                        return(obstacles);
                    }
                }
            }
            return(obstacles);
        }
예제 #2
0
 public static IList <T> Shuffle <T>(this IList <T> list)
 {
     ListExt.Shuffle(list);
     return(list);
 }