Java 旋转rubiks立方体的面

Java 旋转rubiks立方体的面,java,multidimensional-array,transpose,Java,Multidimensional Array,Transpose,我有一个Face类,它为rubiks立方体的每一面接受3x3整数数组。我正在尝试创建一个rotateRight()方法 010203 070401 040506 becomes 080502 070809 090603 但是,我不确定如何使我的方法rotateRight()返回类型面(这是我运行代码时面临的当前错误)。我能得到一些帮助吗?以下是我目前拥有的代码: public class Face{ priva

我有一个Face类,它为rubiks立方体的每一面接受3x3整数数组。我正在尝试创建一个rotateRight()方法

010203                 070401
040506     becomes     080502
070809                 090603
但是,我不确定如何使我的方法rotateRight()返回类型面(这是我运行代码时面临的当前错误)。我能得到一些帮助吗?以下是我目前拥有的代码:

public class Face{
  private int[][] grid;

  public Face(int[][] grid){
    grid = new int[3][3];
  }


  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[j][i]=grid[i][j];
      }
    }
  }
}
公共类面孔{
私有int[][]网格;
公共面(int[][]网格){
网格=新整数[3][3];
}
公共面旋转右(){
int行=3;
int cols=3;
int[]transposedArray=新int[3][3];

对于(int i=0;i如果我答对了你的问题,你会失败,因为你没有在方法的末尾返回面类型。如果这是正确的,你只需要在
rotateRight()创建一个新的
Face
实例:

public Face rotateRight(){
int行=3;
int cols=3;
int[]transposedArray=新int[3][3];

对于(int i=0;i您必须创建一个新的
Face
实例才能返回,但是您还需要调整循环中的数组分配。否则您将无法分配正确的位置

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[j][2-i]=grid[i][j];
        }
    }
    return new Face(transposedArray);
}
public Face rotateRight(){
int行=3;
int cols=3;
int[]transposedArray=新int[3][3];

对于(int i=0;iIs Rotate方法,应该更改现有面或生成一个新面?错误是什么。您也可以发布错误吗?请注意,这可能是Cubing行话中的
rotateFront
方法。@PM77-1它应该返回一个新的!
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[j][2-i]=grid[i][j];
        }
    }
    return new Face(transposedArray);
}
public class Face{
    private int[][] grid;

    public Face(int[][] grid){
        this.grid = grid;
    }

    enum DIRECTION {
        RIGHT,
        LEFT
    }

    public Face rotateFront(DIRECTION direction){
        int rows = grid.length;
        int[][] transposedArray = new int[rows][rows];

        for (int i = 0; i<rows; i++){
            for (int j = 0; j<rows; j++){
                if (direction == DIRECTION.RIGHT) {
                    transposedArray[j][(rows-1)-i]=grid[i][j];
                } else {
                    transposedArray[(rows-1)-j][i]=grid[i][j];
                }
            }
        }

        return new Face(transposedArray);
    }

    public String toString() {
        int rows = grid.length;
        String output = "";
        for (int i = 0; i<rows; i++){
            for (int j = 0; j<rows; j++){
                output += "["+ grid[i][j]+"] ";
            }
            output += "\n";
        }
        return output;
    }

    public static void main(String[] args) {
        int[][] originalArray = new int[][]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};

        System.out.println("Create our inital Face:");
        Face startFace = new Face(originalArray);
        System.out.println(startFace);

        System.out.println("Rotate our inital Face to the right:");
        Face rotatedFace = startFace.rotateFront(DIRECTION.RIGHT);
        System.out.println(rotatedFace);

        System.out.println("Rotate our rotated Face to the left:");
        Face rotatedFace2 = rotatedFace.rotateFront(DIRECTION.LEFT);
        System.out.println(rotatedFace2);
    }
}