У меня есть класс Face:
public class Face{
private int[][] grid;
protected int[][] transposedArray;
protected Face half;
public Face(int[][] grid){
int[][] copy = new int[3][3];
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[i].length; j++){
copy[i][j] = grid[i][j];
}
}
this.grid = copy;
}
public Face rotateRight(){
int rows = 3;
int cols = 3;
int[][] transposedArray = new int[3][3];
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
transposedArray[i][j]=this.grid[rows-j-1][i];
}
}
return new Face(transposedArray);
}
public Face rotateLeft(){
int rows = 3;
int cols = 3;
int[][] transposedArray = new int[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
transposedArray[2-j][i] = this.grid[i][j];
}
}
return new Face(transposedArray);
}
public Face rotateHalf(){
half = this.rotateRight();
half = half.rotateRight();
return half;
}
public int[][] getGrid(){
int[][] copy = new int[3][3];
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[i].length; j++){
copy[i][j] = this.grid[i][j];
}
}
return copy;
}
public String toString(){
String str = "";
for (int i = 0; i<3;i++){
for (int j = 0; j<3; j++){
str += String.format("%02d",this.grid[i][j]);
}
}
String str1 = str.substring(0,6);
String str2 = str.substring(6,12);
String str3 = str.substring(12,18);
return str1+"\n"+str2+"\n"+str3+"\n";
}
}
Что представляет собой грань 6-ти кубика Рубика.
и класс Рубикс:
public class Rubik{
private int[][][] grid;
protected int[][][] orientated;
public Rubik(int[][][] grid){
this.grid = grid;
}
public Rubik viewUp(){
int rows = 3;
int cols = 3;
int[][][] orientated = new int[6][3][3];
Face topFace = new Face((this.grid)[0]);
Face leftFace = new Face((this.grid)[1]);
Face frontFace = new Face((this.grid)[2]);
Face rightFace = new Face((this.grid)[3]);
Face downFace = new Face((this.grid)[4]);
Face backFace = new Face((this.grid)[5]);
Face newTopFace = backFace;
Face newLeftFace = leftFace.rotateRight();
Face newFrontFace = topFace;
Face newRightFace = rightFace.rotateLeft();
Face newDownFace = frontFace;
Face newBackFace = downFace;
//top
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[0][i][j] = (newTopFace.getGrid())[i][j];
}
}
//left
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[1][i][j] = (newLeftFace.getGrid())[i][j];
}
}
//front
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[2][i][j] = (newFrontFace.getGrid())[i][j];
}
}
//right
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[3][i][j] = (newRightFace.getGrid())[i][j];
}
}
//down
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[4][i][j] = (newDownFace.getGrid())[i][j];
}
}
//back
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[5][i][j] = (newBackFace.getGrid())[i][j];
}
}
return new Rubik(orientated);
}
public Rubik viewDown(){
int rows = 3;
int cols = 3;
int[][][] orientated = new int[6][3][3];
Face topFace = new Face((this.grid)[0]);
Face leftFace = new Face((this.grid)[1]);
Face frontFace = new Face((this.grid)[2]);
Face rightFace = new Face((this.grid)[3]);
Face downFace = new Face((this.grid)[4]);
Face backFace = new Face((this.grid)[5]);
Face newTopFace = frontFace;
Face newLeftFace = leftFace.rotateLeft();
Face newFrontFace = downFace;
Face newRightFace = rightFace.rotateRight();
Face newDownFace = backFace;
Face newBackFace = topFace;
//top
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[0][i][j] = (newTopFace.getGrid())[i][j];
}
}
//left
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[1][i][j] = (newLeftFace.getGrid())[i][j];
}
}
//front
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[2][i][j] = (newFrontFace.getGrid())[i][j];
}
}
//right
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[3][i][j] = (newRightFace.getGrid())[i][j];
}
}
//down
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[4][i][j] = (newDownFace.getGrid())[i][j];
}
}
//back
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[5][i][j] = (newBackFace.getGrid())[i][j];
}
}
return new Rubik(orientated);
}
public Rubik viewLeft(){
int rows = 3;
int cols = 3;
int[][][] orientated = new int[6][3][3];
Face topFace = new Face((this.grid)[0]);
Face leftFace = new Face((this.grid)[1]);
Face frontFace = new Face((this.grid)[2]);
Face rightFace = new Face((this.grid)[3]);
Face downFace = new Face((this.grid)[4]);
Face backFace = new Face((this.grid)[5]);
Face newTopFace = topFace.rotateLeft();
Face newLeftFace = backFace.rotateHalf();
Face newFrontFace = leftFace;
Face newRightFace = frontFace;
Face newDownFace = downFace.rotateRight();
Face newBackFace = rightFace.rotateHalf();
//top
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[0][i][j] = (newTopFace.getGrid())[i][j];
}
}
//left
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[1][i][j] = (newLeftFace.getGrid())[i][j];
}
}
//front
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[2][i][j] = (newFrontFace.getGrid())[i][j];
}
}
//right
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[3][i][j] = (newRightFace.getGrid())[i][j];
}
}
//down
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[4][i][j] = (newDownFace.getGrid())[i][j];
}
}
//back
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[5][i][j] = (newBackFace.getGrid())[i][j];
}
}
return new Rubik(orientated);
}
public Rubik viewRight(){
int rows = 3;
int cols = 3;
int[][][] orientated = new int[6][3][3];
Face topFace = new Face((this.grid)[0]);
Face leftFace = new Face((this.grid)[1]);
Face frontFace = new Face((this.grid)[2]);
Face rightFace = new Face((this.grid)[3]);
Face downFace = new Face((this.grid)[4]);
Face backFace = new Face((this.grid)[5]);
Face newTopFace = topFace.rotateRight();
Face newLeftFace = frontFace;
Face newFrontFace = rightFace;
Face newRightFace = backFace.rotateHalf();
Face newDownFace = downFace.rotateLeft();
Face newBackFace = leftFace.rotateHalf();
//top
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[0][i][j] = (newTopFace.getGrid())[i][j];
}
}
//left
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[1][i][j] = (newLeftFace.getGrid())[i][j];
}
}
//front
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[2][i][j] = (newFrontFace.getGrid())[i][j];
}
}
//right
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[3][i][j] = (newRightFace.getGrid())[i][j];
}
}
//down
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[4][i][j] = (newDownFace.getGrid())[i][j];
}
}
//back
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
orientated[5][i][j] = (newBackFace.getGrid())[i][j];
}
}
return new Rubik(orientated);
}
public String toString(){
String str = "";
for (int k = 0; k<6; k++){
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
str += String.format("%02d",this.grid[k][i][j]);
}
}
}
String str1 = str.substring(0,6);
String str2 = str.substring(6,12);
String str3 = str.substring(12,18);
String str4 = str.substring(18,24);
String str5 = str.substring(24,30);
String str6 = str.substring(30,36);
String str7 = str.substring(36,42);
String str8 = str.substring(42,48);
String str9 = str.substring(48,54);
String str10 = str.substring(54,60);
String str11 = str.substring(60,66);
String str12 = str.substring(66,72);
String str13 = str.substring(72,78);
String str14 = str.substring(78,84);
String str15 = str.substring(84,90);
String str16 = str.substring(90,96);
String str17 = str.substring(96,102);
String str18 = str.substring(102,108);
return "......"+str1+"......"+"\n"+"......"+str2+"......"+"\n"+"......"+str3+"......"
+"\n"+str4+str7+str10+"\n"
+str5+str8+str11+"\n"
+str6+str9+str12+"\n"
+"......"+str13+"......"+"\n"+"......"+str14+"......"+"\n"+"......"+str15+"......"
+"\n"+"......"+str16+"......"+"\n"+"......"+str17+"......"+"\n"+"......"+str18+"......"
+"\n";
}
}
, который в настоящее время представляет 6-сторонний куб, в результате чего методы viewUp, viewDown, viewLeft и viewRight возвращают вам открытое представление кубика Рубика, если смотреть с разных сторон. Вот пример, когда я запускаю rubiks.viewUp () из оригинального Rubiks Cube:
......010203...... ......464748......
......040506...... ......495051......
......070809...... ......525354......
101112192021282930 161310010203303336
131415222324313233 to 171411040506293235
161718252627343536 181512070809283134
......373839...... ......192021......
......404142...... ......222324......
......434445...... ......252627......
......464748...... ......373839......
......495051...... ......404142......
......525354...... ......434445......
Я хочу реализовать другой метод в классе Rubik frontfaceRight (), который направлен на то, чтобы повернуть текущую переднюю грань куба Рубика по часовой стрелке и вернуть новый объект Рубика. Как мне это сделать, особенно без реализации поворотов отдельно для каждой из шести граней?