コード例 #1
0
        public void Should_shuffle_array()
        {
            var array = new[] {1, 2, 3, 4, 5};
            var sourceArray = new int[array.Length];
            Array.Copy(array, sourceArray, sourceArray.Length);
            var shuffledArray = array.Shuffle();

            Assert.That(shuffledArray, Is.EquivalentTo(sourceArray));
        }
コード例 #2
0
 public void Shuffe()
 {
     var random = new Random(243);
     var sorted = new[] { 1, 2, 3, 4, 5, 6 };
     var expected = new[] { 5, 2, 1, 3, 6, 4 };
     int[] shuffled = null;
     Assert.DoesNotThrow(() =>
         shuffled = sorted.Shuffle(random).ToArray());
     Assert.That(shuffled, Is.Not.Null);
     Assert.That(shuffled.Length, Is.EqualTo(sorted.Length));
     Assert.That(shuffled, Is.EqualTo(expected));
 }
コード例 #3
0
		public void Shuffle_RandomNull ()
		{
			IEnumerable<int> s = new[]{1};
			IEnumerable<int> r = s.Shuffle (null);
			AssertAreSame (s, r);
		}
コード例 #4
0
        public void CompareTo()
        {
            BigInteger hugeInteger = BigInteger.Pow(long.MaxValue, 10);
            BigDecimal hugeNegative = new BigDecimal(-hugeInteger, hugeInteger);
            BigDecimal veryLargeNegative = new BigDecimal(long.MinValue, long.MaxValue);
            BigDecimal largeNegative = new BigDecimal(long.MinValue, int.MaxValue);
            BigDecimal mediumNegative = new BigDecimal(long.MinValue);
            BigDecimal mediumNegativePlusOne = new BigDecimal(long.MinValue + 1);
            BigDecimal zero = BigDecimal.Zero;
            BigDecimal nine = new BigDecimal(9);
            BigDecimal ten = new BigDecimal(10);
            BigDecimal tenPointOne = new BigDecimal(101, -1);
            BigDecimal eleven = new BigDecimal(11);
            BigDecimal nineteen = new BigDecimal(19);
            BigDecimal nineteenPointFive = new BigDecimal(195, -1);
            BigDecimal twenty = new BigDecimal(20);
            BigDecimal twentyFive = new BigDecimal(25);
            BigDecimal thirty = new BigDecimal(30);
            BigDecimal mediumNegativeMinusOne = new BigDecimal(long.MaxValue - 1);
            BigDecimal mediumPositive = new BigDecimal(long.MaxValue);
            BigDecimal largePositive = new BigDecimal(long.MaxValue, int.MaxValue);
            BigDecimal veryLargePositive = new BigDecimal(long.MaxValue, long.MaxValue);
            BigDecimal hugePositive = new BigDecimal(hugeInteger, hugeInteger);

            var expected = new[]
            {
                hugeNegative,
                veryLargeNegative,
                largeNegative,
                mediumNegative,
                mediumNegativePlusOne,
                zero,
                nine,
                ten,
                tenPointOne,
                eleven,
                nineteen,
                nineteenPointFive,
                twenty,
                twentyFive,
                thirty,
                mediumNegativeMinusOne,
                mediumPositive,
                largePositive,
                veryLargePositive,
                hugePositive
            };

            for (int i = 0; i < expected.Length; i++)
            {
                BigDecimal alpha = expected[i];
                Assert.AreEqual(alpha, alpha);
                Assert.AreEqual(0, alpha.CompareTo(alpha));
                for (int j = i + 1; j < expected.Length; j++)
                {
                    BigDecimal bravo = expected[j];
                    Assert.AreEqual(-1, alpha.CompareTo(bravo));
                    Assert.AreEqual(1, bravo.CompareTo(alpha));
                }
            }

            Assert.IsTrue(expected.Shuffle().OrderBy(x => x).SequenceEqual(expected));
        }
コード例 #5
0
        private void button1_Click( object sender, RoutedEventArgs e )
        {
            button1.Content = !_started ? "Stop" : "Start";
            _watch.Reset ();

            if ( !_started ) {
                _ctr.Image = getImage ();

                if ( _ctr.Image == null )
                    return;

                _ctr.CreatePuzzles ();

                _ctr.Shuffle ();

                _watch.Start ();

                _moveCount = 0;
                label2.Content = "Moves: 0";
                _timer.Elapsed += ( a, o ) => {
                    if ( _autoSolving ) {
                        Dispatcher.BeginInvoke ( new Action ( () => {
                            var points = new[]{
                                new Point(0, 0),
                                new Point(1, 0),
                                new Point(2, 0),
                                new Point(0, 1),
                                new Point(1, 1),
                                new Point(2, 1),
                                new Point(0, 2),
                                new Point(1, 2),
                                new Point(2, 2)
                            };

                            points.Shuffle ();

                            for ( int i = 0; i < 9; i++ ) {

                                Point point = points[ i ];
                                PuzzleItem itm = _ctr.GetItem ( point );
                                _ctr.Move ( itm, false );

                            }

                            _ctr.CauseUpdate ();
                        } ) );
                    }

                    label1.Dispatcher.BeginInvoke ( new Action ( () => {
                        label1.Content = string.Format ( "Timer: {0:mm}:{0:ss}.{0:fff} ", _watch.Elapsed );
                    } ), null );
                };

                _timer.Start ();
            }
            else {
                _watch.Stop ();
                _timer.Stop ();

                _ctr.Reset ();
            }

            _started = !_started;
        }
コード例 #6
0
        public static string Variation(this string str, VariationMode mode)
        {
            var result = str;
            if (mode != VariationMode.None)
            {
                var replaceChars = new[] { '*', '.', ':', '!', '_', '-', '#', '%' };
                //char[] replaceChars = new char[] { (char)160, (char)8203, (char)12288, (char)65279, (char)10240 };

                var shuffled = replaceChars.Shuffle();
                string shuffledArray = new string(shuffled.ToArray());

                switch (mode)
                {
                    case VariationMode.AddPlaceHolders:
                        string placeholder = shuffledArray.Substring(0, 5);

                        char[] plac = placeholder.ToArray();
                        Array.Reverse(plac);
                        string reversePlaceHolder = new string(plac);

                        result = placeholder + " " + str + " " + reversePlaceHolder;
                        break;
                    case VariationMode.AlternativeSpaces:
                        char[] resArray = str.ToArray();
                        for (int i = 0; i < resArray.Length; i++)
                        {
                            if (resArray[i] == ' ')
                            {
                                char replacingChar = shuffledArray[i % shuffledArray.Length];
                                resArray[i] = replacingChar;
                            }
                        }
                        result = new string(resArray);
                        break;
                    case VariationMode.TruncateAndAddNumbers:
                        Random rnd = new Random(DateTime.Now.Millisecond);
                        result = str.Substring(0, Math.Min(46, str.Length));
                        result += " " + rnd.Next(1000).ToString();
                        break;
                }
            }
            return result;
        }
コード例 #7
0
ファイル: IEnumerableTest.cs プロジェクト: mono/rocks
		public void Shuffle_RandomNull ()
		{
			IEnumerable<int> s = new[]{1};
			s.Shuffle (null);
		}
コード例 #8
0
ファイル: Program.cs プロジェクト: Sl0vi/algorithms
 static void ShuffleNumbers()
 {
     var random = new Random(243);
     var sorted = new[] { 1, 2, 3, 4, 5, 6 };
     Console.WriteLine("Shuffling numbers:");
     Console.Write("Unshuffled: ");
     for (var i = 0; i < sorted.Length; i++)
     {
         Console.Write(sorted[i]);
         if (i < sorted.Length - 1)
             Console.Write(", ");
     }
     Console.Write(Environment.NewLine);
     Console.Write("Shuffled: ");
     var shuffled = sorted.Shuffle(random).ToArray();
     for (var i = 0; i < shuffled.Length; i++)
     {
         Console.Write(shuffled[i]);
         if (i < shuffled.Length - 1)
             Console.Write(", ");
     }
     Console.Write(Environment.NewLine);
 }