public Cube() { meshTransforms = new Matrix[26]; OriginalCubeDraw(); ScramblingVectors = new List<Vector3>(); model = null; cubeConfig = new CubeConfig(); }
public Cube() { meshTransforms = new Matrix[26]; OriginalCubeDraw(); ScramblingVectors = new List <Vector3>(); model = null; cubeConfig = new CubeConfig(); }
public bool AreSame(CubeConfig first, CubeConfig second) { for (int i = 0; i < 6; i++) { string[,] firstState = first.GetCubeState()[i]; string[,] secondState = second.GetCubeState()[i]; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (firstState[x, y] != secondState[x, y]) return false; } } } return true; }
public bool AreSame(CubeConfig first, CubeConfig second) { for (int i = 0; i < 6; i++) { string[,] firstState = first.GetCubeState()[i]; string[,] secondState = second.GetCubeState()[i]; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (firstState[x, y] != secondState[x, y]) { return(false); } } } } return(true); }
public int GetCubeValue(CubeConfig cubeState) { var state = cubeState.GetCubeState(); int value = 0; for (int i = 0; i < state.Count; i++) { string[,] currentFace = state[i]; string[,] targetFace = targetState[i]; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (currentFace[x, y] != targetFace[x, y]) { value++; } } } } return(value); }
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 SelfSolve(Cube _cube) { cube = _cube; CubeState = cube.cubeConfig; targetState = CubeState.GetCubeState(); }
public int GetCubeValue(CubeConfig cubeState) { var state = cubeState.GetCubeState(); int value = 0; for (int i = 0; i < state.Count; i++) { string[,] currentFace = state[i]; string[,] targetFace = targetState[i]; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (currentFace[x, y] != targetFace[x, y]) value++; } } } return value; }
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(); } } }