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
58 changes: 58 additions & 0 deletions src/main/java/hse/java/practice/task1/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,62 @@ public void setParts(CubeColor[][] parts) {
public String toString() {
return Arrays.deepToString(parts);
}
//TODO:
public void rotateFace(RotateDirection direction){

switch (direction){
case CLOCKWISE:
rotateFaceClockwise();
break;
case COUNTERCLOCKWISE:
rotateFaceCounterClockwise();
break;
}

}

private void rotateFaceClockwise(){
CubeColor[][] transpotedColors = new CubeColor[3][3];

for (int rowIndex = 0; rowIndex < 3; rowIndex++) {
for (int colIndex = 0; colIndex < 3; colIndex++) {
transpotedColors[rowIndex][colIndex] = parts[colIndex][rowIndex];
}
}

parts = transpotedColors.clone();
}

private void rotateFaceCounterClockwise(){
for (int i = 0; i < 3; i++) {
rotateFaceClockwise();
}
}

public CubeColor[] getRow(int index) {
return parts[index];
}


public void setRow(int index, CubeColor[] colors){
parts[index] = colors;
}

public CubeColor[] getCol(int index){
CubeColor[] tmp = new CubeColor[3];

for (int rowIndex : new int[]{0, 1, 2}){
tmp[rowIndex] = parts[rowIndex][index];
}

return tmp;
}

public void setCol(int index, CubeColor[] colors){
for (int rowIndex : new int[]{0,1,2}) {
parts[rowIndex][index] = colors[rowIndex];
}
}

}

234 changes: 232 additions & 2 deletions src/main/java/hse/java/practice/task1/RubiksCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Необходимо реализовать интерфейс Cube
* При повороте передней грани, меняются верх низ право и лево
*/
public class RubiksCube {
public class RubiksCube implements Cube{

private static final int EDGES_COUNT = 6;

Expand All @@ -26,13 +26,243 @@ public RubiksCube() {
}
}

public void front(RotateDirection direction) {
@Override
public void up(RotateDirection direction) {
switch (direction){
case CLOCKWISE:
rotateUpClockwise();
break;
case COUNTERCLOCKWISE:
rotateUpCounterClockwise();
break;
}
}

public void rotateUpClockwise(){
edges[0].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] rightToFront = edges[3].getRow(0);

CubeColor[] backToRight = edges[5].getRow(0);
edges[3].setRow(0,backToRight);

CubeColor[] lefToBack = edges[2].getRow(0);
edges[5].setRow(0, lefToBack);

CubeColor[] frontToLeft = edges[4].getRow(0);
edges[2].setRow(0, frontToLeft);

edges[4].setRow(0, rightToFront);

}

public void rotateUpCounterClockwise(){
rotateUpClockwise();
rotateUpClockwise();
rotateUpClockwise();
}

@Override
public void down(RotateDirection direction) {
switch (direction){
case CLOCKWISE:
rotateDownClockwise();
break;
case COUNTERCLOCKWISE:
rotateDownCounterClockwise();
break;
}
}

public void rotateDownClockwise(){
edges[1].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] rightToBack = edges[3].getRow(2);

CubeColor[] frontToRight = edges[4].getRow(2);
edges[3].setRow(2, frontToRight);

CubeColor[] leftToFront = edges[2].getRow(2);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кстати не плохое решение

edges[4].setRow(2, leftToFront);

CubeColor[] backToLeft = edges[5].getRow(2);
edges[2].setRow(2, backToLeft);

edges[5].setRow(2, rightToBack);

}

public void rotateDownCounterClockwise(){
rotateDownClockwise();
rotateDownClockwise();
rotateDownClockwise();
}

@Override
public void left(RotateDirection direction) {
switch (direction){
case CLOCKWISE:
rotateLeftClockwise();
break;
case COUNTERCLOCKWISE:
rotateLeftCounterClockwise();
break;
}
}

public void rotateLeftClockwise(){
edges[2].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] backToUp = edges[5].getCol(2);


CubeColor[] downToBack = edges[1].getCol(0);
downToBack = reverseArr(downToBack);
edges[5].setCol(2, downToBack);

CubeColor[] frontToDown = edges[4].getCol(0);
edges[1].setCol(0, frontToDown);

CubeColor[] upToFront = edges[0].getCol(0);
edges[4].setCol(0, upToFront);

backToUp = reverseArr(backToUp);
edges[0].setCol(0, backToUp);

}

public void rotateLeftCounterClockwise(){
rotateLeftClockwise();
rotateLeftClockwise();
rotateLeftClockwise();
}

@Override
public void right(RotateDirection direction) {
switch (direction){
case CLOCKWISE:
rotateRightClockwise();
break;
case COUNTERCLOCKWISE:
rotateRightCounterClockwise();
break;
}
}

public void rotateRightClockwise(){
edges[3].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] upToBack = edges[0].getCol(2);

CubeColor[] frontToUp = edges[4].getCol(2);
edges[0].setCol(2,frontToUp);

CubeColor[] downToFront = edges[1].getCol(2);
edges[4].setCol(2, downToFront);

CubeColor[] backToDown = edges[5].getCol(0);
backToDown = reverseArr(backToDown);
edges[1].setCol(2, backToDown);

upToBack = reverseArr(upToBack);
edges[5].setCol(0, upToBack);
}

public void rotateRightCounterClockwise(){
rotateRightClockwise();
rotateRightClockwise();
rotateRightClockwise();
}

@Override
public void front(RotateDirection direction) {

switch (direction){
case CLOCKWISE:
rotateFrontClockwise();
break;
case COUNTERCLOCKWISE:
rotateFrontCounterClockwise();
break;
}
}

@Override
public void back(RotateDirection direction) {
switch (direction){
case CLOCKWISE:
rotateBackClockwise();
break;
case COUNTERCLOCKWISE:
rotateBackCounterClockwise();
break;
}
}

public void rotateBackClockwise(){
edges[5].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] leftToDown = edges[2].getCol(0);

CubeColor[] upToLeft = edges[0].getRow(0);
upToLeft = reverseArr(upToLeft);
edges[2].setCol(0, upToLeft);

CubeColor[] rightToUp = edges[3].getCol(2);
rightToUp = reverseArr(rightToUp);
edges[0].setRow(0, rightToUp);

CubeColor[] downToRight = edges[1].getRow(2);
downToRight = reverseArr(downToRight);
edges[3].setCol(2, downToRight);

edges[1].setRow(2, leftToDown);
}

public void rotateBackCounterClockwise(){
rotateBackClockwise();
rotateBackClockwise();
rotateBackClockwise();
}

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

public void rotateFrontClockwise(){
edges[4].rotateFace(RotateDirection.CLOCKWISE);

CubeColor[] upToRight = edges[0].getRow(2);

CubeColor[] leftToUp = edges[2].getCol(2);
leftToUp = reverseArr(leftToUp);
edges[0].setRow(2, leftToUp);

CubeColor[] downToLeft = edges[1].getRow(0);
edges[2].setCol(2,downToLeft);

CubeColor[] rightToDown = edges[3].getCol(0);
rightToDown = reverseArr(rightToDown);
edges[1].setRow(0,rightToDown);

edges[3].setCol(0, upToRight);

}

public void rotateFrontCounterClockwise(){
rotateFrontClockwise();
rotateFrontClockwise();
rotateFrontClockwise();
}

private CubeColor[] reverseArr(CubeColor[] arr){
CubeColor[] tmp = arr.clone();
int len = arr.length;
for (int i = 0; i < len; i++){
tmp[i] = arr[len - 1 - i];
}
return tmp;
}

@Override
public String toString() {
Expand Down