Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.Stack;
- public class Cube {
- private final int
- TOP = 0,
- LEFT = 1,
- FRONT = 2,
- RIGHT = 3,
- BACK = 4,
- BOTTOM = 5;
- private int [][] cube;
- public static void main(String[] args) {
- Cube c = new Cube();
- Cube.solve(c);
- }
- static private int [][]scanCube(int[][] cube){
- String[] sides = { "TOP", "LEFT", "FRONT", "RIGHT", "BACK", "BOTTOM" };
- int side_index = 0;
- Scanner in = new Scanner(System.in);
- int[][] input = new int [6][4];
- System.out.println("Welcome to the 2x2x2 cube solver :)\n \n"
- + "Please enter your cube");
- for (String side: sides) {
- System.out.println("Enter the colors for the " + side + " side");
- System.out.println(" 0 = white \n 1 = orange \n 2 = green \n 3 = red\n 4 = blue\n 5 = yellow\n");
- int counter = 0;
- while(counter < 4){
- input[side_index][counter] = Integer.parseInt(in.next());
- counter++;
- }
- side_index++;
- }
- in.close();
- return input;
- }
- static private void solve(Cube c) {
- Set<Cube> cubesFound = new HashSet<Cube>();
- cubesFound.add(c);
- Stack<Cube> s = new Stack<Cube>();
- s.push(c);
- Set<Stack<Cube>> initialPaths = new HashSet<Stack<Cube>>();
- initialPaths.add(s);
- solve(initialPaths, cubesFound);
- }
- static private void solve(Set<Stack<Cube>> livePaths, Set<Cube> cubesFoundSoFar) {
- System.out.println("livePaths size:" + livePaths.size());
- int numDupes = 0;
- Set<Stack<Cube>> newLivePaths = new HashSet<Stack<Cube>>();
- for(Stack<Cube> currentPath : livePaths) {
- Set<Cube> nextStates = currentPath.peek().getNextStates(cubesFoundSoFar);
- for (Cube next : nextStates) {
- if (currentPath.size() > 1 && next.isSolved()) {
- currentPath.push(next);
- System.out.println("Path length:" + currentPath.size());
- //This is the line that prints out the cube below
- System.out.println("Path:" + currentPath);
- System.exit(0);
- } else if (!cubesFoundSoFar.contains(next)) {
- Stack<Cube> newCurrentPath = new Stack<Cube>();
- newCurrentPath.addAll(currentPath);
- newCurrentPath.push(next);
- newLivePaths.add(newCurrentPath);
- cubesFoundSoFar.add(next);
- } else {
- numDupes += 1;
- }
- }
- }
- String storeStates = "positions.txt";
- try {
- PrintWriter outputStream = new PrintWriter(storeStates);
- outputStream.println(cubesFoundSoFar);
- outputStream.println(storeStates);
- outputStream.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Duplicates found " + numDupes + ".");
- solve(newLivePaths, cubesFoundSoFar);
- }
- private Cube() {
- cube = new int[][] {
- { 0, 0, 0, 0 },
- { 1, 1, 1, 1 },
- { 2, 2, 2, 2 },
- { 3, 3, 3, 3 },
- { 4, 4, 4, 4 },
- { 5, 5, 5, 5 }
- };
- cube = scanCube(cube);
- cube = print_cube(cube);
- }
- private Cube(Cube other) {
- cube = new int[other.cube.length][];
- for (int i = 0; i < other.cube.length; i++) {
- cube[i] = Arrays.copyOf(other.cube[i], other.cube[i].length);
- }
- }
- private boolean isSolved() {
- for (int i = 0; i < cube.length; i++) {
- for (int k = 1; k < cube[i].length; k++) {
- if (cube[i][0] != cube[i][k]) {
- return false;
- }
- }
- }
- return true;
- }
- @Override
- public boolean equals(Object other) {
- return other instanceof Cube && Arrays.deepEquals(((Cube) other).cube, cube);
- }
- @Override
- public int hashCode() {
- return Arrays.deepHashCode(cube);
- }
- public Set<Cube> getNextStates(Set<Cube> cubesFoundSoFar) {
- Set<Cube> s = new HashSet<Cube>();
- Cube c = new Cube(this);
- c.rotateFrontCCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateFront2();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateFrontCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateRightCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateRight2();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateRightCCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateUpCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateUp2();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- c = new Cube(this);
- c.rotateUpCCW();
- if (!cubesFoundSoFar.contains(c))
- s.add(c);
- return s;
- }
- /*
- print_sides() is to format the 2d array
- in such a way that it looks like flat cube
- [][]
- [][]
- [][][][][][][][]
- [][][][][][][][]
- [][]
- [][]
- */
- private void print_sides(int cube[][], int first_side, int side_count) {
- char ColorNames [] = {'W','O','G','R','B','Y'};
- for(int line = 0; line < 2; line++) {
- for(int side = first_side; side < side_count + first_side; side++) {
- System.out.print(" ");
- if(side_count == 1 || side_count == 6) {
- System.out.print(" ");
- }
- for(int column = 0; column < 2; column++) {
- int sticker = (2 * line) + column;
- System.out.print(ColorNames[cube[side][sticker]]);
- }
- }
- System.out.println();
- }
- }
- @Override
- public String toString() {
- final String NL = System.lineSeparator();
- String acc = NL;
- acc += " " + cube[TOP][0] + cube[TOP][1] + NL;
- acc += " " + cube[TOP][2] + cube[TOP][3] + NL;
- acc += "" + cube[LEFT][0] + cube[LEFT][1] + " " + cube[FRONT][0] + cube[FRONT][1] + " " + cube[RIGHT][0] + cube[RIGHT][1] + " " + cube[BACK][0] + cube[BACK][1] + NL;
- acc += "" + cube[LEFT][2] + cube[LEFT][3] + " " + cube[FRONT][2] + cube[FRONT][3] + " " + cube[RIGHT][2] + cube[RIGHT][3] + " " + cube[BACK][2] + cube[BACK][3] + NL;
- acc += " " + cube[BOTTOM][0] + cube[BOTTOM][1] + NL;
- acc += " " + cube[BOTTOM][2] + cube[BOTTOM][3] + NL;
- return acc;
- }
- private int[][] print_cube(int [][] cube) {
- print_sides(cube, 0, 1);
- print_sides(cube, 1, 4);
- print_sides(cube, 5, 1);
- return cube;
- }
- private void rotateFrontCCW() {
- rotateFaceCCW(FRONT);
- int temp1 = cube[TOP][2];
- int temp2 = cube[TOP][3];
- cube[TOP][2] = cube[RIGHT][0];
- cube[TOP][3] = cube[RIGHT][2];
- cube[RIGHT][0] = cube[BOTTOM][1];
- cube[RIGHT][2] = cube[BOTTOM][0];
- cube[BOTTOM][0] = cube[LEFT][1];
- cube[BOTTOM][1] = cube[LEFT][3];
- cube[LEFT][1] = temp2;
- cube[LEFT][3] = temp1;
- }
- private void rotateFrontCW() {
- rotateFaceCW(FRONT);
- int temp1 = cube[TOP][2];
- int temp2 = cube[TOP][3];
- cube[TOP][2] = cube[LEFT][3];
- cube[TOP][3] = cube[LEFT][1];
- cube[LEFT][1] = cube[BOTTOM][0];
- cube[LEFT][3] = cube[BOTTOM][1];
- cube[BOTTOM][0] = cube[RIGHT][2];
- cube[BOTTOM][1] = cube[RIGHT][0];
- cube[RIGHT][0] = temp1;
- cube[RIGHT][2] = temp2;
- }
- private void rotateFront2(){
- rotateFace2(FRONT);
- int temp1 = cube[TOP][2];
- int temp2 = cube[TOP][3];
- int temp3 = cube[LEFT][1];
- int temp4 = cube[LEFT][3];
- cube[TOP][2] = cube[BOTTOM][1];
- cube[TOP][3] = cube[BOTTOM][0];
- cube[LEFT][1] = cube[RIGHT][2];
- cube[LEFT][3] = cube[RIGHT][0];
- cube[RIGHT][2] = temp3;
- cube[RIGHT][0] = temp4;
- cube[BOTTOM][1] = temp1;
- cube[BOTTOM][0] = temp2;
- }
- private void rotateRightCCW() {
- rotateFaceCCW(RIGHT);
- int temp1 = cube[TOP][1];
- int temp2 = cube[TOP][3];
- cube[TOP][3] = cube[BACK][0];
- cube[TOP][1] = cube[BACK][2];
- cube[BACK][0] = cube[BOTTOM][3];
- cube[BACK][2] = cube[BOTTOM][1];
- cube[BOTTOM][1] = cube[FRONT][1];
- cube[BOTTOM][3] = cube[FRONT][3];
- cube[FRONT][1] = temp1;
- cube[FRONT][3] = temp2;
- }
- private void rotateRightCW() {
- rotateFaceCW(RIGHT);
- int temp1 = cube[TOP][1];
- int temp2 = cube[TOP][3];
- cube[TOP][1] = cube[FRONT][1];
- cube[TOP][3] = cube[FRONT][3];
- cube[FRONT][1] = cube[BOTTOM][1];
- cube[FRONT][3] = cube[BOTTOM][3];
- cube[BOTTOM][1] = cube[BACK][2];
- cube[BOTTOM][3] = cube[BACK][0];
- cube[BACK][2] = temp1;
- cube[BACK][0] = temp2;
- }
- private void rotateRight2(){
- rotateFace2(RIGHT);
- int temp1 = cube[TOP][1];
- int temp2 = cube[TOP][3];
- int temp3 = cube[FRONT][1];
- int temp4 = cube[FRONT][3];
- cube[TOP][1] = cube[BOTTOM][1];
- cube[TOP][3] = cube[BOTTOM][3];
- cube[FRONT][1] = cube[BACK][2];
- cube[FRONT][3] = cube[BACK][0];
- cube[BACK][2] = temp3;
- cube[BACK][0] = temp4;
- cube[5][3] = temp1;
- cube[5][1] = temp2;
- }
- private void rotateUpCW(){
- rotateFaceCW(TOP);
- int temp1 = cube[FRONT][0];
- int temp2 = cube[FRONT][1];
- cube[FRONT][0] = cube[RIGHT][0];
- cube[FRONT][1] = cube[RIGHT][1];
- cube[RIGHT][0] = cube[BACK][0];
- cube[RIGHT][1] = cube[BACK][1];
- cube[BACK][0] = cube[LEFT][0];
- cube[BACK][1] = cube[LEFT][1];
- cube[LEFT][0] = temp1;
- cube[LEFT][1] = temp2;
- }
- private void rotateUpCCW(){
- rotateFaceCCW(TOP);
- int temp1 = cube[FRONT][0];
- int temp2 = cube[FRONT][1];
- cube[FRONT][0] = cube[LEFT][0];
- cube[FRONT][1] = cube[LEFT][1];
- cube[LEFT][0] = cube[BACK][0];
- cube[LEFT][1] = cube[BACK][1];
- cube[BACK][0] = cube[RIGHT][0];
- cube[BACK][1] = cube[RIGHT][1];
- cube[RIGHT][0] = temp1;
- cube[RIGHT][1] = temp2;
- }
- private void rotateUp2(){
- rotateFace2(TOP);
- int temp1 = cube[FRONT][0];
- int temp2 = cube[FRONT][1];
- int temp3 = cube[LEFT][0];
- int temp4 = cube[LEFT][1];
- cube[FRONT][0] = cube[BACK][0];
- cube[FRONT][1] = cube[BACK][1];
- cube[LEFT][0] = cube[RIGHT][0];
- cube[LEFT][1] = cube[RIGHT][1];
- cube[RIGHT][0] = temp3;
- cube[RIGHT][1] = temp4;
- cube[BACK][0] = temp1;
- cube[BACK][1] = temp2;
- }
- private void rotateFace2(int faceNum) {
- int[] face = cube[faceNum];
- int temp1 = face[1];
- int temp2 = face[3];
- face[1] = face[2];
- face[3] = face[0];
- face[2] = temp2;
- face[0] = temp1;
- }
- private void rotateFaceCW(int faceNum) {
- int[] face = cube[faceNum];
- int temp = face[0];
- face[0] = face[2];
- face[2] = face[3];
- face[3] = face[1];
- face[1] = temp;
- }
- private void rotateFaceCCW(int faceNum) {
- int[] face = cube[faceNum];
- int temp = face[0];
- face[0] = face[1];
- face[1] = face[3];
- face[3] = face[2];
- face[2] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment