diff --git a/src/main/java/hse/java/practice/task1/RubiksCube.java b/src/main/java/hse/java/practice/task1/RubiksCube.java index 2091b657..26b94fd7 100644 --- a/src/main/java/hse/java/practice/task1/RubiksCube.java +++ b/src/main/java/hse/java/practice/task1/RubiksCube.java @@ -26,10 +26,177 @@ public RubiksCube() { } } + private void RotateItself(RotateDirection direction, Integer s) { + if (direction == RotateDirection.CLOCKWISE) { + CubeColor tmp = edges[s].getParts()[0][0]; + edges[s].getParts()[0][0] = edges[s].getParts()[2][0]; + edges[s].getParts()[2][0] = edges[s].getParts()[2][2]; + edges[s].getParts()[2][2] = edges[s].getParts()[0][2]; + edges[s].getParts()[0][2] = tmp; + tmp = edges[s].getParts()[0][1]; + edges[s].getParts()[0][1] = edges[s].getParts()[1][0]; + edges[s].getParts()[1][0] = edges[s].getParts()[2][1]; + edges[s].getParts()[2][1] = edges[s].getParts()[1][2]; + edges[s].getParts()[1][2] = tmp; + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + CubeColor tmp = edges[s].getParts()[0][0]; + edges[s].getParts()[0][0] = edges[s].getParts()[0][2]; + edges[s].getParts()[0][2] = edges[s].getParts()[2][2]; + edges[s].getParts()[2][2] = edges[s].getParts()[2][0]; + edges[s].getParts()[2][0] = tmp; + tmp = edges[s].getParts()[0][1]; + edges[s].getParts()[0][1] = edges[s].getParts()[1][2]; + edges[s].getParts()[1][2] = edges[s].getParts()[2][1]; + edges[s].getParts()[2][1] = edges[s].getParts()[1][0]; + edges[s].getParts()[1][0] = tmp; + } + } + public void front(RotateDirection direction) { + RotateItself(direction,4); + + CubeColor[] up_color = edges[0].getParts()[2].clone(); + + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[0].getParts()[2][i] = edges[2].getParts()[2 - i][2]; + edges[2].getParts()[2 - i][2] = edges[1].getParts()[0][2 - i]; + edges[1].getParts()[0][2 - i] = edges[3].getParts()[i][0]; + edges[3].getParts()[i][0] = up_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[0].getParts()[2][i] = edges[3].getParts()[i][0]; + edges[3].getParts()[i][0] = edges[1].getParts()[0][2-i]; + edges[1].getParts()[0][2-i] = edges[2].getParts()[2-i][2]; + edges[2].getParts()[2-i][2] = up_color[i]; + } + } + } + + public void back(RotateDirection direction) { + RotateItself(direction,5); + + CubeColor[] up_color = edges[0].getParts()[0].clone(); + + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[0].getParts()[0][i] = edges[3].getParts()[i][2]; + edges[3].getParts()[i][2] = edges[1].getParts()[2][2-i]; + edges[1].getParts()[2][2-i] = edges[2].getParts()[2-i][0]; + edges[2].getParts()[2-i][0] = up_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[0].getParts()[0][i] = edges[2].getParts()[2-i][0]; + edges[2].getParts()[2-i][0] = edges[1].getParts()[2][2-i]; + edges[1].getParts()[2][2-i] = edges[3].getParts()[i][2]; + edges[3].getParts()[i][2] = up_color[i]; + } + } + } + + public void up(RotateDirection direction) { + RotateItself(direction,0); + + CubeColor[] front_color = edges[4].getParts()[0].clone(); + + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[0][i] = edges[3].getParts()[0][i]; + edges[3].getParts()[0][i] = edges[5].getParts()[0][i]; + edges[5].getParts()[0][i] = edges[2].getParts()[0][i]; + edges[2].getParts()[0][i] = front_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[0][i] = edges[2].getParts()[0][i]; + edges[2].getParts()[0][i] = edges[5].getParts()[0][i]; + edges[5].getParts()[0][i] = edges[3].getParts()[0][i]; + edges[3].getParts()[0][i] = front_color[i]; + } + } + } + + public void down(RotateDirection direction) { + RotateItself(direction,1); + + CubeColor[] front_color = edges[4].getParts()[2].clone(); + + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[2][i] = edges[2].getParts()[2][i]; + edges[2].getParts()[2][i] = edges[5].getParts()[2][i]; + edges[5].getParts()[2][i] = edges[3].getParts()[2][i]; + edges[3].getParts()[2][i] = front_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[2][i] = edges[3].getParts()[2][i]; + edges[3].getParts()[2][i] = edges[5].getParts()[2][i]; + edges[5].getParts()[2][i] = edges[2].getParts()[2][i]; + edges[2].getParts()[2][i] = front_color[i]; + } + } + } + + public void left(RotateDirection direction) { + RotateItself(direction,2); + + CubeColor[] front_color = new CubeColor[3]; + for (int i = 0; i < 3; i++) { + front_color[i] = edges[4].getParts()[i][0]; + } + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[i][0] = edges[0].getParts()[i][0]; + edges[0].getParts()[i][0] = edges[5].getParts()[2-i][2]; + edges[5].getParts()[2-i][2] = edges[1].getParts()[i][0]; + edges[1].getParts()[i][0] = front_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[i][0] = edges[1].getParts()[i][0]; + edges[1].getParts()[i][0] = edges[5].getParts()[2-i][2]; + edges[5].getParts()[2-i][2] = edges[0].getParts()[i][0]; + edges[0].getParts()[i][0] = front_color[i]; + } + } + } + + public void right(RotateDirection direction) { + RotateItself(direction,3); + + CubeColor[] front_color = new CubeColor[3]; + for (int i = 0; i < 3; i++) { + front_color[i] = edges[4].getParts()[i][2]; + } + + if (direction == RotateDirection.CLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[i][2] = edges[1].getParts()[i][2]; + edges[1].getParts()[i][2] = edges[5].getParts()[2-i][0]; + edges[5].getParts()[2-i][0] = edges[0].getParts()[i][2]; + edges[0].getParts()[i][2] = front_color[i]; + } + } + else if (direction == RotateDirection.COUNTERCLOCKWISE) { + for (int i = 0; i < 3; i++) { + edges[4].getParts()[i][2] = edges[0].getParts()[i][2]; + edges[0].getParts()[i][2] = edges[5].getParts()[2-i][0]; + edges[5].getParts()[2-i][0] = edges[1].getParts()[i][2]; + edges[1].getParts()[i][2] = front_color[i]; + } + } } - + public Edge[] getEdges() { return edges; }