示例#1
0
        private static string GetScrambleFromLeaves(FinalLeaf edgeLeaf, FinalLeaf cornerLeaf, Random rand, int twistedRight = -1)
        {
            var cube = new SetupCube(rand);

            cube.ApplyCornerLeaf(cornerLeaf, twistedRight);
            cube.ApplyEdgeLeaf(edgeLeaf);
            var cubeStr = cube.ToKociembaString();

            var startInfo = new ProcessStartInfo
            {
                UseShellExecute        = false,
                FileName               = "kociemba.exe",
                Arguments              = cubeStr,
                RedirectStandardOutput = true,
                CreateNoWindow         = true
            };

            try
            {
                var process = new Process {
                    StartInfo = startInfo
                };
                process.Start();
                process.WaitForExit();
                var output = process.StandardOutput.ReadToEnd();
                return(Inverser.Inverse(output));
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        /// Scrambles corners according to the given permutation.
        /// If the permutation has parity, the parity edges will be swapped.
        /// </summary>
        /// <param name="leaf">The permutation of the corners</param>
        /// <param name="twistedRight">The number of corners twisted clockwise in place.  If not specified, twisted corners will be twisted in random directions</param>
        public void ApplyCornerLeaf(FinalLeaf leaf, int twistedRight = -1)
        {
            var buffer = ULBList;
            // All pieces except the buffer
            var corners = new List <CircularLinkedList <char> > {
                URBList, ULFList, URFList, DLBList, DRBList, DLFList, DRFList
            };
            var twists  = leaf.NumTwisted;
            var singles = leaf.Cycles.Skip(1).Count(x => x == 1);

            for (int cycleIdx = 0; cycleIdx < leaf.Cycles.Count; cycleIdx++)
            {
                var cycleLength = leaf.Cycles[cycleIdx];
                var cycleList   = new List <CircularLinkedList <char> > {
                };
                if (cycleIdx == 0)
                {
                    cycleList.Add(buffer);
                }
                while (cycleList.Count < cycleLength)
                {
                    var corner = corners.SelectRandom(x => new Fraction(1, corners.Count), rand);
                    cycleList.Add(corner);
                    corners.Remove(corner);
                }
                var temp = cycleList[0].Head;
                for (int k = 0; k < cycleList.Count - 1; k++)
                {
                    cycleList[k].Head = cycleList[k + 1].Head;
                }
                cycleList[cycleList.Count - 1].Head = temp;

                if (cycleLength == 1 && cycleIdx != 0)   // twisted corner outside the buffer
                {
                    var twistedProb = (double)twists / singles;
                    if (rand.NextDouble() < twistedProb)
                    {
                        if (twistedRight == -1)
                        {
                            if (rand.NextDouble() < 0.5)
                            {
                                cycleList[0].Head = cycleList[0].Head.Right;
                                buffer.Head       = buffer.Head.Left;
                            }
                            else
                            {
                                cycleList[0].Head = cycleList[0].Head.Left;
                                buffer.Head       = buffer.Head.Right;
                            }
                        }
                        else
                        {
                            var twistedRightProb = (double)twistedRight / twists;
                            if (rand.NextDouble() < twistedRightProb)
                            {
                                cycleList[0].Head = cycleList[0].Head.Right;
                                buffer.Head       = buffer.Head.Left;
                                twistedRight--;
                            }
                            else
                            {
                                cycleList[0].Head = cycleList[0].Head.Left;
                                buffer.Head       = buffer.Head.Right;
                            }
                        }
                        singles--;
                        twists--;
                    }
                    else
                    {
                        singles--;
                    }
                }
                else
                {
                    foreach (var piece in cycleList)
                    {
                        var num = rand.Next(3);
                        if (num == 1)
                        {
                            piece.Head  = piece.Head.Left;
                            buffer.Head = buffer.Head.Right;
                        }
                        if (num == 2)
                        {
                            piece.Head  = piece.Head.Right;
                            buffer.Head = buffer.Head.Left;
                        }
                    }
                }
            }

            // swap UB and UL if parity
            if (leaf.Cycles.Count % 2 == 1)
            {
                var temp = UBList.Head;
                UBList.Head = ULList.Head;
                ULList.Head = temp;
            }
        }
        /// <summary>
        /// Scrambles edges according to the given permutation
        /// </summary>
        /// <param name="leaf"></param>
        public void ApplyEdgeLeaf(FinalLeaf leaf)
        {
            var buffer = UBList;
            // All pieces except the buffer
            var edges = new List <CircularLinkedList <char> > {
                ULList, UFList, URList, DLList, DRList, DFList, DBList, BRList, BLList, FRList, FLList
            };
            var twists  = leaf.NumTwisted;
            var singles = leaf.Cycles.Skip(1).Count(x => x == 1);

            for (int cycleIdx = 0; cycleIdx < leaf.Cycles.Count; cycleIdx++)
            {
                var cycleLength = leaf.Cycles[cycleIdx];
                var cycleList   = new List <CircularLinkedList <char> > {
                };
                if (cycleIdx == 0)
                {
                    cycleList.Add(buffer);
                }
                while (cycleList.Count < cycleLength)
                {
                    var edge = edges.SelectRandom(x => new Fraction(1, edges.Count), rand);
                    cycleList.Add(edge);
                    edges.Remove(edge);
                }
                var temp = cycleList[0].Head;
                for (int k = 0; k < cycleList.Count - 1; k++)
                {
                    cycleList[k].Head = cycleList[k + 1].Head;
                }
                cycleList[cycleList.Count - 1].Head = temp;

                if (cycleLength == 1 && cycleIdx != 0)
                {
                    var twistedProb = (double)twists / singles;
                    if (rand.NextDouble() < twistedProb)
                    {
                        cycleList[0].Head = cycleList[0].Head.Right;
                        buffer.Head       = buffer.Head.Left;
                        singles--;
                        twists--;
                    }
                    else
                    {
                        singles--;
                    }
                }
                else
                {
                    foreach (var piece in cycleList)
                    {
                        var num = rand.Next(2);
                        if (num == 1)
                        {
                            piece.Head  = piece.Head.Left;
                            buffer.Head = buffer.Head.Right;
                        }
                    }
                }
            }
        }