private static void OptimalAndQuickSolveUI() { while (true) { Cube cube = new Cube(); Console.WriteLine(Cube.ToFriendlyString(Cube.Scramble(12, 12, cube))); Console.WriteLine(cube.ToString()); char key; do { Console.WriteLine("\nPress 'S' for Optimal Solve or 'Q' for Quick Solve."); key = Console.ReadKey().KeyChar; } while ((key != 's') && (key != 'q')); DateTime now = DateTime.Now; Console.WriteLine("\nSolve started at: " + now.ToShortTimeString()); if (key == 's') { Console.WriteLine(Cube.ToFriendlyString(Cube.OptimalSolve(cube))); } else { Cube.Move[] moves = Cube.QuickSolve(cube, false); Console.WriteLine(Cube.ApplyMoves(cube, moves)); Console.WriteLine(Cube.ToFriendlyString(moves)); } double subTotMilSec = (DateTime.Now - now).TotalMilliseconds; int subMin = (int)Math.Floor(subTotMilSec / 60000); subTotMilSec = subTotMilSec - (subMin * 60000); int subSec = (int)Math.Floor(subTotMilSec / 1000); int subMilSec = (int)Math.Floor(subTotMilSec - (subSec * 1000)); Console.WriteLine("Solved in " + subMin + " minute(s), " + subSec + " second(s) and " + subMilSec + " millisecond(s)"); Console.ReadKey(); } }
public static Move[] OptimalSolve(Cube cube, bool onlyBottomFace = false, bool feedBack = true) { Move maxMoveIndex; if (onlyBottomFace) { maxMoveIndex = Enum.GetValues(typeof(Move)).Cast <Move>().Max(); } else { maxMoveIndex = (Move)Enum.GetValues(typeof(MoveOptimalSolve)).Cast <MoveOptimalSolve>().Max(); } //maxMoveIndex = (Move)Enum.GetValues(typeof(MoveOptimalSolve)).Cast<MoveOptimalSolve>().Max(); // TEST List <Move> moves = new List <Move>(); int amountChecked = 0; List <List <Move> > movesChecked = new List <List <Move> >(); //Console.WriteLine("Solved: " + cube.IsSolved()); if ((!onlyBottomFace && cube.IsSolved()) || (onlyBottomFace && cube.Faces[5].IsOneColor() != -1)) { return(moves.ToArray()); } for (int i = 0; i <= 11; i++) { moves.Add((Move)0); int j = 0; do { Cube testCube = new Cube(cube); Cube.ApplyMoves(testCube, moves.ToArray()); if ((!onlyBottomFace && testCube.IsSolved()) || (onlyBottomFace && testCube.Faces[5].IsOneColor() != -1)) { return(moves.ToArray()); } amountChecked++; //Console.WriteLine(testCube); //Console.WriteLine(ToFriendlyString(moves.ToArray()) + " " + testCube.IsSolved()); //Console.Write(ToFriendlyString(moves.ToArray()) + "\t\t"); bool isDecentMove; j = -1; do { isDecentMove = true; j++; if (0 < j) { moves[j - 1] = (Move)0; } moves[j]++; // Console.Write(j.ToString() + i.ToString() + " "); if (j < i) { isDecentMove = IsDecentNextMove(moves[j], moves[j + 1]); } while (j < i && moves[j] < maxMoveIndex && !isDecentMove) { moves[j]++; isDecentMove = IsDecentNextMove(moves[j], moves[j + 1]); } if (!isDecentMove) { //Console.WriteLine("Not decent: " + moves[j]); moves[j]++; } //Console.Write(":" + moves[j] + " " + (j < i ? moves[j + 1].ToString() : "") + " " + isDecentMove + "\n"); } while (j < i && maxMoveIndex < moves[j]); // Console.WriteLine("DAMN"); } while (moves[j] <= maxMoveIndex); moves[j] = (Move)0; if (feedBack) { Console.WriteLine("Amount Checked: " + amountChecked); //Console.ReadKey(); } } return(null); }