Reputation:
When I run the following test case:
@Test(timeout=1000) public void shape_displayChar2_rot(){
String layout =
"b....\n"+
".....\n"+
"....b\n"+
"";
String expect =
"SHAPE z\n"+
"height: 5; width: 3; rotation: CW90\n"+
"..z\n"+
"...\n"+
"...\n"+
"...\n"+
"z..\n"+
"";
char newDC = 'z';
char dc = getDisplayChar(layout);
Shape shape = FitIt.makeShape(layout,dc);
shape.setDisplayChar(newDC);
shape.rotateCW();
assertEquals(newDC, shape.getDisplayChar());
String actual = shape.toString();
assertEquals(expect,actual);
}
I get the following failure:
Expected:
SHAPE z
height: 5; width: 3; rotation: CW90
..z
...
...
...
z..
My actual code's result:
Actual:
SHAPE z
height: 5; width: 3; rotation: CW90
b....
.....
....b
..z
...
...
...
z..
The String layout
from the above test case is rotated by rotateCW()
by 90 degrees ClockWise and also changes its character. My question is why am I getting:
b.... <<<<----- WHY AM I GETTING THIS ?
..... <<<<----- WHY AM I GETTING THIS ?
....b <<<<----- WHY AM I GETTING THIS ?
..z
...
...
...
z..
instead of just:
..z
...
...
...
z..
Code:
import java.util.*;
public class CreateShape implements Shape {
private String layout;
private int height;
private int width;
private char dc;
private Rotation initialPos;
private char[][] shape;
//private char[][] rotatedShape = new char[height][width];// = new char[shape.length][shape[0].length];
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;
//nextPos = Rotation.CW0;
}
public Rotation getRotation() {
return initialPos;
}
public int getHeight() {
return this.height;
}
public int getWidth() {
return this.width;
}
public char getDisplayChar() {
return dc;
}
public void setDisplayChar(char c) {
this.dc = c;
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
//if(shape[i][j] == '.')
//this.newLayout += shape[i][j];
if (shape[i][j] != '.') {
shape[i][j] = c;
}
}
//this.newLayout+="\n";
}
}
public void rotateCW() {
initialPos = initialPos.next();
int w = shape.length;
int h = shape[0].length;
char[][] 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;
}
public boolean isFilledAt(int row, int col) {
if (row < 0 || row >= height || col < 0 || col >= width) {
throw new FitItException("Oops! Out of bounds!");
} else {
for (int r = 0; r <= height; r++) {
for (int c = 0; c <= width; c++) {
if (shape[row][col] == dc) {
return true;
}
}
}
}
return false;
}
public String toString() {
return "SHAPE " + this.dc + "\n" +
"height: " + this.height + ";" + " width: " + this.width + "; " + getRotation().toString() + "\n" +
this.layout;
}
}
class FitIt
public class FitIt {
public static Shape makeShape(String layout, char displayChar) {
Shape result;
int height = 0;
int width = 0;
Scanner data = new Scanner(layout);
while (data.hasNextLine()) {
String line = data.nextLine();
width = line.length();
height++;
}
char[][] charLayout = new char[height][width];
Scanner data2 = new Scanner(layout);
for (int i = 0; i < height; i++) {
String line = data2.nextLine();
for (int j = 0; j < width; j++) {
if (line.charAt(j) != '.') {
charLayout[i][j] = displayChar;
}
if (line.charAt(j) == '.') {
charLayout[i][j] = line.charAt(j);
}
}
}
data2.close();
result = new CreateShape(height, width, displayChar, charLayout, layout);
return result;
}
Upvotes: 4
Views: 93
Reputation: 7057
When you rotate the text, you are appending the rotated lines to the current layout.
You are not starting from a fresh String
. So your original layout is preserved in the result.
Upvotes: 3
Reputation: 1697
In your rotateCW()
method, you forget to clear the layout
. You can add layout = ""
before the for loop and have a try.
Upvotes: 3