예제 #1
0
파일: Minefield.cs 프로젝트: Nachev/Telerik
        /// <summary>
        /// Initializes a new instance of the <see cref="Minefield"/> class.
        /// </summary>
        /// <param name="rows">Number of rows.</param>
        /// <param name="cols">Number of columns.</param>
        /// <param name="numberOfMines">Number of mines.</param>
        /// <param name="rndGenerator">Random generator provider.</param>
        public Minefield(int rows, int cols, int numberOfMines, IRandomGeneratorProvider rndGenerator)
        {
            // Validations
            this.RowsCount       = rows;
            this.ColumnsCount    = cols;
            this.NumberOfMines   = numberOfMines;
            this.RandomGenerator = rndGenerator;

            // Initializations
            this.OpenedCellsCount = 0;
            this.Cells            = this.GenerateMinefield(rows * cols, numberOfMines);
            this.allNeighborMines = this.CalculateNeighborMines();
        }
예제 #2
0
        /// <summary>
        /// Handles shuffling of an IEnumerable of T with Fisher-Yates shuffle algorithm with given random generator.
        /// </summary>
        /// <typeparam name="T">Generic type.</typeparam>
        /// <param name="source">IEnumerable of T to be shuffled randomly.</param>
        /// <param name="randomGenerator">IRandomGeneratorProvider for testable random numbers.</param>
        /// <returns>Shuffled IEnumerable.</returns>
        public static IEnumerable <T> Shuffle <T>(this IEnumerable <T> source, IRandomGeneratorProvider randomGenerator)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (randomGenerator == null)
            {
                throw new ArgumentNullException("rng");
            }

            return(source.ShuffleIterator(randomGenerator));
        }
예제 #3
0
        /// <summary>
        /// Returns shuffled IEnumerable of T with Fisher-Yates shuffle algorithm with given random generator.
        /// </summary>
        /// <typeparam name="T">Generic type.</typeparam>
        /// <param name="source">IEnumerable to be shuffled.</param>
        /// <param name="randomGenerator">Provider of random generator of type IRandomGeneratorProvider.</param>
        /// <returns>Shuffled IEnumerable.</returns>
        private static IEnumerable <T> ShuffleIterator <T>(this IEnumerable <T> source, IRandomGeneratorProvider randomGenerator)
        {
            T[] elements = source.ToArray();

            for (int i = elements.Length - 1; i > 0; i--)
            {
                int swapIndex = randomGenerator.Next(i + 1);
                yield return(elements[swapIndex]);

                elements[swapIndex] = elements[i];
            }

            yield return(elements[0]);
        }
예제 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MinefieldEasy"/> class.
 /// </summary>
 /// <param name="rows">Number of rows.</param>
 /// <param name="cols">Number of columns.</param>
 /// <param name="numberOfMines">Number of mines.</param>
 /// <param name="rndGenerator">Random generator provider.</param>
 public MinefieldEasy(int rows, int cols, int numberOfMines, IRandomGeneratorProvider rndGenerator) : base(rows, cols, numberOfMines, rndGenerator)
 {
 }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MinefieldEasy"/> class.
 /// </summary>
 /// <param name="rows">Number of rows.</param>
 /// <param name="cols">Number of columns.</param>
 /// <param name="numberOfMines">Number of mines.</param>
 /// <param name="rndGenerator">Random generator provider.</param>
 public MinefieldEasy(int rows, int cols, int numberOfMines, IRandomGeneratorProvider rndGenerator) : base(rows, cols, numberOfMines, rndGenerator)
 {
 }