From 1a1ca614b42af27d3d5d92f1cc1ac99963107b7c Mon Sep 17 00:00:00 2001 From: aurhwwh Date: Thu, 5 Feb 2026 01:22:02 +0300 Subject: [PATCH 1/2] first try --- .../hse/java/practice/task1/RubiksCube.java | 166 +++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/src/main/java/hse/java/practice/task1/RubiksCube.java b/src/main/java/hse/java/practice/task1/RubiksCube.java index 2091b657..8f068b23 100644 --- a/src/main/java/hse/java/practice/task1/RubiksCube.java +++ b/src/main/java/hse/java/practice/task1/RubiksCube.java @@ -26,10 +26,174 @@ public RubiksCube() { } } + private void rotateSideClockWise(int side){ + CubeColor[][] edgeClone = edges[side].getParts().clone(); + edges[side].getParts()[0][0]=edgeClone[2][0]; + edges[side].getParts()[0][1]=edgeClone[1][0]; + edges[side].getParts()[0][2]=edgeClone[0][0]; + edges[side].getParts()[1][0]=edgeClone[2][1]; + edges[side].getParts()[1][2]=edgeClone[0][1]; + edges[side].getParts()[2][0]=edgeClone[2][2]; + edges[side].getParts()[2][1]=edgeClone[1][2]; + edges[side].getParts()[2][2]=edgeClone[0][2]; + } + + private void rotateSideCounterClockWise(int side){ + rotateSideClockWise(side); + rotateSideClockWise(side); + rotateSideClockWise(side); + } + public void front(RotateDirection direction) { + CubeColor[][] edge2Clone = edges[2].getParts().clone(); + CubeColor[][] edge0Clone = edges[0].getParts().clone(); + CubeColor[][] edge3Clone = edges[3].getParts().clone(); + CubeColor[][] edge1Clone = edges[1].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(4); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[2][i] = edge2Clone[2 - i][2]; + edges[2].getParts()[2 - i][2] = edge1Clone[0][2 - i]; + edges[1].getParts()[0][2 - i] = edge3Clone[i][0]; + edges[3].getParts()[i][0] = edge0Clone[2][i]; + } + } + else{ + rotateSideCounterClockWise(4); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[2][i] = edge3Clone[i][0]; + edges[2].getParts()[2 - i][2] = edge0Clone[2][i]; + edges[1].getParts()[0][2 - i] = edge2Clone[2-i][2]; + edges[3].getParts()[i][0] = edge1Clone[0][2-i]; + } + } + } + + void up(RotateDirection direction){ + CubeColor[][] edge2Clone = edges[2].getParts().clone(); + CubeColor[][] edge4Clone = edges[4].getParts().clone(); + CubeColor[][] edge3Clone = edges[3].getParts().clone(); + CubeColor[][] edge5Clone = edges[5].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(0); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[0][i] = edge2Clone[0][i]; + edges[2].getParts()[0][i] = edge4Clone[0][i]; + edges[3].getParts()[0][i] = edge5Clone[0][i]; + edges[4].getParts()[0][i] = edge3Clone[0][i]; + } + } + else{ + rotateSideCounterClockWise(0); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[0][i] = edge3Clone[0][i]; + edges[2].getParts()[0][i] = edge5Clone[0][i]; + edges[3].getParts()[0][i] = edge4Clone[0][i]; + edges[4].getParts()[0][i] = edge2Clone[0][i]; + } + } + } + + void down(RotateDirection direction){ + CubeColor[][] edge2Clone = edges[2].getParts().clone(); + CubeColor[][] edge4Clone = edges[4].getParts().clone(); + CubeColor[][] edge3Clone = edges[3].getParts().clone(); + CubeColor[][] edge5Clone = edges[5].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(1); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[2][i] = edge3Clone[2][i]; + edges[2].getParts()[2][i] = edge5Clone[2][i]; + edges[3].getParts()[2][i] = edge4Clone[2][i]; + edges[4].getParts()[2][i] = edge2Clone[2][i]; + } + } + else{ + rotateSideCounterClockWise(1); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[2][i] = edge2Clone[2][i]; + edges[2].getParts()[2][i] = edge4Clone[2][i]; + edges[3].getParts()[2][i] = edge5Clone[2][i]; + edges[4].getParts()[2][i] = edge3Clone[2][i]; + } + } + } + void left(RotateDirection direction){ + CubeColor[][] edge0Clone = edges[0].getParts().clone(); + CubeColor[][] edge4Clone = edges[4].getParts().clone(); + CubeColor[][] edge1Clone = edges[1].getParts().clone(); + CubeColor[][] edge5Clone = edges[5].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(2); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[i][0] = edge5Clone[2-i][2]; + edges[4].getParts()[i][0] = edge0Clone[i][0]; + edges[1].getParts()[i][0] = edge4Clone[i][0]; + edges[5].getParts()[2-i][2] = edge1Clone[i][0]; + } + } + else{ + rotateSideCounterClockWise(2); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[2-i][2] = edge0Clone[i][0]; + edges[0].getParts()[i][0] = edge4Clone[i][0]; + edges[4].getParts()[i][0] = edge1Clone[i][0]; + edges[1].getParts()[2-i][0] = edge5Clone[i][2]; + } + } } - + + void right(RotateDirection direction){ + CubeColor[][] edge0Clone = edges[0].getParts().clone(); + CubeColor[][] edge4Clone = edges[4].getParts().clone(); + CubeColor[][] edge1Clone = edges[1].getParts().clone(); + CubeColor[][] edge5Clone = edges[5].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(3); + for (int i = 0; i < 3; i++) { + edges[5].getParts()[2-i][0] = edge0Clone[i][2]; + edges[0].getParts()[i][2] = edge4Clone[i][2]; + edges[4].getParts()[i][2] = edge1Clone[i][2]; + edges[1].getParts()[2-i][2] = edge5Clone[i][0]; + } + } + else{ + rotateSideCounterClockWise(3); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[i][2] = edge5Clone[2-i][0]; + edges[4].getParts()[i][2] = edge0Clone[i][2]; + edges[1].getParts()[i][2] = edge4Clone[i][2]; + edges[5].getParts()[2-i][0] = edge1Clone[i][2]; + } + } + } + + void back(RotateDirection direction){ + CubeColor[][] edge2Clone = edges[2].getParts().clone(); + CubeColor[][] edge0Clone = edges[0].getParts().clone(); + CubeColor[][] edge3Clone = edges[3].getParts().clone(); + CubeColor[][] edge1Clone = edges[1].getParts().clone(); + if (direction==RotateDirection.CLOCKWISE){ + rotateSideClockWise(5); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[0][i] = edge3Clone[i][2]; + edges[2].getParts()[i][0] = edge0Clone[0][2-i]; + edges[1].getParts()[2][i] = edge2Clone[i][0]; + edges[3].getParts()[2-i][2] = edge1Clone[2][i]; + } + } + else{ + rotateSideCounterClockWise(5); + for (int i = 0; i < 3; i++) { + edges[0].getParts()[0][i] = edge2Clone[2-i][0]; + edges[2].getParts()[i][0] = edge1Clone[2][i]; + edges[1].getParts()[2][i] = edge3Clone[2-i][2]; + edges[3].getParts()[i][2] = edge0Clone[0][i]; + } + } + } + public Edge[] getEdges() { return edges; } From 65a33eb0fce01bbdac3b1cf0adb865cbf7870bb3 Mon Sep 17 00:00:00 2001 From: aurhwwh Date: Thu, 5 Feb 2026 01:58:51 +0300 Subject: [PATCH 2/2] fix clone --- .../hse/java/practice/task1/RubiksCube.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/hse/java/practice/task1/RubiksCube.java b/src/main/java/hse/java/practice/task1/RubiksCube.java index 8f068b23..0448f2c4 100644 --- a/src/main/java/hse/java/practice/task1/RubiksCube.java +++ b/src/main/java/hse/java/practice/task1/RubiksCube.java @@ -26,8 +26,16 @@ public RubiksCube() { } } + private CubeColor[][] clone2d(CubeColor[][] original){ + CubeColor[][] copy = new CubeColor[3][3]; + for (int i = 0; i < 3; i++) { + System.arraycopy(original[i], 0, copy[i], 0, 3); + } + return copy; + } + private void rotateSideClockWise(int side){ - CubeColor[][] edgeClone = edges[side].getParts().clone(); + CubeColor[][] edgeClone = clone2d(edges[side].getParts()); edges[side].getParts()[0][0]=edgeClone[2][0]; edges[side].getParts()[0][1]=edgeClone[1][0]; edges[side].getParts()[0][2]=edgeClone[0][0]; @@ -45,10 +53,10 @@ private void rotateSideCounterClockWise(int side){ } public void front(RotateDirection direction) { - CubeColor[][] edge2Clone = edges[2].getParts().clone(); - CubeColor[][] edge0Clone = edges[0].getParts().clone(); - CubeColor[][] edge3Clone = edges[3].getParts().clone(); - CubeColor[][] edge1Clone = edges[1].getParts().clone(); + CubeColor[][] edge2Clone = clone2d(edges[2].getParts()); + CubeColor[][] edge0Clone = clone2d(edges[0].getParts()); + CubeColor[][] edge3Clone = clone2d(edges[3].getParts()); + CubeColor[][] edge1Clone = clone2d(edges[1].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(4); for (int i = 0; i < 3; i++) { @@ -70,10 +78,10 @@ public void front(RotateDirection direction) { } void up(RotateDirection direction){ - CubeColor[][] edge2Clone = edges[2].getParts().clone(); - CubeColor[][] edge4Clone = edges[4].getParts().clone(); - CubeColor[][] edge3Clone = edges[3].getParts().clone(); - CubeColor[][] edge5Clone = edges[5].getParts().clone(); + CubeColor[][] edge2Clone = clone2d(edges[2].getParts()); + CubeColor[][] edge4Clone = clone2d(edges[4].getParts()); + CubeColor[][] edge3Clone = clone2d(edges[3].getParts()); + CubeColor[][] edge5Clone = clone2d(edges[5].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(0); for (int i = 0; i < 3; i++) { @@ -95,10 +103,10 @@ void up(RotateDirection direction){ } void down(RotateDirection direction){ - CubeColor[][] edge2Clone = edges[2].getParts().clone(); - CubeColor[][] edge4Clone = edges[4].getParts().clone(); - CubeColor[][] edge3Clone = edges[3].getParts().clone(); - CubeColor[][] edge5Clone = edges[5].getParts().clone(); + CubeColor[][] edge2Clone = clone2d(edges[2].getParts()); + CubeColor[][] edge4Clone = clone2d(edges[4].getParts()); + CubeColor[][] edge3Clone = clone2d(edges[3].getParts()); + CubeColor[][] edge5Clone = clone2d(edges[5].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(1); for (int i = 0; i < 3; i++) { @@ -120,10 +128,10 @@ void down(RotateDirection direction){ } void left(RotateDirection direction){ - CubeColor[][] edge0Clone = edges[0].getParts().clone(); - CubeColor[][] edge4Clone = edges[4].getParts().clone(); - CubeColor[][] edge1Clone = edges[1].getParts().clone(); - CubeColor[][] edge5Clone = edges[5].getParts().clone(); + CubeColor[][] edge0Clone = clone2d(edges[0].getParts()); + CubeColor[][] edge4Clone = clone2d(edges[4].getParts()); + CubeColor[][] edge1Clone = clone2d(edges[1].getParts()); + CubeColor[][] edge5Clone = clone2d(edges[5].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(2); for (int i = 0; i < 3; i++) { @@ -145,10 +153,10 @@ void left(RotateDirection direction){ } void right(RotateDirection direction){ - CubeColor[][] edge0Clone = edges[0].getParts().clone(); - CubeColor[][] edge4Clone = edges[4].getParts().clone(); - CubeColor[][] edge1Clone = edges[1].getParts().clone(); - CubeColor[][] edge5Clone = edges[5].getParts().clone(); + CubeColor[][] edge0Clone = clone2d(edges[0].getParts()); + CubeColor[][] edge4Clone = clone2d(edges[4].getParts()); + CubeColor[][] edge1Clone = clone2d(edges[1].getParts()); + CubeColor[][] edge5Clone = clone2d(edges[5].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(3); for (int i = 0; i < 3; i++) { @@ -170,10 +178,10 @@ void right(RotateDirection direction){ } void back(RotateDirection direction){ - CubeColor[][] edge2Clone = edges[2].getParts().clone(); - CubeColor[][] edge0Clone = edges[0].getParts().clone(); - CubeColor[][] edge3Clone = edges[3].getParts().clone(); - CubeColor[][] edge1Clone = edges[1].getParts().clone(); + CubeColor[][] edge2Clone = clone2d(edges[2].getParts()); + CubeColor[][] edge0Clone = clone2d(edges[0].getParts()); + CubeColor[][] edge3Clone = clone2d(edges[3].getParts()); + CubeColor[][] edge1Clone = clone2d(edges[1].getParts()); if (direction==RotateDirection.CLOCKWISE){ rotateSideClockWise(5); for (int i = 0; i < 3; i++) {