2015-04-26 34 views
2

мой метод rotateCW() должен поворачивать фигуру по часовой стрелке на 90 градусов каждый раз при ее вызове. Если сделать следующее:Метод каждый раз дает тот же результат

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

Тогда форма будет повернут 270 градусов clockswise начиная с 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!"; 
    } 
} 
+0

Что такое "Вращение" в вашем коде ???? и что означает поворот фигуры –

+0

@AbhishekMishra, Rotation - это перечисление. Я также добавил, что перечисление в коде –

+0

на том основании, что вы говорите, что оно не вращается.? \ –

ответ

1

Вы не обновит форму после выполнения подкачки. Добавление shape = swapped делает вращение не проходит вместо вызова снова повернуть на оригинальной форме:

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 
+0

Большое спасибо! @DTing –

Смежные вопросы