Example #1
0
        public bool isDifferentItemsInFirstFloorLessThanThree(Permutation p_comparedPermutation)
        {
            CubeCubicle l_permutationCubicle;
            int         l_counter = 0;

            for (int i = 0; i < 20; i++)
            {
                if ((l_permutationCubicle = p_comparedPermutation.getCubicleData(i)) != null)
                {
                    if (Rubik.isFirstFloor(l_permutationCubicle.getLocation()))
                    {
                        if (!l_permutationCubicle.getCubiePosition()
                            .equals(getPositionOfCubicleOfCubiclePlace(l_permutationCubicle.getLocation())) ||
                            !l_permutationCubicle.getCurrentCubieOriginalLocation()
                            .equals(getOriginalLocationOfCurrentCubicleInLocation(l_permutationCubicle.getLocation())))
                        {
                            l_counter++;
                        }
                    }
                }
            }
            if (l_counter < 3)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public void applyToRubik(Rubik p_rubik)
 {
     foreach (var l_itr in c_array)
     {
         p_rubik.rotateFace(l_itr as Rotation);
     }
 }
Example #3
0
 public void applyToRubik(Rubik p_rubik)
 {
     if (c_prevSolution != null)
     {
         c_prevSolution.applyToRubik(p_rubik);
     }
     c_rotationLinkedList.applyToRubik(p_rubik);
 }
Example #4
0
        private void findBetterSolution(Solution p_solution, RotationTree p_tree, SolutionManager p_solutionManager,
                                        int p_floor)
        {
            Rubik       l_rubik        = new Rubik();
            Permutation l_permutation  = p_solution.getPermutation().getCopy();
            int         l_minimumValue = l_permutation.getValue(p_floor);

            //	if (l_minimumValue < 8)
            //		l_minimumValue = 8;
            l_rubik.setPermutation(l_permutation);
            searchTree(l_minimumValue - 4, p_tree, l_rubik, p_solutionManager,
                       p_solution, p_floor, 0);
        }
        public static void findGoodRotationLinks(String p_firstFloorFile
                                                 , String p_secondFloorFile, String p_thirdFloorFile, int p_levels)
        {
            RubikFileWriter    l_firstWriter        = new RubikFileWriter(p_firstFloorFile);
            RubikFileWriter    l_secondWriter       = new RubikFileWriter(p_secondFloorFile);
            RubikFileWriter    l_thirdWriter        = new RubikFileWriter(p_thirdFloorFile);
            Rubik              l_rubik              = new Rubik();
            Permutation        l_initialPermutation = l_rubik.getPermutation();
            RotationLinkedList l_rotationLinkedList = new RotationLinkedList();

            BuildFilesForRotation(l_firstWriter, l_secondWriter, l_thirdWriter
                                  , l_rubik, l_initialPermutation, l_rotationLinkedList, p_levels, "");
            l_firstWriter.close();
            l_secondWriter.close();
            l_thirdWriter.close();
        }
        public static void BuildFilesForRotation(RubikFileWriter p_firstFloorFile, RubikFileWriter p_secondFloorFile, RubikFileWriter p_thirdFloorFile
                                                 , Rubik p_rubik
                                                 , Permutation p_initialPermutation, RotationLinkedList p_rotationLinkedList, int p_level, String p_progressString)
        {
            if (p_level == 0)
            {
                return;
            }
            if (p_level > 5)
            {
                Console.WriteLine(p_progressString);
            }
            int i = 0;

            foreach (Face face in Enum.GetValues(typeof(Face)))
            {
                foreach (Direction direction in Enum.GetValues(typeof(Direction)))
                {
                    i++;
                    String   myProgressString = p_progressString + String.Format(".{0}", i);
                    Rotation newRotation      = new Rotation(face, direction);
                    if (p_rotationLinkedList.isRedundant(newRotation))
                    {
                        continue;
                    }
                    p_rotationLinkedList.addRotation(newRotation);
                    p_rubik.rotateFace(newRotation);
                    if (p_rubik.isDifferentItemsInFirstFloorLessThanThree(p_initialPermutation))
                    {
                        p_rotationLinkedList.writeToFile(p_firstFloorFile);
                    }
                    if (p_rubik.isDifferentItemsOnlyInSecondFloorLessThanThree(p_initialPermutation))
                    {
                        p_rotationLinkedList.writeToFile((p_secondFloorFile));
                    }
                    if (p_rubik.changesOnlyInThirdFloor(p_initialPermutation))
                    {
                        p_rotationLinkedList.writeToFile(p_thirdFloorFile);
                    }

                    BuildFilesForRotation(p_firstFloorFile, p_secondFloorFile, p_thirdFloorFile,
                                          p_rubik, p_initialPermutation, p_rotationLinkedList, p_level - 1, myProgressString);
                    p_rotationLinkedList.removeRotation();
                    p_rubik.rotateFace(newRotation.getReverse());
                }
            }
        }
Example #7
0
        public Solution solve(Rubik p_rubik, RotationTree p_firstTree, RotationTree p_secondTree, RotationTree p_thirdTree)
        {
            int             l_numberOfCubicleInPlace;
            Permutation     l_permutation     = p_rubik.getPermutation();
            SolutionManager l_solutionManager = new SolutionManager();
            Solution        l_solutionToDev;

            RotationLinkedList l_rotationLinkedList = new RotationLinkedList();

            int l_floor = getTargetFloor(l_permutation);

            l_numberOfCubicleInPlace = l_permutation.getValue(l_floor);

            l_solutionManager.addSolution(l_rotationLinkedList, l_permutation, null, l_numberOfCubicleInPlace, l_floor);
            while ((l_solutionToDev = l_solutionManager.getBestUndeveloped()) != null &&
                   l_solutionManager.getBestValue() < 40)
            {
                int targetFloor = getTargetFloor(l_solutionToDev.getPermutation());
                Console.Write("Searching {0}", l_solutionToDev.getPermutation().getValue(targetFloor));
                if (l_solutionManager.getBestValue() > l_solutionToDev.getPermutation().getValue(targetFloor) + 14)
                {
                    Console.WriteLine("Couldn't Find a Solution");
                    return(l_solutionManager.getBest());
                }
                if (targetFloor == 1)
                {
                    findBetterSolution(l_solutionToDev, p_firstTree, l_solutionManager, targetFloor);
                }
                if (targetFloor == 2)
                {
                    findBetterSolution(l_solutionToDev, p_secondTree, l_solutionManager, targetFloor);
                }
                if (targetFloor == 3)
                {
                    findBetterSolution(l_solutionToDev, p_thirdTree, l_solutionManager, targetFloor);
                }

                l_floor = getTargetFloor(l_solutionManager.getBestValue());

                //  Console.Write("Floor={0}, Best yet:{1}\n", l_floor, l_solutionManager.getBestValue());
                // l_solutionManager.getBest().print();
            }

            var toReturn = l_solutionManager.getBest();

            return(toReturn);
        }
Example #8
0
        public bool changesOnlyInThirdFloor(Permutation p_comparedPermutation)
        {
            CubeCubicle l_permutationCubicle;
            int         l_counter = 0;

            for (int i = 0; i < 20; i++)
            {
                if ((l_permutationCubicle = p_comparedPermutation.getCubicleData(i)) != null)
                {
                    if (!Rubik.isThirdFloor(l_permutationCubicle.getLocation()))
                    {
                        if (!l_permutationCubicle.getCubiePosition()
                            .equals(getPositionOfCubicleOfCubiclePlace(l_permutationCubicle.getLocation())) ||
                            !l_permutationCubicle.getCurrentCubieOriginalLocation()
                            .equals(getOriginalLocationOfCurrentCubicleInLocation(l_permutationCubicle.getLocation())))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!l_permutationCubicle.getCubiePosition()
                            .equals(getPositionOfCubicleOfCubiclePlace(l_permutationCubicle.getLocation())) ||
                            !l_permutationCubicle.getCurrentCubieOriginalLocation()
                            .equals(getOriginalLocationOfCurrentCubicleInLocation(l_permutationCubicle.getLocation())))
                        {
                            l_counter++;
                        }
                    }
                }
            }


            if (l_counter > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #9
0
        public void searchTree(int p_minimumValue, RotationTree p_tree,
                               Rubik p_rubik, SolutionManager p_solutionManager,
                               Solution p_prevSolution, int p_floor, int depth)
        {
            if (p_minimumValue < 2)
            {
                p_minimumValue = 2;
            }
            Permutation l_permutation = p_rubik.getPermutation().getCopy();
            Rubik       l_rubik       = new Rubik();

            for (int i = 0; i < p_tree.getSize(); i++)
            {
                RotationLinkedList l_rotationLinkedList = p_tree.getRotationLinkedList(i);
                if (l_rotationLinkedList != null)
                {
                    l_rubik.setPermutation(l_permutation);
                    for (int j = 0; j < l_rotationLinkedList.size(); j++)
                    {
                        l_rubik.rotateFace(l_rotationLinkedList.get(j));
                    }
                    Permutation l_resultPermutation = l_rubik.getPermutation().getCopy();

                    if (l_resultPermutation.getValue(p_floor) >= p_minimumValue)
                    {
                        p_solutionManager.addSolution(l_rotationLinkedList, l_resultPermutation, p_prevSolution, l_resultPermutation.getValue(p_floor), p_floor);
                    }
                    if (p_floor == 3 && depth == 0)
                    {
                        //  Console.WriteLine("Hi");
                        searchTree(p_minimumValue, p_tree, l_rubik, p_solutionManager,
                                   new Solution(l_rotationLinkedList, l_resultPermutation, p_prevSolution), p_floor, 1);
                    }
                }
            }
        }
Example #10
0
        public static void Main(String[] args)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            Rubik myRubik = new Rubik();

            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.B, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.L, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.F, Direction.CW));
            myRubik.rotateFace(new Rotation(Face.R, Direction.CW));
            Solver          mySolver        = new Solver();
            RotationTree    firstFloorTree  = new RotationTree();
            RotationTree    secondFloorTree = new RotationTree();
            RotationTree    thirdFloorTree  = new RotationTree();
            RubikFileReader readFirstFloor  = new RubikFileReader("..\\..\\..\\Resources\\FirstFloor.txt");
            RubikFileReader readSecondFloor = new RubikFileReader("..\\..\\..\\Resources\\SecondFloor.txt");
            RubikFileReader readThirdFloor  = new RubikFileReader("..\\..\\..\\Resources\\ThirdFloor.txt");

            RotationTreeLoader.loadRotationTreeFromFile(readFirstFloor, firstFloorTree);
            RotationTreeLoader.loadRotationTreeFromFile(readSecondFloor, secondFloorTree);
            RotationTreeLoader.loadRotationTreeFromFile(readThirdFloor, thirdFloorTree);

            Solution mySolution = mySolver.solve(myRubik, firstFloorTree, secondFloorTree, thirdFloorTree);

            mySolution.applyToRubik(myRubik);
            mySolution.print();
            stopWatch.Stop();
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                 ts.Hours, ts.Minutes, ts.Seconds,
                                                 ts.Milliseconds / 10);

            Console.Write("Elapsed Time={0} seconds", elapsedTime);
            //27-12-2017: started 11:39 PM, Failed
            myRubik.getPermutation().print();
        }