Example #1
0
        /**
         * Generate a random pyraminx position.
         * @param r         random int generator
         */

        public PyraminxSolverState RandomState(Random r)
        {
            var state = new PyraminxSolverState();

            do
            {
                state.EdgePerm = r.Next(NEdgePerm);
            } while (PrunPerm[state.EdgePerm] == -1); // incorrect permutation (bad parity)
            state.EdgeOrient   = r.Next(NEdgeOrient);
            state.CornerOrient = r.Next(NCornerOrient);
            state.Tips         = r.Next(NTips);
            return(state);
        }
Example #2
0
        private static string Solve(PyraminxSolverState state, int desiredLength, bool exactLength, bool inverse,
                                    bool includingTips)
        {
            var r             = new Random();
            var solution      = new int[MaxLength];
            var foundSolution = false;

            // If we count the tips in the desired length, we have to subtract the number of unsolved tips from the length of the main puzzle search.
            if (includingTips)
            {
                desiredLength -= state.UnsolvedTips();
            }
            var length = exactLength ? desiredLength : 0;

            while (length <= desiredLength)
            {
                if (Search(state.EdgePerm, state.EdgeOrient, state.CornerOrient, 0, length, 42, solution, r))
                {
                    foundSolution = true;
                    break;
                }
                length++;
            }

            if (!foundSolution)
            {
                return(null);
            }

            var scramble = new StringBuilder((MaxLength + 4) * 3);

            if (inverse)
            {
                for (var i = length - 1; i >= 0; i--)
                {
                    scramble.Append(" ").Append(InverseMoveToString[solution[i]]);
                }
            }
            else
            {
                for (var i = 0; i < length; i++)
                {
                    scramble.Append(" ").Append(MoveToString[solution[i]]);
                }
            }

            // Scramble the tips
            var arrayTips = new int[4];

            UnpackCornerOrient(state.Tips, arrayTips);
            for (var tip = 0; tip < 4; tip++)
            {
                var dir = arrayTips[tip];
                if (dir > 0)
                {
                    scramble.Append(" ")
                    .Append(inverse ? TipToString[tip * 2 + dir - 1] : InverseTipToString[tip * 2 + dir - 1]);
                }
            }

            return(scramble.ToString().Trim());
        }
Example #3
0
        /**
         * Return a generator of a given position in exactly length number of turns or not at all.
         * Returns either the solution or the generator (inverse solution)
         * @param state         state
         * @param length        length of the desired solution
         * @param includingTips do we want to include tips in the solution lenght ?
         * @return              a string representing the solution or the scramble of a random position
         */

        public string GenerateExactly(PyraminxSolverState state, int length, bool includingTips)
        {
            return(Solve(state, length, true, true, includingTips));
        }
Example #4
0
        /**
         * Solve a given position in less than or equal to length number of turns.
         * Returns either the solution or the generator (inverse solution)
         * @param state         state
         * @param length        length of the desired solution
         * @param includingTips do we want to include tips in the solution lenght ?
         * @return              a string representing the solution or the scramble of a random position
         */

        public string SolveIn(PyraminxSolverState state, int length, bool includingTips)
        {
            return(Solve(state, length, false, false, includingTips));
        }