Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/hse/java/practice/task1/Edge.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hse.java.practice.task1;

import java.util.Arrays;
import java.util.Collections;

public class Edge {

Expand All @@ -27,6 +28,35 @@ public CubeColor[][] getParts() {
return parts;
}

private void transpose() {
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
CubeColor temp = parts[i][j];
parts[i][j] = parts[j][i];
parts[j][i] = temp;
}
}
}

private void reverse() {
for (int i = 0; i < 3; i++) {
Collections.reverse(Arrays.asList(parts[i]));
}
}

public void rotate(RotateDirection direction) {
switch (direction) {
case CLOCKWISE -> {
transpose();
reverse();
}
case COUNTERCLOCKWISE -> {
reverse();
transpose();
}
}
}

public void setParts(CubeColor[][] parts) {
this.parts = parts;
}
Expand Down
180 changes: 178 additions & 2 deletions src/main/java/hse/java/practice/task1/RubiksCube.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hse.java.practice.task1;


import java.util.Arrays;

/**
* Необходимо реализовать интерфейс Cube
* При повороте передней грани, меняются верх низ право и лево
*/
public class RubiksCube {
public class RubiksCube implements Cube {

private static final int EDGES_COUNT = 6;

Expand All @@ -25,11 +26,186 @@ public RubiksCube() {
edges[i] = new Edge(colors[i]);
}
}
private void rotateUpRing(RotateDirection direction) {
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 3;
} else {
n = 1;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp5 = Arrays.copyOf(edges[5].getParts()[0], 3);
edges[5].getParts()[0] = Arrays.copyOf(edges[3].getParts()[0], 3);
edges[3].getParts()[0] = Arrays.copyOf(edges[4].getParts()[0], 3);
edges[4].getParts()[0] = Arrays.copyOf(edges[2].getParts()[0], 3);
edges[2].getParts()[0] = temp5;
}
}

private void rotateDownRing(RotateDirection direction) {
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 1;
} else {
n = 3;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp5 = Arrays.copyOf(edges[5].getParts()[2], 3);
edges[5].getParts()[2] = Arrays.copyOf(edges[3].getParts()[2], 3);
edges[3].getParts()[2] = Arrays.copyOf(edges[4].getParts()[2], 3);
edges[4].getParts()[2] = Arrays.copyOf(edges[2].getParts()[2], 3);
edges[2].getParts()[2] = temp5;
}
}

private void rotateRightRing(RotateDirection direction) {
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 1;
} else {
n = 3;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp5 = new CubeColor[3];
for (int i = 0; i < 3; i++) {
temp5[i] = edges[5].getParts()[i][0];
}

for (int i = 0; i < 3; i++) {
edges[5].getParts()[i][0] = edges[0].getParts()[i][2];
}
for (int i = 0; i < 3; i++) {
edges[0].getParts()[i][2] = edges[4].getParts()[2 - i][2];
}
for (int i = 0; i < 3; i++) {
edges[4].getParts()[i][2] = edges[1].getParts()[i][2];
}
for (int i = 0; i < 3; i++) {
edges[1].getParts()[i][2] = temp5[2 - i];
}
}
}

private void rotateLeftRing(RotateDirection direction) {
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 1;
} else {
n = 3;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp4 = new CubeColor[3];
for (int i = 0; i < 3; i++) {
temp4[i] = edges[4].getParts()[i][0];
}

for (int i = 0; i < 3; i++) {
edges[4].getParts()[i][0] = edges[0].getParts()[i][0];
}
for (int i = 0; i < 3; i++) {
edges[0].getParts()[i][0] = edges[5].getParts()[2 - i][2];
}
for (int i = 0; i < 3; i++) {
edges[5].getParts()[i][2] = edges[1].getParts()[i][0];
}
for (int i = 0; i < 3; i++) {
edges[1].getParts()[i][0] = temp4[2 - i];
}
}
}

private void rotateBackRing(RotateDirection direction) {
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 1;
} else {
n = 3;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp2 = new CubeColor[3];
for (int i = 0; i < 3; i++) {
temp2[i] = edges[3].getParts()[i][2];
}

for (int i = 0; i < 3; i++) {
edges[3].getParts()[i][2] = edges[1].getParts()[2][2 - i];
}
for (int i = 0; i < 3; i++) {
edges[1].getParts()[2][i] = edges[2].getParts()[i][0];
}
for (int i = 0; i < 3; i++) {
edges[2].getParts()[i][0] = edges[0].getParts()[0][2 - i];
}
for (int i = 0; i < 3; i++) {
edges[0].getParts()[0][i] = temp2[i];
}
}
}

private void rotateFrontRing(RotateDirection direction){
int n;
if (direction == RotateDirection.CLOCKWISE) {
n = 1;
} else {
n = 3;
}
for (int j = 0; j < n; j++) {
CubeColor[] temp3 = new CubeColor[3];
for (int i = 0; i < 3; i++) {
temp3[i] = edges[3].getParts()[i][0];
}

for (int i = 0; i < 3; i++) {
edges[3].getParts()[i][0] = edges[0].getParts()[2][i];
}
for (int i = 0; i < 3; i++) {
edges[0].getParts()[2][i] = edges[2].getParts()[i][2];
}
for (int i = 0; i < 3; i++) {
edges[2].getParts()[i][2] = edges[1].getParts()[0][2 - i];
}
for (int i = 0; i < 3; i++) {
edges[1].getParts()[0][i] = temp3[2 - i];
}
}
}

@Override
public void up(RotateDirection direction) {
edges[0].rotate(direction);
rotateUpRing(direction);
}

@Override
public void down(RotateDirection direction) {
edges[1].rotate(direction);
rotateDownRing(direction);

}

@Override
public void left(RotateDirection direction) {
edges[2].rotate(direction);
rotateLeftRing(direction);
}

@Override
public void right(RotateDirection direction) {
edges[3].rotate(direction);
rotateRightRing(direction);
}

public void front(RotateDirection direction) {
edges[4].rotate(direction);
rotateFrontRing(direction);
}

@Override
public void back(RotateDirection direction) {
edges[5].rotate(direction);
rotateBackRing(direction);
}

public Edge[] getEdges() {
return edges;
}
Expand Down