我的方法rotationCW()每次调用Shape时,应将其顺时针旋转90度。如果执行以下操作:

someObject.rotateCW();
someObject.rotateCW();
someObject.rotateCW();


然后,形状将从270开始顺时针旋转到0度。

但是,无论我叫多少次,我的rotateCW()代码都只会旋转到90度。例如:

x . a
. . b
x . c


在两次调用rotateCW()(180)之后,该表格应变为:

someObject.rotateCW();
someObject.rotateCW();

c . x
b . .
x . a


但是我得到的只是:

x . x
. . .
c b a


无论我调用rotateCW多少次,我的代码只会旋转一次!

import java.util.Scanner;
public class Tester {

    public static void main(String[] args)
    {

        //FitIt temp = new FitIt();
        //CreateShape result = (CreateShape) temp.makeShape(". . .\n"+"n . n", 'a');

    CreateShape temp = new CreateShape(6,6, 'a', new char[][]{{'x','.','a'},
                                                                  {'.','.','b'},
                                                                  {'x','.','c'}}, "x . a\n"
                                                                                + ". . b\n"
                                                                                + "x . c");
        temp.rotateCW();
        temp.rotateCW();
        temp.rotateCW();
        System.out.println(temp);
}
import java.util.*;

public class CreateShape implements Shape {

//  private String newLayout  = "";
    private String layout;
    private int height;
    private int width;
    private char dc;
    private Rotation initialPos;
    private char[][] shape;
    private char[][] swapped;


    public CreateShape(int height, int width, char dc, char[][] charLayout, String layout)
    {
        this.height = height;
        this.width = width;
        this.dc = dc;
        this.shape = charLayout;
        this.layout = layout;
        initialPos = Rotation.CW0;
    }

    public void rotateCW()
    {
        layout = "";
        initialPos = initialPos.next();
        int w = shape.length;
        int h = shape[0].length;

        swapped = new char[h][w];
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                swapped[i][j] = shape[w-j-1][i];
                layout += swapped[i][j];
            }
            layout += "\n";
        }
        height = swapped.length;
        width = swapped[0].length;
        //newLayout = layout;
    }
public String toString()
    {
        return "SHAPE " + this.dc +"\n" +
                "height: " + this.height+";" + " width: " + this.width+"; " + getRotation().toString() + "\n" +
                 this.layout;
    }


枚举旋转

public enum Rotation {

    CW0, CW90, CW180, CW270;

    // Calling rot.next() will return the next enumeration element
    // representing the next 90 degree clock-wise rotation after rot.
    public Rotation next()
    {
        if(this == CW0)
            return CW90;
        else if(this == CW90)
            return CW180;
        else if(this == CW180)
            return CW270;
        else if(this == CW270)
            return CW0;

        return null;
    }

    //toString representation of rotation
    @Override
    public String toString()
    {
        if(this == CW0)
            return "rotation: CW0";
        else if(this == CW90)
            return "rotation: CW90";
        else if(this == CW180)
            return "rotation: CW180";
        else if(this == CW270)
            return "rotation: CW270";

        return "Something is wrong! Check ROTATION again!";
    }
}

最佳答案

执行交换后,您不会更新形状。添加shape = swapped使旋转持续存在,而不是在原始形状上再次调用rotate:

  public void rotateCW() {
    layout = "";
    initialPos = initialPos.next();
    int w = shape.length;
    int h = shape[0].length;

    swapped = new char[h][w];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        swapped[i][j] = shape[w - j - 1][i];
        layout += swapped[i][j];
      }
      layout += "\n";
    }
    height = swapped.length;
    width = swapped[0].length;
    shape = swapped;
  }




  public static void main(String[] args) {
    CreateShape temp = new CreateShape(6, 6, 'a', new char[][]{{'x', '.', 'a'},
        {'.', '.', 'b'},
        {'x', '.', 'c'}}, "x . a\n"
        + ". . b\n"
        + "x . c");
    temp.rotateCW();
    System.out.println(temp);
    temp.rotateCW();
    System.out.println(temp);
    temp.rotateCW();
    System.out.println(temp);
  }




SHAPE a
height: 3; width: 3; rotation: CW90
x.x
...
cba

SHAPE a
height: 3; width: 3; rotation: CW180
c.x
b..
a.x

SHAPE a
height: 3; width: 3; rotation: CW270
abc
...
x.x

10-06 09:29