示例#1
0
        /**
         * Generate a random 2x2 position.
         * @param r         random int generator
         */
        public TwoByTwoState RandomState(Random r)
        {
            TwoByTwoState state = new TwoByTwoState();

            state.Permutation = r.Next(N_PERM);
            state.Orientation = r.Next(N_ORIENT);
            return(state);
        }
示例#2
0
        private string Solve(TwoByTwoState state, int desiredLength, bool exactLength, bool inverse)
        {
            int[] solution      = new int[MAX_LENGTH];
            int[] best_solution = new int[MAX_LENGTH + 1];
            bool  foundSolution = false;
            int   length        = exactLength ? desiredLength : 0;

            while (length <= desiredLength)
            {
                best_solution[length] = 42424242;
                if (Search(state.Permutation, state.Orientation, 0, length, 42, solution, best_solution))
                {
                    foundSolution = true;
                    break;
                }
                length++;
            }

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

            if (length == 0)
            {
                return("");
            }

            StringBuilder scramble = new StringBuilder(MAX_LENGTH * 3);

            if (inverse)
            {
                scramble.Append(inverseMoveToString[best_solution[length - 1]]);
                for (int l = length - 2; l >= 0; l--)
                {
                    scramble.Append(" ").Append(inverseMoveToString[best_solution[l]]);
                }
            }
            else
            {
                scramble.Append(moveToString[best_solution[0]]);
                for (int l = 1; l < length; l++)
                {
                    scramble.Append(" ").Append(moveToString[best_solution[l]]);
                }
            }

            return(scramble.ToString());
        }
示例#3
0
            public TwoByTwoState ToTwoByTwoState()
            {
                var state = new TwoByTwoState();

                var stickersByPiece = GetStickersByPiece(_image);

                // Here's a clever color value assigning system that gives each piece
                // a unique id just by summing up the values of its stickers.
                //
                //            +----------+
                //            |*3*    *2*|
                //            |   U (0)  |
                //            |*1*    *0*|
                // +----------+----------+----------+----------+
                // | 3      1 | 1      0 | 0      2 | 2      3 |
                // |   L (1)  |   F (0)  |   R (0)  |   B (2)  |
                // | 7      5 | 5      4 | 4      6 | 6      7 |
                // +----------+----------+----------+----------+
                //            |*5*    *4*|
                //            |   D (4)  |
                //            |*7*    *6*|
                //            +----------+
                //

                var dColor = stickersByPiece[7][0];
                var bColor = stickersByPiece[7][1];
                var lColor = stickersByPiece[7][2];

                var uColor = (int)((Face)dColor).OppositeFace();
                var fColor = (int)((Face)bColor).OppositeFace();
                var rColor = (int)((Face)lColor).OppositeFace();

                var colorToVal = new int[8];

                colorToVal[uColor] = 0;
                colorToVal[fColor] = 0;
                colorToVal[rColor] = 0;
                colorToVal[lColor] = 1;
                colorToVal[bColor] = 2;
                colorToVal[dColor] = 4;

                var pieces = new int[7];

                for (var i = 0; i < pieces.Length; i++)
                {
                    var stickers = stickersByPiece[i];
                    var pieceVal = colorToVal[stickers[0]] + colorToVal[stickers[1]] + colorToVal[stickers[2]];

                    var clockwiseTurnsToGetToPrimaryColor = 0;
                    while (stickers[clockwiseTurnsToGetToPrimaryColor] != uColor &&
                           stickers[clockwiseTurnsToGetToPrimaryColor] != dColor)
                    {
                        clockwiseTurnsToGetToPrimaryColor++;
                        azzert(clockwiseTurnsToGetToPrimaryColor < 3);
                    }
                    var piece = (clockwiseTurnsToGetToPrimaryColor << 3) + pieceVal;
                    pieces[i] = piece;
                }

                state.Permutation = TwoByTwoSolver.PackPerm(pieces);
                state.Orientation = TwoByTwoSolver.PackOrient(pieces);
                return(state);
            }
示例#4
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 perm      permutation
  * @param orient    random int generator
  * @param length    length of the desired solution
  * @return          a string representing the solution or the scramble of a random position
  */
 public string GenerateExactly(TwoByTwoState state, int length)
 {
     return(Solve(state, length, true, true));
 }
示例#5
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
  * @return          a string representing the solution or the scramble of a random position
  */
 public string SolveIn(TwoByTwoState state, int length)
 {
     return(Solve(state, length, false, false));
 }