public void RunTree() { //declerations string lastMove = ""; List <string[, ]> currentBaseState = new List <string[, ]>(); if (StatesToCheck.Count != 0) { currentBaseState = StatesToCheck[0].Item2.Value.GetCubeState(); } //shuffle move list Shuffle(AllMoves); //start tree foreach (string move in AllMoves) { userState = CubeState.GetCubeState(); bool shouldAddToList = true; CubeConfig tempState = new CubeConfig(); //assign tempState the current base state than rotate it if (isFirstTime) { currentBaseState = userState; tempState.SetStates(currentBaseState); } else if (FirstCheckRun.Count == 0) { currentBaseState = StatesToCheck[0].Item2.Value.GetCubeState(); tempState.SetStates(currentBaseState); } else { currentBaseState = FirstCheckRun[0].Item2.Value.GetCubeState(); tempState.SetStates(currentBaseState); } tempState.Rotate(move); //check for same states foreach (var state in StatesChecked) { if (AreSame(state.Value, tempState)) { shouldAddToList = false; break; } } foreach (var state in StatesToCheck) { if (AreSame(state.Item2.Value, tempState)) { shouldAddToList = false; break; } } //add to StatesToCheck list if (shouldAddToList) { string prvMoves = ""; if (!isFirstTime) { if (FirstCheckRun.Count == 0) { prvMoves = StatesToCheck[0].Item1; } else { prvMoves = FirstCheckRun[0].Item1; } } StatesToCheck.Add(new Tuple <string, KeyValuePair <int, CubeConfig> >(prvMoves + move, new KeyValuePair <int, CubeConfig>(GetCubeValue(tempState), (CubeConfig)tempState.Clone()))); } lastMove = move; } //add to checked list CubeConfig stateChecked = new CubeConfig(); stateChecked.SetStates(currentBaseState); if (lastMove.Contains("I")) { stateChecked.Rotate(lastMove[0].ToString()); } else { stateChecked.Rotate(lastMove + "I"); } StatesChecked.Add(new KeyValuePair <int, CubeConfig>(GetCubeValue(stateChecked), (CubeConfig)stateChecked.Clone())); //remove from need to check list if (!isFirstTime && FirstCheckRun.Count == 0) { StatesToCheck.RemoveAt(0); } //sort by value OrganizeTree(); if (isFirstTime) { foreach (var t in StatesToCheck) { FirstCheckRun.Add(t); } StatesToCheck.Clear(); } else if (FirstCheckRun.Count > 0) { FirstCheckRun.RemoveAt(0); } //run again if needed (added a few if's to prevent index out of range) if (StatesToCheck.Count < 5000) { if (isFirstTime) { isFirstTime = false; RunTree(); } if (StatesToCheck.Count != 0) { if (StatesToCheck[0].Item2.Key != 0) { RunTree(); } } } }
public void Rotate(Vector3 side, bool isClockWise, string algOrder) { Angle -= RotationSpeed; float sidePosition = Main.CubieSize; //Debug.WriteLine("animAngle= " + Angle); //Debug.WriteLine("Angle " + MathHelper.ToRadians(Angle)); float partOfRotation = (float)(100f / RotationSpeed); //Debug.WriteLine(partOfRotation + " = part"); if (side == Vector3.Left) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { //Debug.WriteLine(i); if (isClockWise) { RotateSide(i, MathHelper.PiOver2 / partOfRotation, 1.5f * sidePosition, -1.5f * sidePosition, 0, 'x'); } else { RotateSide(i, -MathHelper.PiOver2 / partOfRotation, 1.5f * sidePosition, -1.5f * sidePosition, 0, 'x'); } } } else if (side == Vector3.Right) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { if (isClockWise) { RotateSide(i, -MathHelper.PiOver2 / 10f, -1.5f * sidePosition, -1.5f * sidePosition, 0, 'x'); } else { RotateSide(i, MathHelper.PiOver2 / 10f, -1.5f * sidePosition, -1.5f * sidePosition, 0, 'x'); } } } else if (side == Vector3.Up) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { if (isClockWise) { RotateSide(i, -MathHelper.PiOver2 / 10f, 0, 0, 0, 'y'); } else { RotateSide(i, MathHelper.PiOver2 / 10f, 0, 0, 0, 'y'); } } } else if (side == Vector3.Down) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { if (isClockWise) { RotateSide(i, -MathHelper.PiOver2 / 10f, 0, 0, 0, 'y'); } else { RotateSide(i, MathHelper.PiOver2 / 10f, 0, 0, 0, 'y'); } } } else if (side == Vector3.Forward) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { if (!isClockWise) { RotateSide(i, -MathHelper.PiOver2 / 10f, 0, -1.5f * sidePosition, -1.5f * sidePosition, 'z'); } else { RotateSide(i, MathHelper.PiOver2 / 10f, 0, -1.5f * sidePosition, 1.5f * sidePosition, 'z'); } } } else if (side == Vector3.Backward) { howManyTurns++; foreach (int i in cubeState.FindCubiesOnSide(side, isClockWise)) { if (!isClockWise) { RotateSide(i, MathHelper.PiOver2 / 10f, 0, -1.5f * sidePosition, 1.5f * sidePosition, 'z'); } else { RotateSide(i, -MathHelper.PiOver2 / 10f, 0, -1.5f * sidePosition, -1.5f * sidePosition, 'z'); } } } if (howManyTurns == (110 - RotationSpeed) / 10) { //cubeConfig.CheckFaceColor(); cubeConfig.Rotate(side, isClockWise); //cubeConfig.CheckFaceColor(); cubeState.Rotate(side, isClockWise); howManyTurns = 0; } }
public void RunTree() { //declerations string lastMove = ""; List<string[,]> currentBaseState = new List<string[,]>(); if (StatesToCheck.Count != 0) currentBaseState = StatesToCheck[0].Item2.Value.GetCubeState(); //shuffle move list Shuffle(AllMoves); //start tree foreach (string move in AllMoves) { userState = CubeState.GetCubeState(); bool shouldAddToList = true; CubeConfig tempState = new CubeConfig(); //assign tempState the current base state than rotate it if (isFirstTime) { currentBaseState = userState; tempState.SetStates(currentBaseState); } else if (FirstCheckRun.Count == 0) { currentBaseState = StatesToCheck[0].Item2.Value.GetCubeState(); tempState.SetStates(currentBaseState); } else { currentBaseState = FirstCheckRun[0].Item2.Value.GetCubeState(); tempState.SetStates(currentBaseState); } tempState.Rotate(move); //check for same states foreach (var state in StatesChecked) { if (AreSame(state.Value, tempState)) { shouldAddToList = false; break; } } foreach (var state in StatesToCheck) { if (AreSame(state.Item2.Value, tempState)) { shouldAddToList = false; break; } } //add to StatesToCheck list if (shouldAddToList) { string prvMoves = ""; if (!isFirstTime) { if (FirstCheckRun.Count == 0) prvMoves = StatesToCheck[0].Item1; else prvMoves = FirstCheckRun[0].Item1; } StatesToCheck.Add(new Tuple<string, KeyValuePair<int, CubeConfig>>(prvMoves + move, new KeyValuePair<int, CubeConfig>(GetCubeValue(tempState), (CubeConfig)tempState.Clone()))); } lastMove = move; } //add to checked list CubeConfig stateChecked = new CubeConfig(); stateChecked.SetStates(currentBaseState); if (lastMove.Contains("I")) stateChecked.Rotate(lastMove[0].ToString()); else stateChecked.Rotate(lastMove + "I"); StatesChecked.Add(new KeyValuePair<int, CubeConfig>(GetCubeValue(stateChecked), (CubeConfig)stateChecked.Clone())); //remove from need to check list if (!isFirstTime && FirstCheckRun.Count == 0) { StatesToCheck.RemoveAt(0); } //sort by value OrganizeTree(); if (isFirstTime) { foreach (var t in StatesToCheck) { FirstCheckRun.Add(t); } StatesToCheck.Clear(); } else if (FirstCheckRun.Count > 0) { FirstCheckRun.RemoveAt(0); } //run again if needed (added a few if's to prevent index out of range) if (StatesToCheck.Count < 5000) { if (isFirstTime) { isFirstTime = false; RunTree(); } if (StatesToCheck.Count != 0) { if (StatesToCheck[0].Item2.Key != 0) RunTree(); } } }